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;
}
Its return 0, NOT return o.
Also, you need to initialize sum and count to 0 before adding to them.
Lastly, your -1 is being added to your sum, and count is being incremented when -1 is entered. Trace your code to see why this is happening. What will you do to fix this?