I;m having a hell of a time this simple program im trying to create... any help would be great! most of it is right except can't get the formula to show up so theres an extra line of code in there because i dont know what to do
this is what I have to get this to do...

Hello World! My name is Your Name.

Enter four numbers in the format nnn nnn nnn nnn <enter>: 18 4 67 5

((18 / 4) + 67) * 5 = 355

The Average of the input numbers is: 23.5

Have a nice day!

{
int num1;
int num2;
int num3;
int num4;
int result;
float average;

printf("Hello World! My name is \n\n");
printf("Enter four numbers in the format nnn nnn nnn nnn <enter>:\n\n");
scanf("%d %d %d %d" , &num1, &num2, &num3, &num4);
result = ((num1 / num2) + num3) * num4;
printf("((num1 / num2) + num3) * num4 is %d\n\n", ((num1 / num2) + num3) * num4);
printf("((%d / %d) + %d) * %d is %d\n\n"), num1, num2, num3, num4, result;
average =(num1 + num2 + num3 + num4)/4;
printf("The Average of the input numbers is: %.1f\n\n" , average);
printf("adios!\n\n");

return 0;
} // main

You misplaced the parentheses on your print line. It should be:

printf("((%d / %d) + %d) * %d is %d\n\n", num1, num2, num3, num4, result); //notice ) at the end and not after the last \n".

If you are wondering why your code still compiled with the misplaced parenthesis, well, that is too long to explain. Long story short: don't use antiquated C functions like scanf/printf. Use IOstreams instead.

Edited 5 Years Ago by mike_2000_17: n/a

Your average computation:

average =(num1 + num2 + num3 + num4)/4;

Will not output floating-point values (i.e. with decimals), this is because the rhs (right-hand side) of the expression involves only integer values (i.e. num1-2-3-4 are integers and the literal value 4 is also interpreted as an integer value). The compiler will interpret this whole expression using "integer arithmetic". Basically, integer arithmetic performs regular math except for the division operations, because it must always yield an integer value. So, an integer division will discard the remainder of the division, for example, 4 / 3 gives 1 because 3 fits only 1 time in 4 with a remainder of 1/3 which is discarded. Integer arithmetic also include the modulus operator % (percent sign) which gives you the remainder (without the division), so 4 % 3 gives 1.

The fix is very simple. You just need to tell the compiler to switch to floating-point arithmetic (which is "normal math", as your pocket calculator would to, as in, 5 / 2 = 2.5). You can do that by adding a 0 decimal to the literal 4, as so:

average =(num1 + num2 + num3 + num4) / 4.0; //notice the .0

This makes the compiler interpret the 4 as a floating-point value, and by the rules of C/C++, the division gets "promoted" to a floating-point division instead of an integer division (i.e. the rule is essentially that for any expression, the most "sophisticated" type determines the arithmetic used for the expression, i.e., it raises all the other values involved up to that "better" type).

If you had 4 stored into an int variable, you would have the same problem, as in:

int divider = 4;
float average =(num1 + num2 + num3 + num4) / divider; //this will invoke integer arithmetic!

The escape that situation, you need, again, to explicitly tell the compiler to switch to floating-point arithmetic by explicitly cast one of the operands of the division to a floating-point value, as so:

//either this:
float average = float(num1 + num2 + num3 + num4) / divider;
//or this:
float average = (num1 + num2 + num3 + num4) / float(divider);
This article has been dead for over six months. Start a new discussion instead.