Thread: Need help with my C code

  1. #1
    Registered User
    Join Date
    Mar 2015
    Posts
    7

    Need help with my C code

    I wrote this code, but when I want to print the average at the end, all it says is 0.00.
    What am I doing wrong?

    Code:
    #include <stdio.h>
    
    char first, last;
    int test_one, test_two, test_three;
    int test_one_score, test_two_score, test_three_score;
    int averageGrade;
    
    int main()
    {
        //Get the maximum scores.
        printf("Please enter the maximum score possible for test one: ");
        scanf("%d", &test_one);
    
        printf("Please enter the maximum score possible for test two: ");
        scanf("%d", &test_two);
    
        printf("Please enter the maximum score possible for test three: ");
        scanf("%d", &test_three);
    
    
        //Get students name.
        printf("\nEnter your first and last name initials: ");
        scanf(" %c%c", &first, &last);
    
    
        //Get student test grades.
        printf("\n\nEnter your test one score: ");
        scanf("%d", &test_one_score);
    
        printf("Enter your test two score: ");
        scanf("%d", &test_two_score);
    
        printf("Enter your test three score: ");
        scanf("%d", &test_three_score);
    
    
        //Calculate average grade.
        averageGrade = (test_one_score + test_two_score + test_three_score)/(test_one + test_two + test_three) * 100;
    
    
        //Output student's info.
        printf("\nStudent's initials: %c%c", first, last);
        printf("\nAverage Score: %d\n", averageGrade);
    
    
        return (0);
    }

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Use floats in place of integers:

    Why your code is not working is :

    Code:
    averageGrade = (test_one_score + test_two_score + test_three_score)/(test_one + test_two + test_three) * 100;
    An integer divided by an integer should give an integer - the fractional part of the result is ignored a problem which you can solve by 1) casting to float before dividing 2) using floats in your whole program.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    799
    You doing the math wrong that's why, an average is calculated by the sum of all relevant values by the number of them.
    Here what you should have wrote:
    Code:
    averageGrade = (test_one_score + test_two_score + test_three_score) / 3;
    averageMaxGrade = (test_one + test_two + test_three) / 3;
    printf("Average Score: %i out of %i",averageGrade,averageMaxGrade);
    Edit: Used wrong name in printf
    Last edited by awsdert; 03-14-2015 at 02:14 AM.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    If "(test_one_score + test_two_score + test_three_score)" is greater than "(test_one + test_two + test_three)", the result will always be zero.

    6.5.5 Multiplicative operators

    6 When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded.88) If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2015
    Posts
    7
    I changed all my variables to floats and now the average is coming out properly. Thanks.
    Is this fine or should I not be using floats for everything?

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    799
    The type that should be used depends on the data your working with, so here turning all the grade variables into floats is fine.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Also, take a look at the proper way to define main and why global variables are bad .
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM