I have this problem.... when running this code , I get strange results.. unexpected ones... I cant seam to figure this one out ( I am still learning C++ )[actualy just started out]
Code:
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
void main()
{
int m=4 , n=4;
int a[m][n] , min , max , b[m][2] ,c[n][2];
int r, k ,i ,j , mini=0, minj=0, maxi=0, maxj=0;
do
{
cout<<"Do you want a random matrix [0/1]?";
cin>>r;
} while ((r!=0)&&(r!=1));
cout<<endl<<endl;
if (r==0) { //manual matrix
for (i=0;i<m;i++){
for(j=0;j<n;j++){
cout<<"enter element "<<i<<" "<<j<<endl;
cin>>a[i][j];
cout<<endl;
}}}
else { //PC generated
for ( i = 0 ; i < m ; i++ ){
for( j = 0 ; j < n ; j++ ){
a[i][j]= rand()/1000;
}}}
//find max...
for (j=0;j<n;j++){
max = a[0][j]; //take 1st elem of row....
for ( i = 0 ; i < m ; i++ ){
if (max < a[j][i]){
max = a[j][i];
//set a list of maximums in matrix b
b[j][0] = max;
b[j][1] = i;
}
}
}
//find min...
for (i=0;i<m;i++){
min = a[i][0]; //take 1st elem of col....
for ( j = 0 ; j < n ; j++ ){
if (min > a[j][i] ){
min = a[j][i];
//set a list of minimums in matrix c
c[i][0]= min;
c[i][1]= j;
}
}
}
for (i = 0 ; i < m ; i++)
{
for(j=0;j<n;j++){
cout<<a[i][j]<<"\t";
}
cout<<endl<<endl;
}
cout<<"\t"<<"Elem"<<"\t"<<"col"<<"\t"<<"row"<<endl; //table headings
cout<<"Max [b]---------------------"<<endl;
for ( k = 0 ; k < m ; k++){
cout<<"\t"<<b[k][0]<<"\t"<<(k+1)<<"\t"<<(b[k][1]+1)<<endl;
}
cout<<"Min [c]---------------------"<<endl;
for (j=0;j<n;j++){
cout<<"\t"<<c[j][0]<<"\t"<<c[j][1]+1<<"\t"<<(j+1)<<endl;
}
}