For循环不会停止

时间:2016-11-15 22:07:04

标签: c loops for-loop

我已经制作了一个介于[0,1000]之间的数组,并且我打印了它。下一步是使用switch语句将数组排列成五种不同的情况0到199等。当尝试这样做时,for循环不会停止。我尝试在案例1中printf之后放countOne,也没有打印输出。

任何建议

感谢您的帮助

#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;
            }
        }
    }
}

这是我当前的代码:

#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;
                }
        }
    }

这是打印出来的:

阵列的大小是多少 3 823 7 347 0到199之间有0个整数 在200和399之间有0个整数

1 个答案:

答案 0 :(得分:0)

该计划存在很多问题。

这是一个更简单的版本:

#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;
}
相关问题