I am writing a program to find the average value of numbers entered but I am having difficulty getting the total value of all the numbers to show. Could you please help me with this code I have written?
Thanks
//program to find the average of values entered #include <stdio.h> int main() { int sumOfValues = 0; int numberOfValueEntered = 0; int storage = 0; float averageOfValues = 0.0; printf("Enter a number or enter 0 to find the average of numbers entered\n"); scanf("%i", &storage ); // reads the value and saves it in storage while (storage != 0) // when storage is not 0 carry out loop { ++numberOfValueEntered ; //adds one to numberOfValueEntered (storage += sumOfValues ); //adds the value of storage to sumOfValues printf("%i,%i" ,numberOfValueEntered,sumOfValues); //test to see if numberOfValueEntered,sumOfValues is working correctly storage = 0; // return value of starage to 0 printf("Please enter the next number or type 0 to exit\n"); scanf("%i", &storage ); // reads the value and saves it in storage }// exits the loop when 0 is entered averageOfValues = (sumOfValues / numberOfValueEntered); //works out the average and stores it in averageOfValues //prints all the values averages etc printf("The results are as follows\n"); printf("you have entered %d numbers, The total value of numbers entered is %i, and the average is %f", numberOfValueEntered,sumOfValues, averageOfValues); return 0; }
This works correctly
int sumOfValues = 0; int numberOfValueEntered = 0; int storage; float averageOfValues; printf("Enter a number or enter 0 to find the average of numbers entered: "); scanf("%i", &storage ); // reads the value and saves it in storage while (storage != 0) // when storage is not 0 carry out loop { ++numberOfValueEntered ; //adds one to numberOfValueEntered sumOfValues += storage; //adds the value of storage to sumOfValues printf("%i, %i\n" ,numberOfValueEntered,sumOfValues); //test to see if numberOfValueEntered,sumOfValues is working correctly printf("Please enter the next number or type 0 to exit: "); scanf("%i", &storage ); // reads the value and saves it in storage }// exits the loop when 0 is entered averageOfValues = (float)sumOfValues / (float)numberOfValueEntered; //works out the average and stores it in averageOfValues //prints all the values averages etc printf("The results are as follows\n"); printf("you have entered %d numbers\nThe total value of numbers entered is %i, and the average is %f\n", numberOfValueEntered, sumOfValues, averageOfValues);
This line:
(storage += sumOfValues ); //adds the value of storage to sumOfValues //
is wrong. it translates to:
storage = storage + sumOfValues
I suspect what you mean is:
sumOfValues = sumOfValues + storage.
which would be written like this:
(sumOfValues += storage);
thank you for the reply i have added it into my code and i am still getting a value of 0 for the total numbers entered which in turn is giving me an average of 0.0,
i cant understand why it will not read the value into the int..
Hi,
Answer 1 is correct.
Change
(storage += sumOfValues ); //adds the value of storage to sumOfValues
to
sumOfValues += storage; //adds the value of storage to sumOfValues
and you get the correct sum of all values.
The hint from answer 2 depends on your compiler. The "%d" is ANSI-C but some compiler also accepts "%i" for int.
And when you change the line
averageOfValues = (sumOfValues / numberOfValueEntered);
averageOfValues = (float)sumOfValues / numberOfValueEntered;
you will get the correct average value. Without the (float)-cast there is a int calculation and you will allways get an value without fractional part. I.e. the average of 5 / 2 will be 2.0. With sumOfValues casted to float the result will be 2.5.
I think your scanf() is wrong.
should it not be:
sscanf(storage, "%i");
edit: Sorry your function should work but maybe try:
scanf("%d", &storage );
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)