C code is quite hackable, isn't it?
One wrong move and a black hole sprouts before you!
C code is quite hackable, isn't it?
One wrong move and a black hole sprouts before you!
Devoted my life to programming...
it doesn't matter how I do it, the 'i' counter never stops. Even if I moved the if statement after the following bracket, it will just make it worse. When I compile and then run it it gives me extra values (0's in this case). For example I set 'j' =2. Then I enter 4 values, 1 through 4. It would give me 1,2, 3, 4, 0, 0, 0, 0, 0,0Code:matrix[10][10]={0}; //initial declaration of my matrix int x = 'x'; for(i = 0; i <10; i++){ for(j = 0; j<cols; j++){ scanf("%d", &matrix[i][j]); if(matrix[i][j] == x) break; } }
So true, so true...
Code:int main(void) { const size_t rows = 4, cols = 3; int mat[rows][cols] = {{0}}; int* mb = ( int*)mat, * me = mb + sizeof(mat) / sizeof(int); while(mb != me) if(scanf("%d", mb++) != 1) break; for(size_t r = 0; r < rows; ++r) for(size_t c = 0; c < cols; ++c) printf("mat[%d][%d]: %d\n", r, c, mat[r][c]); }
Well, nope. If I were waiting just for the code I would have just asked for it since the beginning. But that's no the point if I won't learn programming >_> newbie
Yes I have, I tried putting the break within just the first for loop and it didn't work. I tried testing for the output/input but I didn't get it right either. I got rid of the if(isalpha()) because like said before, I don't need it. I know that for loops are intended to go through a number of iterations. Just a question to get out of the doubt. Would a while loop be better in this case?
ok, the problem is stopping the 'i' from going all the way to ten. I tried breaks in different places. If I put it within just the 'i' for loop, it won't let me enter enough numbers.
I put a printf statement to print 'i'. If I do the following, this happens
when run with gcc,Code:for(i = 0; i < 10; i++){ for(j = 0; j<cols; j++){ scanf("%d", &matrix[i][j]); } if(matrix[i][j] != 1) break; }this what happens when I put the break beforeCode:enter the number of columns you want your matrix to be the number can't exceed 10: 2 you can enter up to 20 items: please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter enter a non numeric value to stop entering values 1 2 i = 0 ***this is your matrix*** (doesn't display anything)Code:enter the number of columns you want your matrix to be the number can't exceed 10: 2 you can enter up to 20 items: please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter enter a non numeric value to stop entering values 1 2 3 4 x i = 10 ***this is your matrix*** 1 2 3 0 4 0 0 0 0 0
@torquemada...
Did you even try the suggestion of making it into a function and using return to exit both loops?
I tried it but the problem is that it won't let me enter numbers pass 2. It might not be the best implementation.
outputCode:for(i = 0; i < 10; i++){ for(j = 0; j<cols; j++){ scanf("%d", &matrix[i][j]); if(matrix[i][j] != 1) i = 10; } if(i >= 10) break; }Code:enter the number of columns you want your matrix to be the number can't exceed 10: 2 you can enter up to 20 items: please enter the numbers you want in your matrix, enter each number by dividing them with spaces or by pressing enter enter a non numeric value to stop entering values 1 2 (it won't let me enter more numbers) ***this is your matrix*** 1 2 0 0 0 0 0 0 0 0
Nonono... you mixed it all up! Stop, rewind and go back to your first code posted. There apply only one of the methods we told you.
EDIT: I just noticed: Don't check the matrix's value, check the return value of scanf. If it's zero, then do something to escape from the loops.
Last edited by GReaper; 09-18-2011 at 11:56 AM.
Devoted my life to programming...
Sorry for the late reply.
I just wanted to say thanks to every one for their time
I figured something out
Instead of letting 'i' be my counter I created another variable "counter" to count 'i' but it wouldn't go higher once the break is reached in the loop. So I stored that value and used it as a counter. It might not sound like the best idea to you but it works for me.
Thanks again