Skip to content

Instantly share code, notes, and snippets.

@lexab
Created September 23, 2018 20:49
Show Gist options
  • Save lexab/d3bf6742e595df6da424fd690c11f660 to your computer and use it in GitHub Desktop.
Save lexab/d3bf6742e595df6da424fd690c11f660 to your computer and use it in GitHub Desktop.
Example for my son
#include <iostream>
#include <fstream>
#include <valarray>
/*
This code read matrix like this:
4 5
1 2 3 4 5
6 12 8 9 10
11 12 12 14 15
16 17 18 12 20
from file, calc summ and write output to file and stdio
*/
int main() {
size_t rows,cols;
std::ifstream fin("first.txt");
std::ofstream fout("end.txt");
fin >>rows >>cols;
// std::cout << "\t Debug: Size: ROW=" <<rows << " COL="<<cols << "\n";
// using dynamic numeric array;
std::valarray<int>va(rows*cols);
size_t i=0;
while (!fin.eof())
fin >> va[i++];
/* more fit for lesson %-)
for(size_t _r=0; _r<rows;_r++) {
for (size_t _c = 0; _c <cols; _c++) {
fin >> va[_r*cols+_c];
}
}
*/
std::cout << "Summ: " << va.sum() <<"\n"; // min or max also works
fout << va.sum();
fin.close();
fout.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment