Thread: average problem

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    6

    average problem

    Hi everyone, so i made a program which should calculate the average of your tests. however the average keeps the same! can someone help me out please? ty anyway^^

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    float notesadded = 0;
    float average = 0;
    float numberOftests = 0;
    float scoreentered = 0;
    char grade[50];
    
    printf("Hi this program will help you calculating your tests average!\n\a");
    printf("Whenever you want to quit the program just enter 0\n");
    
    do{
    printf("In %.2f tests your average is %.2f\n", &numberOftests, &average);
    printf("Enter now you new grade\n");
    
    scanf("%f", &scoreentered);
    notesadded += scoreentered;
    numberOftests++;
    average = notesadded / numberOftests;
    
    if(average >= 90){
    printf("Your grade is:Excellent\n");
    }else if(average > 80){
    printf("Your grade is:Normal\n");
    }else{
    printf("Your grade is:Bad\n");
    }
    
    
    }while(scoreentered !=0);
    


    ty^^

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should compile your code at a high warning level, then pay attention to the warnings. For example:
    Code:
    test.c: In function ‘main’:
    test.c:16:9: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
             printf("In %.2f tests your average is %.2f\n", &numberOftests, &average);
             ^
    Basically, the compiler is saying that on this line:
    Code:
    printf("In %.2f tests your average is %.2f\n", &numberOftests, &average);
    Instead of passing numberOftests and average as arguments, you passed &numberOftests and &average.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    Sorry i corrected the error and now its working.
    However i did not understand why it should have an"&".
    I learned that every time that its not an array i should put an "&", in this case it is a a float not an array thats why i wrote it. Can you explain when and why should i put "&"?
    ty ^^

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Th0rh4memer
    However i did not understand why it should have an"&".
    The compile error indicates that it should not have a "&".

    Quote Originally Posted by Th0rh4memer
    I learned that every time that its not an array i should put an "&", in this case it is a a float not an array thats why i wrote it. Can you explain when and why should i put "&"?
    You need to understand a few things:
    • Given an object x, &x results in a pointer to x, i.e., in this context, & is the address operator.
    • When an array y is passed as an argument, it is converted to a pointer to y's first element.
    • To print an object x with printf, you need to have a format specifier that matches the type of x, and then pass x as the corresponding argument.
    • To read into an object x with scanf, you need to have a format specifier that matches the type of x, and then pass a pointer to x as the corresponding argument.

    So, what you had was a printf call. Since you are not supposed to pass pointers to the objects as the corresponding arguments, passing &numberOftests and &average is wrong. If you had a scanf call instead, then they would be correct, except that then the format specifiers should have been %lf instead of %f.

    However, if you had an array of char named str, and if you want to read a string into str with scanf, you would not pass &str, but rather you would still pass str since str would be converted to a pointer to its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    i did not understand it :/ can u try to say in other words please?
    ty if u cnat dont worry

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Th0rh4memer
    i did not understand it :/ can u try to say in other words please?
    Do you understand pointers?

    If not, then blindly follow these rules of thumb for now:
    • When using printf, do not use &.
    • When using scanf with %s to read a string, do not use &.
    • Otherwise, when using scanf, use &.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    6
    ty u so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-25-2015, 04:10 PM
  2. Replies: 27
    Last Post: 02-14-2015, 11:17 AM
  3. Average of user input in a loop problem
    By tadm123 in forum C Programming
    Replies: 11
    Last Post: 10-03-2012, 10:53 AM
  4. Decidedly average homework problem
    By kingkobra in forum C++ Programming
    Replies: 21
    Last Post: 12-04-2009, 10:10 AM
  5. Grade program average problem
    By Zaz in forum C Programming
    Replies: 1
    Last Post: 11-20-2009, 07:11 PM

Tags for this Thread