2016-11-15 6 views
-1

Ich habe ein Array erstellt, das zwischen [0,1000] liegt, und ich habe es gedruckt. Der nächste Schritt besteht darin, das Array mithilfe von switch-Anweisungen in fünf verschiedenen Fällen 0 bis 199 usw. anzuordnen. Wenn Sie dies versuchen, wird die for-Schleife nicht gestoppt. Ich habe versucht, eine printf nach countOne in Fall 1, kein Ausdruck tritt auch nicht.For-Schleife wird nicht gestoppt

Irgendwelche Vorschläge

Vielen Dank für Ihre Hilfe

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int n; 
int arraySize; 
int randN; 
int rand(); 

int countOne = 0; 
int countTwo = 0; 
int countThree = 0; 
int countFour = 0; 
int countFive = 0; 
int countSix = 0; 
int *p; 
int *p1; 

int main() 
{ 
    printf("What is the size of the array\n"); 
    scanf("%d", &n); 

    //MAKING THE N-size ARRAY 

    int array[n]; 
    int i; 
    for (i = 0; i < n; i++) { 
     randN = rand() % 999; 
     array[i] = randN; 

     p = (int*)malloc(i * sizeof(int)); 
     p[i] = array[i]; 
    } 

    //SORTING THE N-size ARRAY 

    for (i = 0; i < n; i++) { 
     printf("%i\n", array[i]); 
    } 

    p = (int*)malloc(sizeof(int)); 
    p1 = (int*)malloc(5 * sizeof(int)); 
    p1[0] = countOne; 
    p1[1] = countTwo; 

    for (i = 0; i < n; i++) { 
     switch (i) { 
      case 1: 
      for (i = 0; array[i] >= 0 && array[i] <= 199; i++) { 
       countOne++; 
      } 
      case 2: 
      for (i = 0; array[i] >= 200 && array[i] <= 399; i++) { 
       countTwo++; 
       return countTwo; 
      } 
     } 
    } 
} 

HIER MEIN CODE ZUM AKTUELLEN:

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 

int main() 
{ 

int n; 
int arraySize; 
int randN; 
int rand(); 

int countOne = 0; 
int countTwo = 0; 
int countThree = 0; 
int countFour = 0; 
int countFive = 0; 
int countSix = 0; 
int *p; 
int *p1; 

    printf("What is the size of the array\n"); 
    scanf("%d", &n); 

//MAKING THE N-size ARRAY 

    int array[n]; 
    int i; 
    for (i = 0 ; i < n; i++) 
    { 
      randN=rand() % 999; 
      array[i]=randN; 

      p=(int*)malloc(i*sizeof(int)); 
      p[i]= array[i]; 

    } 

//PRINTING THE N-size ARRAY 

     for (i = 0; i < n; i++) 
     { 
      printf("%i\n", array[i]); 
     } 

//SORTING THE N-size ARRAY 

int j; 
for (j = 0 ; j < n ; j++) 
    { 
     switch(j) 
     { 
      case 1: 
       for(i = 0 ; array[i] >= 0 && array[i] <= 199; i++) 
{ 
       countOne++; 
       return countOne; 
       } 
      case 2: 
       for(i = 0 ; array[i] >= 200 && array[i] <= 399; i++) 
       { 
       countTwo++; 
       return countTwo; 
       } 
     } 
    } 

HIER IST DER AUSDRUCK:

Was die Größe des Arrays Es gibt 0 ganze Zahlen zwischen 0 und 199 Es gibt 0 ganze Zahlen zwischen 200 und 399

+1

warum Sie Neuzuweisung 'jeder Iteration P'? Du weißt, dass du Erinnerungen verlierst, oder? –

+1

Ihre 'switch' Anweisung hat eine Menge Probleme. –

+2

Sieht aus wie der ganze Code ... –

Antwort

0

Das Programm hat viele Probleme.

Hier ist eine einfachere Version:

#include <stdio.h> 
#include <stdlib.h> 

int compare_ints(const void *a, const void *b) { 
    const int *pa = a, *pb = b; 
    return (*pa > *pb) - (*pa < *pb); 
} 

int main(void) { 
    int i, n; 
    int stats[5] = { 0, 0, 0, 0, 0 }; 

    printf("What is the size of the array?\n"); 
    scanf("%d", &n); 

    //MAKING THE N-size ARRAY 
    int *array = malloc(n * sizeof(int)); 
    int *saved = malloc(n * sizeof(int)); 

    if (array == NULL || saved == NULL) { 
     printf("cannot allocate arrays\n"); 
     exit(1); 
    } 

    for (i = 0; i < n; i++) { 
     int randN = rand() % 999; 
     saved[i] = array[i] = randN; 
     stats[randN/200] += 1; 
    } 

    printf("initial array contents:\n); 
    for (i = 0; i < n; i++) { 
     printf("%i\n", array[i]); 
    } 
    printf("\n"); 

    //SORTING THE N-size ARRAY 
    qsort(array, n, sizeof(*array), compare_ints); 

    printf("sorted array:\n); 
    for (i = 0; i < n; i++) { 
     printf("%i\n", array[i]); 
    } 
    printf("\n"); 

    for (i = 0; i < 5; i++) { 
     printf("%d values between %d and %d\n", stats[i], i * 200, (i + 1) * 200 - 1); 
    } 
    printf("\n"); 

    // do whatever else you are supposed to with array and saved 
    //... 

    free(array); 
    free(saved); 
    return 0; 
} 
+0

danke sehr elegant. Wird sich wiederholen. Punkt meiner Aufgabe ist, schreibe den Algorithmus zweimal mit einer for-Schleife für einen Versuch und einen Schalter für den anderen – user7117719

Verwandte Themen