Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <chrono>
- using namespace std;
- int* func(int** mas, int* N, int* M, int* sum, int* a, int* b, int* c, int* d) {
- for (int i = 0; i < *N; i++)
- {
- int sum = 0;
- for (int j = 0; j < *M; j++)
- {
- sum += mas[i][j];
- }
- if (*a < sum)
- {
- *a = sum;
- *b = i;
- }
- cout << endl;
- if (*c > sum)
- {
- *c = sum;
- *d = i;
- }
- cout << endl;
- cout << "Result " << i + 1 << " is " << sum;
- }
- return a,b,c,d;
- }
- int** swapping(int** mas, int* N, int* M, int* b, int* d) {
- for (int i = 0; i < *N; i++)
- {
- for (int j = 0; j < *M; j++) {
- swap(mas[*b][j], mas[*d][j]);
- }
- }
- return mas;
- }
- void out(int** mas, int* N, int* M) {
- for (int i = 0; i < *N; i++) {
- cout << " ";
- cout << endl;
- for (int j = 0; j < *M; j++)
- {
- cout << mas[i][j] << " ";
- }
- cout << " ";
- }
- }
- int** generate(int** mas, int* N, int* M) {
- for (int i = 0; i < *N; i++) {
- for (int j = 0; j < *M; j++) {
- mas[i][j] = rand() % 20 + (-9);
- }
- }
- return mas;
- }
- int main()
- {
- srand(time(NULL));
- int M;
- int N;
- int a = 0, b = 0, c = 0, d = 0;
- cout << "Input matrix NxM: ";
- cin >> N;
- cin >> M;
- auto start = chrono::high_resolution_clock::now();
- int* sum = new int[N];
- int** mas = new int* [N];
- for (int i = 0; i < N; i++) {
- mas[i] = new int[M];
- }
- generate(mas, &N, &M);
- out(mas, &N, &M);
- cout << endl;
- func(mas, &N,&M, sum, &a, &b, &c, &d);
- cout <<endl<<endl<<"Max sum is: "<< a<<endl <<"Row of max sum: "<< b+1<<endl<<"Min sum is: " << c <<endl<<"Row of min sum is: "<< d+1;
- cout << endl;
- swapping(mas, &N, &M, &b, &d);
- out(mas, &N, &M);
- for (int i = 0; i < M; i++) {
- delete [] mas[i];
- }
- delete [] mas;
- auto end = chrono::high_resolution_clock::now();
- chrono::duration<float> duration = end - start;
- cout << endl<<endl;
- cout << "Time is " << duration.count() << endl << endl << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement