PDA

View Full Version : Two dimensional array - Have to print out smallest number



Dreadfear
08-08-2012, 02:50 PM
Pay attention to line: 33. Script is supposed to print out smallest number in two dimensional array. It doesnt work - how can i fix it?



#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int i, j, min = 0, ri = 0, ki = 0, m[4][4];
double s = 0;
bool highest;

srand ( time(NULL) );

for (i=0; i<4; i++){
for (j=0; j<4; j++)
m[i][j] = (rand() % 50 + 1);
}

cout <<"Elements:" <<endl;

for (i=0; i<4; i++){
for (j=0; j<4; j++)
cout<< m[i][j] <<" ";
cout <<endl;
}

if (highest == true){
min = 51;
}


for (i=0; i<4; i++){
for (j=0; j<4; j++)
if((min < m[i][j]) == highest){
min = m[i][j];
ri = i;
ki = j;
}
}

cout << "Smallest number " <<min <<" and its on " <<ri <<" row and " <<ki <<" column" <<endl;

for (j=0; j<4; j++)
s = (s + m[0][j]);
cout << "0. row avarage number " << s/4 <<endl;
system ("pause");
return 0;

}

masterBB
08-08-2012, 02:56 PM
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main ()
{
int i, j, min = 0, ri = 0, ki = 0, m[4][4];
double s = 0;
bool highest = false;

srand ( time(NULL) );

for (i=0; i<4; i++){
for (j=0; j<4; j++)
m[i][j] = (rand() % 50 + 1);
}

cout <<"Elements:" <<endl;

for (i=0; i<4; i++){
for (j=0; j<4; j++)
cout<< m[i][j] <<" ";
cout <<endl;
}

if (highest == false){ //changed to false.
min = 51;
}


for (i=0; i<4; i++){
for (j=0; j<4; j++)
if((min < m[i][j]) == highest){
min = m[i][j];
ri = i;
ki = j;
}
}

cout << "Smallest number " <<min <<" and its on " <<ri <<" row and " <<ki <<" column" <<endl;

for (j=0; j<4; j++)
s = (s + m[0][j]);
cout << "0. row avarage number " << s/4 <<endl;
system ("pause");
return 0;

}



In my head it should work like this.

Dreadfear
08-08-2012, 02:59 PM
Yes works thank you