Joel
Joel

Reputation: 19

Having trouble calculating the average of 5 numbers in C

I am very new to C and to programming and I am having issues with getting the average of 5 numbers. I have tried everything I could think of and I have not clue why I am not getting a number besides 0. My code is:

#include <stdio.h>

int main (void)
{    
  long int first,second,third,fourth,fifth, sum;   
  float avg = (first+second+third+fourth+fifth)/5;     
  printf("Please put in five numbers\n");    
  scanf("%d%d%d%d%d", &first, &second, &third, &fourth, &fifth);    
  printf("You entered: %d %d %d %d %d\n" , first, second, third, fourth, fifth);    
  sum = first+second+third+fourth+fifth;    
  printf("The sum of the numbers you entered is %d\n",
            sum);    
  printf("The average is %d\n", avg);    

  return 0;
}

Upvotes: 0

Views: 2910

Answers (3)

Harshini
Harshini

Reputation: 73

I think the problem is that you are initialising average value with the variables that are given after calculating average .Try to calculate average value after giving the variables i.e first,second,third,fourth and fifth.

Upvotes: 2

Suraj Jain
Suraj Jain

Reputation: 4536

There are four problems in your code :

  1. You are calculating average before you take in variable.
  2. You are dividing a sum of integer with other integer due to which , the fractional part will truncate.Integer division truncates the fractional part.
  3. You are printing avg which you defined as float with format specifier %d so it will cause undefined behaviour.
  4. You are using %d specifier for long int , it should %ld.

Taking Into Account All These Problem Your Code Becomes.

#include <stdio.h>
int main (void){

long int first,second,third,fourth,fifth, sum;    
printf("Please put in five numbers\n");    

scanf("%ld%ld%ld%ld%ld", &first, &second, &third, &fourth, &fifth);
//Changed Due To Problem 4.
printf("You entered: %ld %ld %ld %ld %ld\n" , first, second, third, fourth, fifth);  

sum = first+second+third+fourth+fifth; 

float avg = (sum)/5.0;     //Changed Problem No.2 and N0.1

printf("The sum of the numbers you entered is %ld\n", //Changed Due To Problem 4.
        sum);    

printf("The average is %f\n", avg);      //Changed Problem No.3  

return 0;
}

Tip
Always Try To solve the problem yourself before asking for help , it will benefit you more.
And First Have It Ready On a Paper Before Coding , it will decrease many bugs.

Upvotes: 1

DYZ
DYZ

Reputation: 57085

The main problem is that you calculate the average before getting the numbers. The values of the uninitialized variables happened to be 0, and the average of five zeros is a zero, too.

The second problem is that you divide a sum of integer numbers by another integer number. Integer division discards the fractional part of the quotient.

The line:

float avg = (first+second+third+fourth+fifth)/5;

must be moved after scanf, and 5 must become 5.0. The result should be displayed as a floating-point number with %f, not with %d.

Upvotes: 1

Related Questions