Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <iomanip>
- using namespace std;
- int main()
- {
- int array, *row;
- int n, m, i,j, ansi, maxx, maxy, minx, miny;
- bool ans = false;
- cout << "n= ";
- cin >> n;
- cout << "m= ";
- cin >> m;
- array = (int)malloc(n * sizeof(int*));
- row = (int*)malloc(m * sizeof(int));
- int max1 = -1000, max2 = -1000, min = 1000, sum = 0;
- for (i = 0; i < n; i++)
- {
- array[i] = (int*)malloc(m * sizeof(int));
- for (j = 0; j < m; j++) {
- array[i][j] = rand() % 200 - 100;
- cout << setw(5) << array[i][j];
- sum += array[i][j];
- if (array[i][j] > max1) {
- max2 = max1;
- max1 = array[i][j];
- maxx = i;
- maxy = j;
- }
- if (array[i][j] < min) {
- min = array[i][j];
- minx = i;
- miny = j;
- }
- }
- cout << endl;
- }
- for (i = 0; i < n; i++) {
- bool frst = false, sec = false;
- for (j = 0; j < m; j++) {
- if (array[i][j] == max1) {
- if (frst)
- sec = true;
- frst = true;
- }
- }
- if (frst && sec) {
- ans = true;
- ansi = i;
- }
- }
- cout << endl << "# 1" << endl << endl;
- cout << "Max value " << max1 << endl;
- if (ans) {
- cout << "Row with two max values " << ansi << endl;
- }
- else {
- cout << "No row with two max values" << endl;
- }
- cout << endl << "# 2" << endl << endl;
- cout << "SUM of all: " << sum << endl;
- cout << "Count of all: " << n * m << endl;
- cout << "AVG of all: " << (float)sum / n / m << endl;
- cout << "Min " << min << endl;
- cout << "Max " << max1 << endl;
- cout << "AVG of min and max " << (min + max1) / 2 << endl;
- cout << "Answer: " << (float)sum / n / m - (min + max1) / 2 << endl;
- cout << endl << "# 3" << endl << endl;
- int minrowsum = 1000000000000, maxrow = -1000, minrowi = 0;
- for (i = 0; i < n; i++) {
- int s = 0;
- for (j = 0; j < m; j++) {
- s += array[i][j];
- }
- if (s < minrowsum) {
- minrowsum = s;
- minrowi = i;
- }
- }
- for (j = 0; j < m; j++) {
- if (maxrow < array[minrowi][j])
- maxrow = array[minrowi][j];
- }
- cout << "Min sum row " << minrowi << endl;
- cout << "Max value in min sum row " << maxrow << endl;
- cout << endl << "# 4" << endl << endl;
- bool forth = false;
- for (j = 0; j < m; j++) {
- bool col = true;
- for (i = 1; i < n; i++) {
- if (array[i][j] == array[i - 1][j]) {
- col = true;
- break;
- }
- }
- if (col) {
- forth = true;
- break;
- }
- }
- if (forth){
- cout << "There is column with all equal values " << j << endl;
- } else {
- cout << "There is no column with all equal values" << endl;
- }
- cout << endl << "# 5" << endl << endl;
- ans = false;
- for (i = 0; i < n; i++) {
- int c = 0;
- for (j = 0; j < m; j++) {
- if (array[i][j] < 0) {
- c++;
- }
- }
- if (c == 2) {
- ans = true;
- break;
- }
- }
- if (ans) {
- cout << "There is row with 2 neg values " << i << endl;
- }
- else {
- cout << "There is no row with 2 neg values" << endl;
- }
- cout << endl << "# 6" << endl << endl;
- ans = false;
- for (i = 0; i < n; i++) {
- int n = 0, p = 0;
- for (j = 0; j < m; j++) {
- if (array[i][j] < 0) {
- n++;
- }
- if (array[i][j] > 0) {
- p++;
- }
- }
- if (p > n) {
- ans = true;
- break;
- }
- }
- if (ans) {
- cout << "There is row with more pos values " << i << endl;
- }
- else {
- cout << "There is no row with more pos values" << endl;
- }
- Адиль, [28.10.2022 13:21]
- cout << endl << "# 7" << endl << endl;
- int maxrowsum = -1000, maxi=0, mina = 1000;
- for (i = 0; i < n; i++) {
- int s = 0;
- for (j = 0; j < m; j++) {
- s += array[i][j];
- }
- if (s > maxrowsum) {
- maxrowsum = s;
- maxi = i;
- }
- }
- for (j = 0; j < m; j++) {
- if (mina> array[maxi][j])
- mina = array[maxi][j];
- }
- cout << "Max sum row " << maxi << endl;
- cout << "Min abs " << mina << endl;
- cout << endl << "# 8" << endl << endl;
- ans = false;
- for (j = 0; j < m; j++) {
- int n = 0, p = 0;
- for (i = 0; i < n; i++) {
- if (array[i][j] < 0) {
- n++;
- }
- if (array[i][j] > 0) {
- p++;
- }
- }
- if (p == n) {
- ans = true;
- break;
- }
- }
- if (ans) {
- cout << "There is column with equal pos and neg values " << j << endl;
- }
- else {
- cout << "There is no column with equal pos and neg values " << endl;
- }
- cout << endl << "# 9" << endl << endl;
- ans = false;
- cout << "Rows with only pos values: " << endl;
- for (i = 0; i < n; i++) {
- int p = 0;
- int maxvrow = -1000;
- for (j = 0; j < m; j++) {
- if (array[i][j] > 0) {
- p++;
- }
- if (maxvrow < array[i][j]) {
- maxvrow = array[i][j];
- }
- }
- if (p == m) {
- cout << "Row: " << i << endl;
- cout << "Max in this row: " << maxvrow << endl;
- ans = true;
- }
- }
- if (!ans) {
- cout << "No rows";
- }
- cout << endl << "# 10" << endl << endl;
- int sf = 0, sl = 0;
- for (i = 0; i < n; i++) {
- sf += array[i][0];
- sl += array[i][m-1];
- }
- int sfc = 0, slc = 0;
- for (j = 0; j < m; j++) {
- sfc += array[0][j];
- slc += array[n-1][j];
- }
- cout << "AVG of first row " << (float)sf / n << endl;
- cout << "AVG of last row " << (float)sl / n << endl;
- cout << "AVG of first column " << (float)sfc / m << endl;
- cout << "AVG of last column " << (float)slc / m << endl;
- cout << endl << "# 11" << endl << endl;
- int maxabs = max1;
- if (abs(maxabs) < abs(min))
- maxabs = min;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++) {
- cout << setw(10) << (float)array[i][j] / maxabs;
- }
- cout << endl;
- }
- cout << endl << "# 12" << endl << endl;
- int starti, startj, endi, endj;
- if (maxx < minx) {
- starti = maxx;
- startj = maxy;
- endi = minx;
- endj = miny;
- }
- else {
- endi = maxx;
- endj = maxy;
- starti = minx;
- startj = miny;
- }
- int s = 0;
- for (i = 0; i < n; i++) {
- for (j = 0; j < m; j++) {
- if ((i > starti && i < endi)
- (i == starti && j >= startj)
- (i == endi && j <= startj))
- s += array[i][j];
- }
- }
- cout << "Sum of elements that is between min and max value " << s << endl;
- cout << endl << "# 13" << endl << endl;
- int anses[1000], id=0;
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++) {
- int v = rand() % 100;
- cout << setw(5) << v;
- if (v == i * j) {
- anses[id++] = v;
- }
- }
- cout << endl;
- }
- for (i = 0; i < id; i++) {
- cout << anses[i] << " ";
- }
- cout << endl << "# 14" << endl << endl;
- int* sumcols, * sumrows;
- sumcols = (int*)malloc(m * sizeof(int));
- sumrows = (int*)malloc(n * sizeof(int));
- for (i = 0; i < m; i++) {
- sumcols[i] = 0;
- }
- for (i = 0; i < n; i++) {
- sumrows[i] = 0;
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++) {
- sumrows[i] += array[i][j];
- sumcols[j] += array[i][j];
- }
- }
- Адиль, [28.10.2022 13:21]
- int mincol=0, maxrowi=0;
- for (i = 0; i < m; i++) {
- if (sumcols[i] < sumcols[mincol])
- mincol = i;
- }
- for (i = 0; i < n; i++) {
- if (sumrows[i] > sumrows[maxrowi])
- maxrowi = i;
- }
- cout << "Max row " << maxrowi << endl;
- cout << "Min col " << mincol << endl;
- cout << endl << "# 15" << endl << endl;
- for (i = 0; i < n; i++)
- {
- int minrows = 0, maxrows = 0, temp;
- for (j = 0; j < m; j++) {
- if (array[i][maxrows] < array[i][j]) {
- maxrows = j;
- }
- }
- temp = array[i][maxrows];
- array[i][maxrows] = array[i][m-1];
- array[i][m-1] = temp;
- }
- for (i = 0; i < n; i++)
- {
- int minrows = 0, maxrows = 0, temp;
- for (j = 0; j < m; j++) {
- if (array[i][minrows] > array[i][j]) {
- minrows = j;
- }
- }
- temp = array[i][minrows];
- array[i][minrows] = array[i][0];
- array[i][0] = temp;
- }
- for (i = 0; i < n; i++)
- {
- for (j = 0; j < m; j++) {
- cout << setw(5) << array[i][j];
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement