I've been writing a program conducting some operations on two square matrixes. For the time being I've been thinking of a code which will read a matrix of a fixed (previously known size) and I'm writing these data into a 2-D array. However, I've got a problem, because when I'm debugging my code with addictional output messages everything seems fine, but the final output (the one in the for loop) I'm missing some numbers. It is really strange because when I'm prining all variables used in the process their values look fine.
#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
int number = 0;
int index = 0;
int v_ind = 0; // vertical index
int h_ind = 0; // horizontal index
char c;
int size = 3; // temporary fixed size
int searched_number;
int matrix1 [size-1][size-1];
int matrix2 [size-1][size-1];
//scanf("%i %i", &size, &searched_number);
while (index < size)
{
c = getchar_unlocked();
if ( (c >= '0') && (c <= '9') )
{
number = (number * 10) + (c - '0');
continue;
}
if (c == ' ')
{
cout << "number on a space: " << number << endl;
matrix1[h_ind][v_ind] = number;
cout << "1 ) matrix1[" << h_ind << "][" << v_ind << "] : " << matrix1[h_ind][v_ind] << endl << endl;
v_ind ++ ;
number = 0;
continue;
}
if (c == '\n')
{
cout << "num on a newLine: " << number << endl;
matrix1[h_ind][v_ind] = number;
cout << "2) matrix1[" << h_ind << "][" << v_ind << "] : " << matrix1[h_ind][v_ind] << endl << endl;
h_ind ++ ;
v_ind = 0;
number = 0;
index ++ ;
continue;
}
}
for (int i = 0; i < size; i ++) {
for (int j = 0; j < size; j ++) {
int num = matrix1[i][j];
cout << "mat[" <<i <<"][" << j << "] : " << num << " " << endl;
}
}
}
Below I've pasted an exemplary output from Ideone.com of a matrix like this:
| 1 2 3 |
| 4 5 6 |
| 7 8 9 |
Sukces time: 0 memory: 3348 signal:0
number on space: 1
1 ) matrix1[0][0] : 1
number on space: 2
1 ) matrix1[0][1] : 2
num na newLine: 3
2) matrix1[0][2] : 3
number on space: 4
1 ) matrix1[1][0] : 4
number on space: 5
1 ) matrix1[1][1] : 5
num na newLine: 6
2) matrix1[1][2] : 6
number on space: 7
1 ) matrix1[2][0] : 7
number on space: 8
1 ) matrix1[2][1] : 8
num na newLine: 9
2) matrix1[2][2] : 9
mat[0][0] : 1
mat[0][1] : 2
mat[0][2] : 4
mat[1][0] : 4
mat[1][1] : 5
mat[1][2] : 7
mat[2][0] : 7
mat[2][1] : 8
mat[2][2] : 9
The problem looks simple - I'm missing all last numbers from every row, except from the last one. I suspect that somewhere I overwrite proper values but I've got no clue where.