Hi All ...
I'm having a problem in the output of the last for loop ...
This is the program :
Code:
#include <iostream>
#include <iomanip>
#include <conio>
int main()
{
float A[2][3] ;
int i,j,key,row,col;
cout<< "\enter array elements:" << endl;
for ( i=0 ; i<2 ; i++ )
{ for ( j=0 ; j<3 ;j++ )
cin>> A[i][j];
}
cout<< "array elements are: \n" ;
for ( i=0 ; i<2 ; i++ )
{ for ( j=0 ; j<3 ;j++ )
{ cout<< setw(6) << A[i][j];
}
cout<< endl;
}
cout<< "enter the key you want to find : ";
cin>> key;
for ( i=0 ; i<2 ; i++ )
for ( j=0 ; j<3 ; j++ )
{ if ( A[i][j] == key )
{ row=i;
col=j;
cout<< key << " was found at row " << (row+1) <<" col "<< (col+1);
break;
}
else
{
cout<< key << " was not found" ;
break;
}
}
getch();
return 0;
}
The output is :
Code:
enter array elements:
1 2 3 4 5 6
array elements are:
1 2 3
4 5 6
enter the key you want 4
4 was not found4 was found at row 2 col 1
The problem is in the last loop .. if the value is found it will print its location and the other output if its not found ..
This is the output for unfound value :
Code:
enter array elements:
1 2 3 4 5 6
array elements are:
1 2 3
4 5 6
enter the key you want to find : 9
9 was not found9 was not found
How to fix it .. ??
Any help is Greatly Appreciated ..