codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> #include <ctime> #include <fstream> #include <iomanip> #include <string> using namespace std; const int n = 5; const int m = 5; void show_arr(ostream & os, int arr[n][m]) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) os << setw(3) << arr[i][j]; os << '\n'; } } int main() { srand(unsigned (time(0))); ofstream fout; cout << "Enter the name of file: "; string name; getline(cin, name); fout.open(name.c_str()); int arr[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (rand() % 2) arr[i][j] = rand() % 6; else arr[i][j] = -(rand() % 6); show_arr(cout, arr); show_arr(fout, arr); /*for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << setw(3) << arr[i][j]; cout << '\n'; } cout << '\n';*/ int sum_odd(0); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (i % 2 || i == 1) if (j % 2 || j == 1) sum_odd += arr[i][j]; cout << "\nThe sum of odd numbers is " << sum_odd << '\n'; fout << "\nThe sum of odd numbers is " << sum_odd << '\n'; int first_i(0), first_j(0), last_i(0), last_j(0); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (arr[i][j] < 0) { first_i = i; first_j = j; i = n; j = m; } for (int i = n-1; i >= 0; i--) for (int j = m-1; j >= 0; j--) if (arr[i][j] < 0) { last_i = i; last_j = j; i = -1; j = -1; } int sum_between(0); for (int i = first_i; i < last_i + 1; i++) if (i == first_i) for (int j = first_j; j < m; j++) sum_between += arr[i][j]; else if (i == last_i) for (int j = 0; j < last_j + 1; j++) sum_between += arr[i][j]; else for (int j = 0; j < m; j++) sum_between += arr[i][j]; cout << "The sum between first and last negative elements is " << sum_between << "\n\n"; fout << "The sum between first and last negative elements is " << sum_between << "\n\n"; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (arr[i][j] < 0) { if (-(arr[i][j]) <= 1) arr[i][j] = 0; } else if (arr[i][j] <= 1) arr[i][j] = 0; show_arr(cout, arr); show_arr(fout, arr); /*for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << setw(3) << arr[i][j]; cout << '\n'; } cout << '\n';*/ fout.close(); }
Private
[
?
]
Run code
Submit