Help with Average

I do not understand why this program will not work. This is the problem my professor gave me. "Write a complete program that does the following: Input positive integers which count and add in Sum. Let -1 be the signal to stop. Do not count -1 or add it into Sum. You must use a function, average, that takes as input two integer parameters for the count and the Sum. Have your function compute the average as a real number and return its value". This is what I have but it wont compile. I know this is extremely simple but I have almost no programming experience.

#include <iostream>
using namespace std;

float average(int sum, int count){
float a;
a = sum/count;
return a;
}

int main(){
int sum, count, a, i = 0;
while( i != -1 ) {
scanf(%i, i);
count = count + 1;
sum = sum + i;
}
a = average(sum, count);
printf("Sum:%i, Count:%i, Average:%f", sum,count,a);
return o;
}
Last edited on
Line 6: You're doing integer division. i.e. The result is going to be truncated before being converted to a float.

Line 13: scanf format must be quoted.

Line 17: a is an int. You going to trucate the result returned by average.

Line 18: You're using a %f format specified for a, but a is an int.

Line 19: You're trying to return the letter o. It should be zero (0).

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.