Noob here Just curious learning to code

+1 Lucas Cowles · February 14, 2015
Hello all,

I have been following Buckey's tutorials and I add something to the end of them just to see what would happen. This time I added something really simple but the code did not give me the output I expected here is the code. 

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

int main()
    float grade = 0;
    float scoreEntered = 0;
    float numberoftest = 0;
    float avg = 0;

    printf("Press 0 when complete. \n\n");

    do {
           printf("test:%.0f\t\tAverage:%.02f \n", numberoftest, avg);
           printf("\n Enter test score: ");
           scanf(" %f",&scoreEntered);
           grade += scoreEntered;
           avg = grade / numberoftest;

    }while(scoreEntered !=0);

    return 0;
 output on the averages are about 21 points different.

Just curious as to why?

Post a Reply


Oldest  Newest  Rating
0 Tomislav Keškić · February 14, 2015
try to initialize grade,scoreEnter,etc to 0.0 and run the program again
0 Lazar Bulic · February 14, 2015
Just remove 0 if front of 2f
I am talking about this line: 

printf("test:%.0f\t\tAverage:%.02f \n", numberoftest, avg);

Also you can simplify you code by merging these to lines:

printf("test:%.0f\t\tAverage:%.02f \n", numberoftest, avg);
avg = grade / numberoftest;

you don't have to set value to  scoreEntered. 
0 Lucas Cowles · February 15, 2015
Thanks for the replies.

Lazer, I removed the 0. I thought man it was that easy......but that did not solve it.. I tried combining the code but that opened up a whole new can of worms. I changed the code but still getting the the mix match in the average.
#include <stdio.h>
#include <stdlib.h>

int main()
    float grade = 0.0;
    float scoreEntered = 0.0;
    float numberoftest = 0.0;
    float avg = 0.0;

    printf("Press 0 when complete. \n\n");

            printf("test: %.0f \t\t Average: %.2f \n", numberoftest, avg);
            printf("\n Enter test score: ");
            scanf(" %f", &scoreEntered);
            grade += scoreEntered;
            avg = grade / numberoftest;

  printf("your total average is: %.2f ",avg);

    return 0;
0 imran LP · February 15, 2015
Lucas listen up . I tried running your code given in the query . And fortunately its working totally fine . i tried it with .%0.02f and %0.2f but yeah the output comes out to same . but anyways your code is working totally fine and even the average is accurate . i couldnt see any "21 points difference " see here i entered full marks so that we ll have no confusion calculating it manually . i entered 3 test scores i.e : 100, 100,90 and the average for this is 96.67 . so thats clear the theres no error in the code/images/forum/upload/2015-02-15/e55f7908141a8f6d4240325fc12d5f23.PNG

And now talking about the code that u modified and you are expecting some answer. well you cant expect your answer.know why ?
because you are going wrong . look at the output once again and see there you are already getting your output in the end in this example my over all average is 96.67 . but wait . this is the average of 3 Tests and in case to stop calculating the average (according to this set of code) you gotta press 0 and the code will end then and there , its done. its a do while statement . but you modified your test and added """"printf("your total average is: %.2f ",avg);"""" this thing there so the program continues . but at this time what it ll do is its will calculate the average of FOUR tests rather then THREE  why ? because we entered 0 to exit the calculation part but it didnt really get it what to do . and the computer thinks we have entered 4 values so its giving out the average of  4 tests .see here/images/forum/upload/2015-02-15/e92deaee66e2f6c3f0a6ce608ae436a5.PNG

thats the average of 4 tests not three tests and so you are thinking that the average is coming out wrong.
Now all i can say is what you are expecting cant be done through this code . we need to write different codes for that and even i donno how to do so . need to think . LOL even am a beginner just been two weeks.
0 Lazar Bulic · February 15, 2015
Ok this problem is a little more complex. I didn't test the program good when i said to remove 0. So here are some screen shoots. 

Here is your original code. You can see the difference between calculator and the program. 


So in the next picture I just removed the 0.2 so the program prints all decimal points. But as you can see on the last one there is mistake again.

So in the last example I declared 

double avg;

and the result was again accurate but again limited to a curtain point. 
So I was told the C just wants to be fast and after some points randomly adds numbers at x.xxxxxxxxxxxxxxxxxxxxxxxx
But also here is an explanation:
0 Lucas Cowles · February 15, 2015
Thank you all so much for the info,

imran LP, that was the answer I was looking for. I was not realizing it was using that zero as a test score as well after doing the calculation that is exactly what was happening. before I go to the next lesson I will figure how to make it do what I want now that I know how it came up with that other value for average. 

Lazer thanks for the link.   

Appreciate the help, I was trying to really understand it before moving on and it was bugging me.

0 imran LP · February 16, 2015
:) yeah
  • 1



One of the most popular languages of all time.

Bucky Roberts Administrator