Jclee
Jclee

Reputation: 39

C adding up integers stored from a loop

I want my program to ask the user for numbers, afterwards displaying how many entries were even and odd, then displaying the total of all the even entries and the total of all the odd entries.

I've got it all except the total part.

How do I reference correctly the entries from my oddcount++ and evencount++ ? I was thinking maybe an array but that may be over-complicating things. For loop dealing with the total?

#include <stdio.h>


int evencount = 0, oddcount = 0, i = 0;
int eventotal, oddtotal;



int main () {


int number;
char name[256];



printf("Enter your name \n");
scanf("%s", &name);

printf("Enter numbers within 1-100 \n");
printf("Enter 0 to quit\n");

while (number != 0) {
    scanf("%d", &number);
    if (number%2 == 1) {
        oddcount++;
    }
    else {
        evencount++;

    }

};



printf("%s,the numbers you have entered are broken down as follows: \n", name);


    printf("%d odd entries \n", oddcount);
    printf("%d even entries\n", evencount);

//printf("You entered even numbers with a total value of %d \n", eventotal );
//printf("You entered odd numbers with a total value of %d \n", oddtotal);



return 0;

}

Upvotes: 0

Views: 194

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310940

The program can look the following way

#include <stdio.h>

int main( void ) 
{
    unsigned int evencount = 0, oddcount = 0;
    unsigned int eventotal = 0, oddtotal = 0;

    unsigned int number;
    char name[256];

    printf( "Enter your name: " );
    scanf( "%255s", name );

    printf( "Enter numbers within 1-100 \n" );
    printf( "Enter 0 to quit\n" );

    while ( scanf( "%u", &number ) == 1 && number != 0 ) 
    {
        if ( number % 2 == 1 ) 
        {
            oddcount++;
            oddtotal += number;
        }
        else 
        {
            evencount++;
            eventotal += number;
        }
    }

    printf( "%s,the numbers you have entered are broken down as follows: \n", name);

    printf( "%u odd entries \n", oddcount );
    printf( "%u even entries\n", evencount );

    printf( "You entered even numbers with a total value of %u\n", eventotal );
    printf( "You entered odd numbers with a total value of %u \n", oddtotal );

    return 0;
}

Or the program could be written using arrays instead of separate variables. For example

#include <stdio.h>

int main( void ) 
{
    enum { Even = 0, Odd = 1 };

    unsigned int counts[2] = { 0, 0 };
    unsigned int totals[2] = { 0, 0 };

    unsigned int number;
    char name[256];

    printf( "Enter your name: " );
    scanf( "%255s", name );

    printf( "Enter numbers within 1-100 \n" );
    printf( "Enter 0 to quit\n" );

    while ( scanf( "%u", &number ) == 1 && number != 0 ) 
    {
        ++counts[number % 2];
        totals[number % 2] += number;
    }

    printf( "%s,the numbers you have entered are broken down as follows: \n", name);

    printf( "%u odd entries \n", counts[Odd] );
    printf( "%u even entries\n", counts[Even] );

    printf( "You entered even numbers with a total value of %u\n", totals[Even] );
    printf( "You entered odd numbers with a total value of %u \n", totals[Odd] );

    return 0;
}

If your compiler supports C99 then you could initialize the arrays the following way for readability

unsigned int counts[2] = { [Even] = 0, [Odd] = 0 };
unsigned int totals[2] = { [Even] = 0, [Odd] = 0 };

As for your while loop

while (number != 0) {
    scanf("%d", &number);
    if (number%2 == 1) {
        oddcount++;
    }
    else {
        evencount++;

    }
};
^^^

then for starters there is no need to place an empty statement after it. Variable number was not initialized and has indeterminate value. Function scanf can issue an error. In this case variable number will not contain a new value. However it is checked in the loop. As result the loop can be infinite. And at last you have to include increments of variables oddtotal and eventotal in the loop.

Upvotes: 2

LPs
LPs

Reputation: 16213

First of all

int number;

should be

int number=1;

Otherwise is UB.

Second thing

scanf("%s", &name);

have to be

scanf("%s", name);

If you try to compile your code with -Wall option it will warn you about this last point:

test.c: In function ‘main’:
test.c:467:2: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[256]’ [-Wformat=]
  scanf("%s", &name);
  ^

Last thing your loop must avoid to count 0 as a even number:

while (number != 0)
{
    scanf("%d", &number);

    if (number)
    {
        if (number%2 == 1)
        {
            oddcount++;
        }
        else
        {
            evencount++;
        }
    }
}

Upvotes: 1

Haris
Haris

Reputation: 12270

What you can do is keep integers for even and odd sum and add them on the fly when the user enters the numbers.

And initialize the variable number since the first comparison in the while loop is with an uninitialized variable, thats undefined behavior.

You loop should look something like this

number = 1;
while (number != 0)
{
    scanf("%d", &number);
    if (number%2 == 1)
    {
        oddcount++;
        oddtotal += number;
    }
    else 
    {
        evencount++;
        eventotal += number
    }
}

printf("sum of even number = %d\n", eventotal);
printf("sum of odd number = %d\n", oddtotal);


Since array name is the base address of that array, you do not need & for scanf()

scanf("%s", name);

NOTE:

1) Use the standard definition of main()

int main(void) //if no command line arguments.

2) Global variables default initialization value is 0. So you don't need to initialize them like you did here.

int evencount = 0, oddcount = 0, i = 0;

Upvotes: 2

Michael
Michael

Reputation: 58427

Use two variables:

int evensum = 0, oddsum = 0;

if (number%2 == 1) {
    oddcount++;
    oddsum += number;
}
else {
    evencount++;
    evensum += number;

}

Or an array with two entires:

// The sum of the even numbers go in sum[0], and the sum of the odd numbers
// go in sum[1]
int sum[] = {0, 0};

if (number%2 == 1) {
    oddcount++;
}
else {
    evencount++;

}
sum[number&1] += number;

Upvotes: 2

Related Questions