Average of Array

Back to General discussions forum

MitchelP     2017-07-10 04:22:03
User avatar

include <iostream>

include <fstream>

include <string>

include <sstream>

using namespace std;

int main() { ifstream input("input.txt"); ofstream output("output.txt"); string str; int num, sum = 0, div = 0; while(getline(input, str)){ string buf; istringstream iss(str); while(iss >> buf){ istringstream ss(buf); ss >> num; sum += num; div++; } output << sum/div << " "; sum = 0; div = 0; } return 0; }

That is the code I wrote and from what I can tell is will give the correct answer However the answers that I get a wrong (and not an off by one) and I think the checker for this question is broken Any suggestions?

Quandray     2017-07-10 06:26:54
User avatar

Hi,

If I try your code with the example input data, it gives the answer 3 3 10 0 when the expected answer is 4 15 1.

Here's a few things to look at.

The first line of input contains the number of test cases.

Each test case has a 0 marking the end. This should not be included in calculations.

Try to get your code to read from stdin and write to stdout. It makes it a lot easier to help you.

I can't see that your code does rounding as required by this problem.

Please login and solve 5 problems to be able to post at forum