简体   繁体   English

C语言程序显示错误结果,奇数

[英]Program in C is showing a wrong result, Odd Number

Enter number of integers to be stored : 5 输入要存储的整数数:5

Enter 5 integers: 输入5个整数:

 1 2 3 4 5

There are 2 even numbers in the set. 集合中有2个偶数。 There are 3 odd numbers in the set. 集合中有3个奇数。

Even numbers: 偶数:

2
4

Odd numbers: 奇数:

1
3
5

Output: 输出:

Sum of Odd Numbers is 51
Sum of Even Numbers is 6
--------------------------------
Process exited after 3.389 seconds with return value 0
Press any key to continue . . .

This is the code: 这是代码:

#include <stdio.h>

int main()
{
    int N, n;
    printf("Enter number of integers to be stored : ");
    scanf("%d", &N);
    int count[N];
    printf("\nEnter %d integers: \n", N);
    for(int n=0;n<N;n++)    
    {
      scanf("%d", &count[n]);
    }
    //Even and Odd Counter
    int even_counter=0, odd_counter=0;
    for(n=0;n<N;n++)
    {
        //even_counter
        if(count[n]%2==0)
        {
            even_counter++;
        }
        //odd_counter
        else
        {
            odd_counter++;
        }
    }
    printf("\nThere are %d even numbers in the set.", even_counter);
    printf("\nThere are %d odd numbers in the set.\n", odd_counter);


    //Sorting of Even and Odd
    int i=0;
    printf("\nEven numbers: \n");
    for(n=0;n<N;n++)
    {   
        if(count[n]%2==0)
        {
            printf("%d\n", count[n]);
        }
    }
    printf("\nOdd numbers: \n");
    for(n=0;n<N;n++)
    {
        if(count[n]%2==1)
        {
            printf("%d\n", count[n]);
        }
    }

    //Sum of Odd and Even Values

    //EvenSummation
    int even_lister[i], sumEven, odd_lister[i], sumOdd;
    for(n=0;n<N;n++)
    {   
        if(count[n]%2==0)
        {
            even_lister[i]=count[n];
            sumEven+=even_lister[i];
        }
        else //OddSummation      
        {
            int odd_lister[i], sumOdd, i=0;

            odd_lister[i]=count[n];
            sumOdd+=odd_lister[i];

        }
     }
     printf("\nSum of Odd Numbers is %d", sumOdd);
     printf("\nSum of Even Numbers is %d", sumEven);
}

What's wrong with my program? 我的程序怎么了? I've tried everything I knew :( The odd value is giving strange results. 我已经尝试了所有我知道的:(奇数值给出了奇怪的结果。

Here's the part where you calculate the sum of even numbers, which works: 这是您计算偶数之和的部分,它起作用:

    if(count[n]%2==0)
    {
        even_lister[i]=count[n];
        sumEven+=even_lister[i];
    }

Now here's the part where you calculate the sum of odd numbers, which doesn't: 现在,这里是您计算奇数之和的部分,而不是:

    else //OddSummation      
    {
        int odd_lister[i], sumOdd, i=0;

        odd_lister[i]=count[n];
        sumOdd+=odd_lister[i];

    }

Do you see the difference? 你看得到差别吗? There's an extra line in the second one. 第二个中有额外的一行。 In the version that doesn't work, you re-declared some local variables and assigned your values to those local variables. 在不起作用的版本中,您重新声明了一些局部变量,并将值分配给了这些局部变量。 That's why it doesn't work. 这就是为什么它不起作用。 You didn't do anything with the "original" variables in a higher-up scope that you later print to the screen. 您没有对更高范围的“原始”变量进行任何操作,随后将其打印到屏幕上。

Furthermore, both parts are actually broken because you never initialised either sumEven or sumOdd to 0 , so their values are unspecified, and you're adding to unspecified values to create other unspecified values. 此外,这两个部分实际上都是损坏的,因为您从未将sumEvensumOdd初始化为0 ,所以它们的值是未指定的,并且您要添加到未指定的值以创建其他未指定的值。 Whether this bug creates an observable symptom or not is undefined. 此错误是否产生可观察到的症状是不确定的。

Another problem is that you declare your arrays like this: 另一个问题是您这样声明数组:

int even_lister[i];

but i is a variable that you set to 0 and never changed. 但是i是一个变量,您将其设置为0且从未更改。 So those arrays have zero length and every single access to them is illegal. 因此,这些数组的长度为零 ,对它们的每次访问都是非法的。 Perhaps you meant to use n instead? 也许您打算使用n代替?

You really need to turn on your compiler warnings and read your code more carefully. 您确实需要打开编译器警告并更仔细地阅读代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM