For example, we have a random 5 x 5 array
1 5 9 4 2
6 4 3 7 9
9 4 6 2 5
2 7 8 5 9
4 1 9 7 7
In the above array we need to look at the first
row
and findmin
andmax
, And then change theircolumns
. So the min and max in the first row are 1 and 9 and the answer should be
9
51
4 2
3
46
7 9
6
49
2 5
8
72
5 9
9
14
7 7
The columns swapped are highlighted . I have tried to solve this problem withe the following code .
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
int array[5] = {0,0,0,0,0};
int max = array[0];
int min = array[0];
int indexOfMax = 0;
int indexOfMin = 0;
int n, m;
cout << "n = "; cin >> n;
cout << "m = "; cin >> m;
int **array = new int *[n]; // for the output of a random array
for(int i = 0; i < n; i++)
array[i] = new int [m];
srand((unsigned int)time(NULL));
for(int i = 0; i < n; i++){ // loop for the array
for(int j = 0; j < m; j++){
array[i][j] = rand() % 20;
cout << array[i][j] << " ";
if(array[i] > max) // finding the max in first row
{
max = array[i];
indexOfMax = i;
}
if(array[i] < min) // finding the min in the first row
{
min = indexOfMin;
indexOfMin = i;
}
cout << indexOfMin << " " << indexOfMax << endl;
}
}
cout << '\n';
}
return 0;
}
So, primarily I get this main.cpp:16:11: error: conflicting declaration ‘int** array’
. And then i would like to know how to swap the columns of min
and max
?
Aucun commentaire:
Enregistrer un commentaire