My program asks for numbers and places them in a 2D array.
Then it asks for a number and performs a search, after which it prints position for said number within the 2D array.
Search function has a bug that adds 1 to expected row position. "Fixed" it by substracting 1 to "row" variable, but it is ovb not acceptable.
Currently feeling kind of dumb about this, help will be appreciated
Code:
#include <iostream>
using namespace std;
#define R 3
#define C 2
typedef int matrix[R][C];
void input(matrix m){
int i, j;
i = j = 0;
for(i = 0; i< R; i++){
for(j = 0; j < C; j++){
cout << "Row " << i << " Column " << j << ": ";
cin >> m[i][j];
}
}
}
void search(int x, matrix m, int& row, int& column){
int r, c;
bool OK;
OK = false;
r = 0;
while((!OK) && (r < R))
{
c = 0;
while((!OK) && (c < C))
{
if(m[r][c] == x)
OK = true;
else
c++;
}
r++;
}
column = c;
row = r-1; //LOL
}
int main(){
matrix mat;
int number;
int row;
int column;
input(mat);
cout << "Which number are you looking for?: ";
cin >> number;
search(number, mat, row, column);
cout << endl << "Row " << row << " Column: " << column;
fflush stdin;
getchar();
return 0;
}