Switch语句不会重复计数

时间:2016-11-25 20:54:43

标签: c loops switch-statement

这个程序的目标是创建一个介于0到999之间的值数组,然后计算[0,199],[200,399],[400,599]范围内的值的数量等等。

问题是如果计数已经发生,switch语句将不会继续计数。 (注意:必须使用switch语句)

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

int main()
{
    int n;
    int randN;
    int rand(); 
    int countOne = 0;
    int countTwo = 0;
    int countThree = 0;
    int countFour = 0;
    int countFive = 0;
    int countSix = 0;
    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;

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

    }   

    //PRINTING THE N-size ARRAY  

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

    //SORTING THE N-size ARRAY 

    //Created this to avoid to solve a fragmentation problem 
    int p2[5] = {countOne, countTwo, countThree, countFour, countFive};

    for (i = 0 ;  i < n ; i++)
    {   
        switch(i)
        {
            case 1:
                if(array[i] >= 0 && array[i] <=199)
                {
                    countOne++;
                    break;
                }
            case 2:
                if(array[i] >= 200  && array[i] <= 399)
                {
                    countTwo++;
                    break;
                }
            case 3:
                if(array[i] >= 400 && array[i] <= 599)
                {
                    countThree++;
                    break;
                }
            case 4:
                if(array[i] >=600 && array[i] <= 799)
                {
                    countFour++;
                    break;
                }
            case 5:
                if(array[i] >=800 && array[i] <= 999)
                {
                    countFive++;
                    break;
                }

        }
    }
    printf("There is %d integers between 0 and 199\n", countOne);
    printf("There is %d integers between 200 and 399\n", countTwo);
    printf("There is %d integers between 400 and 599\n", countThree);
    printf("There is %d integers between 600 and 799\n", countFour);
    printf("There is %d integers between 800 and 999\n", countFive);

}

输入'34'时的输出

阵列的大小是多少 34个
823个
7
347个
587个
184个
953个
672个
188个
161个
955个
828个
719个
762个
359个
909个
164个
52个
277个
43个
298个
744个
478个
792个
254个
508个
636个
789个
963个
62个
637个
952个
272个
689个
298个
0到199之间有1个整数 在2到399之间有1个整数 400到599之间有1个整数 在600到799之间有0个整数 800到999之间有1个整数

2 个答案:

答案 0 :(得分:1)

正如评论中所提到的,您的代码没有按照您在问题中提到的那样做。 Jonathan Leffler解释了为什么你的评论中没有得到输出。我认为这是你想要实现的目标。

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

int main()
{
    int n;
    int randN;
    int rand(); 
    int countOne = 0;
    int countTwo = 0;
    int countThree = 0;
    int countFour = 0;
    int countFive = 0;
    int countSix = 0;
    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;

        /* MAYUR: These two lines are unnecessary and removing them. */
        //p1=(int*)malloc(i*sizeof(int));
        //p1[i]= array[i];

    }   

    //PRINTING THE N-size ARRAY  

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

    //SORTING THE N-size ARRAY //MAYUR: Why? btw, you are not sorting it here!

    //Created this to avoid to solve a fragmentation problem //MAYUR: What problem? I am removing it.
    //int p2[5] = {countOne, countTwo, countThree, countFour, countFive};

    for (i = 0 ;  i < n ; i++)
    {
        //MAYUR: You can categorize the number for given range values and use switch case.

        int tempCategory = array[i] / 100;
        switch(tempCategory)
        {
            //Equivalent to (array[i] >= 0 && array[i] <=199)
            case 0:
            case 1:
                {
                    countOne++;
                } break;

            //Equivalent to (array[i] >= 200 && array[i] <=399)
            case 2:
            case 3:
                {
                    countTwo++;
                } break;

            //Equivalent to (array[i] >= 400 && array[i] <=599)
            case 4:
            case 5:
                {
                    countThree++;
                } break;

            //Equivalent to (array[i] >= 600 && array[i] <=799)
            case 6:
            case 7:
                {
                    countFour++;
                } break;

            //Equivalent to (array[i] >= 800 && array[i] <=999)
            case 8:
            case 9:
                {
                    countFive++;
                } break;

            default:
                {
                    printf("Invalid number array[%d]=[%d] tempCategory[%d]\n", i, array[i], tempCategory);
                } break;

        }
    }
    printf("There is %d integers between 0 and 199\n", countOne);
    printf("There is %d integers between 200 and 399\n", countTwo);
    printf("There is %d integers between 400 and 599\n", countThree);
    printf("There is %d integers between 600 and 799\n", countFour);
    printf("There is %d integers between 800 and 999\n", countFive);

}

答案 1 :(得分:-1)

问题是,你根本不需要使用开关盒。您需要访问一次数组的每个元素。只需一个for循环即可。您已使用相同的技术打印出数组的元素。删除不需要的部分后的必要代码如下:

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

int main()
{
int n;
int randN;
int rand(); 
int countOne = 0;
int countTwo = 0;
int countThree = 0;
int countFour = 0;
int countFive = 0;
//int countSix = 0;
//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;

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

}   

//PRINTING THE N-size ARRAY  

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

//SORTING THE N-size ARRAY 

//Created this to avoid to solve a fragmentation problem 
// int p2[5] = {countOne, countTwo, countThree, countFour, countFive};

for (i = 0 ;  i < n ; i++)
{   
    if(array[i] >= 0  && array[i] <= 199 )
    {
        countOne++;

    }

    else if(array[i] >= 200  && array[i] <= 399)
    {
        countTwo++;

    }

    else if(array[i] >= 400 && array[i] <= 599)
    {
        countThree++;

    }

    else if(array[i] >=600 && array[i] <= 799)
    {
        countFour++;

    }

    else
    {
        countFive++;

    }
}
printf("There is %d integers between 0 and 199\n", countOne);
printf("There is %d integers between 200 and 399\n", countTwo);
printf("There is %d integers between 400 and 599\n", countThree);
printf("There is %d integers between 600 and 799\n", countFour);
printf("There is %d integers between 800 and 999\n", countFive);

}

我检查了输出,现在是正确的。