1. ## 2d array question

hi all,

im new to programming in general and im probably asking something very simple here.....

well i have a 2d symmetric array where i search for a value and if found i want to give the value of zero to the whole row

here what i have managed until now:
Code:
```#include "stdafx.h"
#include<stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int array1[3][3]={{  0,113,303},
{113,  0,196},
{303,196,  0}};
int i;
int j;

for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
/* check if number 113 exists */

if(array1[i][j]==113){
/*if yes make it zero*/
array1[i][j]=0;

/*also, make the values for row i =0*/

}
printf("%2d ",array1[i][j]);

}

printf("\n");

}

scanf("%d",&i);

return 0;
}```

2. Replace this part:
Originally Posted by goofy26
Code:
```			if(array1[i][j]==113){
/*if yes make it zero*/
array1[i][j]=0;

/*also, make the values for row i =0*/

}```
with an inner for() loop:
Code:
```int ii;  /* at the top please! */

if(array1[i][j]==113){
for (ii=0;ii<3;ii++) array[i][ii]=0;
break;      /* might as well be done with this row */
}```

3. hi,

thanks for your reply, i have put your suggestion in but i still dont get zeros on the first row:

Code:
```#include "stdafx.h"
#include<stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int array1[3][3]={{  0,113,303},
{113,  0,196},
{303,196,  0}};
int i;
int j;
int ii;  /* at the top please! */

for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
/* check if number 113 exists */

if(array1[i][j]==113){
for (ii=0;ii<3;ii++){
array1[i][ii]=0;
break;
}/* might as well be done with this row */

/*also, make the values for row i =0*/

}
printf("%2d ",array1[i][j]);

}

printf("\n");

}

scanf("%d",&i);

return 0;
}```
the result is quite different now

its:

0 113 303
0 0 196
303 196 0

the result that im after is

0 0 0
0 0 196
303 196 0

4. I think the break is in the wrong place.

--
Mats

5. Okay, this:
Code:
```			if(array1[i][j]==113){
for (ii=0;ii<3;ii++) array[i][ii]=0;
break;
}```
and this
Code:
```                        if(array1[i][j]==113){
for (ii=0;ii<3;ii++){
array1[i][ii]=0;
break;
}
}```
ARE NOT THE SAME. You put the break inside the for(ii) loop.

ps. I think you want this output
0 0 0
0 0 0
303 196 0

and that's what you'll get.

6. I've put it like you said:

Code:
```#include "stdafx.h"
#include<stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int array1[3][3]={{  0,113,303},
{113,  0,196},
{303,196,  0}};
int i;
int j;
int ii;

for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
/* check if number 113 exists */

if(array1[i][j]==113){
for (ii=0;ii<3;ii++) array1[i][ii]=0;
break;

}
printf("%2d ",array1[i][j]);

}

printf("\n");

}

scanf("%d",&i);

return 0;
}```
and now i get:

0
303 196 0

(im not sure why it does give me all 3 rows now)

i think i didnt explain it very well when i first posted the question

where i check whether a number is equal to 113
i want to to set the line it first finds the number to zero

so in this 113 is in position 0,1 and 1,0

the first time it finds 113 is in position 0,1
so i want it to be able to set row's 0 values to zero
but not row's one

thx a lot for your help i really appreciate it

7. Sorry, you should take the break right out, I didn't take into account the flow with the printing.

There is a flaw in your code tho -- you start printing the row BEFORE you have checked it. So if there was a row with 113 in it with a non-zero value before it in the row (eg, 666, 113, 0) it will change the row after printing 666, so you will see 666 0 0.

You should print the array in a completely separate block or function after you parse (then you can put that break statement back in too).

i've put it like that:

Code:
```#include "stdafx.h"
#include<stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int array1[3][3]={{  0,113,303},
{113,  0,196},
{303,196,  0}};
int i;
int j;
int ii;

for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
/* check if number 113 exists */

if(array1[i][j]==113){
for (ii=0;ii<3;ii++) array1[i][ii]=0;
break;
}
}
}
for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
printf("%2d ",array1[i][j]);
}
printf("\n");
}
scanf("%d",&i);

return 0;
}```
and i get the result you mentioned earlier:
0 0 0
0 0 0
303 196 0

is there a way to get it not to set the whole second row to zero

9. i've added one more line and now i get the result i want:

(not sure if its the right way to do it though)

Code:
```#include "stdafx.h"
#include<stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
int array1[3][3]={{  0,113,303},
{113,  0,196},
{303,196,  0}};
int i;
int j;
int ii;

for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
/* check if number 113 exists */
if(array1[i][j]==113){
for (ii=0;ii<3;ii++) array1[i][ii]=0;
array1[j][i]=0;
break;

}

}
break;
}
for(i=0;i<=2;i++){

for (j=0;j<=2;j++){
printf("%2d ",array1[i][j]);
}
printf("\n");
}
scanf("%d",&i);

return 0;
}```

10. Just a note, that you did say this in the Original Post:

Originally Posted by goofy26
well i have a 2d symmetric array where i search for a value and if found i want to give the value of zero to the whole row
If you want to do something different to each row, you would have to make different rules for each row by using more if statements and/or conditions:
Code:
```for (j=0;j<=2;j++){
if((i==1) && (array1[i][j]==113)){
for (ii=0;ii<3;ii++) array1[i][ii]=0;
break;
}
else if((i==2) && (array1[i][j]==113)){
[only change two numbers]
}
...etc.```

}

11. ok, ill keep that in mind as i continue to change this further

thanks so much for your help