Click here to Skip to main content
15,886,851 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The output always rounds up or down? instead of showing a decimal float? Any help is well appreciated!
Thanks!
Here is the code:

C#
#include<stdio.h>
#include<conio.h>

 void DisplayInstructions();
 int CalculateAverage(int a,int b, int c);
 float DisplayAnswer(float ans);

 void main()
 {
     int Integer1 = 0, Integer2 = 0, Integer3 = 0;
     float answer;
     DisplayInstructions();
     scanf_s("%d", &Integer1);
     scanf_s("%d", &Integer2);
     scanf_s("%d", &Integer3);
     answer = CalculateAverage( Integer1, Integer2, Integer3);
     DisplayAnswer(answer);
     _getch();
 }

 void DisplayInstructions()
 {
     printf("Enter 3 Integers seperated by the ENTER key\n");
 }

 int CalculateAverage(int a, int b, int c)
 {
     float average, num = 3;
     average = ( a + b + c )/num;

     return average ;
 }

 float DisplayAnswer(float ans)
 {
     printf("Answer=%f\n", ans);
 }
Posted
Comments
[no name] 16-Feb-15 19:36pm    
If you inspect more closely it is NOT rounding up and down but ALWAYS truncating: http://www.cplusplus.com/doc/tutorial/typecasting/
If in doubt use your debugger.

What would you expect if you return int? Change "int CalculateAverage" to float "int CalculateAverage".

—SA
 
Share this answer
 
Change int CalculateAnswer to return a float. In printf change format string for 2 decimal points. Look up cplusplus.com for details.
 
Share this answer
 
v2
Comments
LBIreland 16-Feb-15 19:16pm    
Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900