C++ Input/output -
#include <iostream> #include <fstream> using namespace std; int main() { int , b , c , , n; int d = 0; ifstream myfile; myfile.open("duomenys1.txt"); myfile >> n; (int = 0; < n; i++ ) { myfile >> >> b >> c; d += (a + b + c)/3 ; } ofstream myotherfile; myotherfile.open ("rezultatai1.txt"); myotherfile << d; myotherfile.close(); myotherfile.close(); return 0; }
the programs should read 3 (3 n) rows of numbers (5 7 4 ; 9 9 8; 8 7 8), rows summed separately , given 3 different averages (7 ; 9 ; 8) in rezultatai1.txt file. -2143899376 result.
the problem isn't huge number, need program give every row's average number separately in output file, in output file written (7 ; 9 ; 8)
you must make 1 output per line , must use floating point arithmetic followed rounding if want rounded averages.
#include <iostream> #include <iostream> #include <cmath> int main() { const int numbers_per_lines = 3; std::ofstream output("rezultatai1.txt"); std::ifstream input("duomenys1.txt"); int number_of_lines; input >> number_of_lines; for(int i=0; i<number_of_lines; ++i) { double sum=0; for(int num=0; num<numbers_per_line; ++num) { double x; input >> x; sum += x; } output << << ' ' << std::round(sum/numbers_per_line) << std::endl; } }
Comments
Post a Comment