Help with flawed algorithm?
Hey mates,
I'm writing a program I was told to, where you enter a matrix (rows and columns), and it needs to calculate it like this:
1 2 3
+ + +
1 1 1
solution is: 2 3 4
Now, notice that for this matrix, the number of rows entered was 2! and columns 3.
So the operands are like an extra row that the user enters. The number of rows refers the one that contain integers.
My algorith works perfectly except for one little flaw which I can't seem to debug - if you enter for rows "2", it won't stop untill you feed in 3 rows. I've been through my algorith and I can't see why...
I'm adding the input loop if you guys can spot it:
Help will be much appreciated!
Code:
printf("Enter the number of rows and columns:\n");
scanf("%d %d", &row, &column);
fail=0;
while( row<1 || row > 99 || column > 99 || column < 1)
{
printf("Enter a valid number of rows and columns:\n");
scanf("%d, %d", &row, &column);
}
row+= (row-1);
int matrix[row][column];
printf("Enter the matrix: \n");
i=0;
j=0;
fail=0;
while (i< (row)) //Input the matrix!!!
{
j=0;
while (j<column)
{
if ((i%2)==0)
{
scanf("%d ", &input);
matrix[i][j] = input;
if (input==' ')
{
continue;
}
if (input=='\n')
{
if (j==(column))
{
printf("\n");
break;
}
}
}
if ((i%2)!=0)
{
oper=getchar();
if (oper==' ')
{
continue;
}
if (oper=='-' || oper=='+' || oper=='/' || oper=='*')
{
matrix[i][j] = oper;
}
else
{
fail=1;
}
}
j+=1;
}
i+=1;
}