# Thread: How can i write this program by using 2d array.

1. ## How can i write this program by using 2d array.

Code:

Code:
```#include <stdio.h>int main()
{
int arr[10]={1,2,3,4,5,6,7,8,9,10};  //  only for testing purpose
int i,j;
for (i=0;i<10;i++) {
int flag = 1;
if (arr[i] == 1) flag = 0;
for (j=2;j<=arr[i]/j;j++) {
if (arr[i]%j == 0) {
flag = 0;
break;
}
}
if (flag) printf("%d ",arr[i]);
}
return 0;
}```

2. What problem are you trying to solve? What's the algorithm? Once you figure those out, it should be easy to use a 2D array (unless you need help with the syntax for creating a 2D array, which you need to ask for if that's the problem you're having).

3. I am new in Array programming.So i was trying to write a program by using c programming.The Question is"storing 10 number into one/two dimensional array & Print Only Prime Number by using c programming Language ".I already solve this program by using one dimensional array.But Now i am trying to solve this program by using two dimentional array(2d array).How can i write this program by using 2d array.

4. So try it with
Code:
```int arr[5][2] = {
// figure it out
};```
and
Code:
```int arr[2][5] = {
// figure it out
};```

5. I tried like this but it did not worked:
Code:
```#include <stdio.h> int main()

int arr[2][5]={1,2,3,4,5},{6,7,8,9,10}};  //  only for testing purpose
int i,j;
for (i=0;i<10;i++) {
int flag = 1;
if (arr[i] == 1) flag = 0;
for (j=2;j<=arr[i]/j;j++) {
if (arr[i]%j == 0) {
flag = 0;
break;
}
}
if (flag) printf("%d ",arr[i]);
}
return 0;
}```

6. Well no, because you need a nested for loop, and two subscripts.

7. I am new in c programming.can i get a example how to do this kind of declaration in this programming.

8. You need to access each array element with
arr[row][col]

So create a for loop for row, and within that, create a for loop for col.

9. I need an Example.I can not understand how to do that.

10. I tried like this but it did not worked:
Code:
```#include <stdio.h>
main()
{
int i,j;
int arr[2][5]={{1,2,3,4,5},{6,7,8,9,10}};
for(i=0;i<10;i++){
int flag = 1;
if (arr[i] == 1) flag = 0;
{
for(j=2;j<=arr[i]/j;j++){
if (arr[i]%j == 0) {
flag = 0;
break;
}
}

if (flag)
printf("This is num:%d\n",arr[i][j]);

}
return 0;
}```

11. Again, you need two subscripts to access one number in arr. You are still using arr[i] in places and that will never be right again.

The reason is because arr[i] is only half the information... you access arr[i] and you get this.
Code:
```{1,2,3,4,5} // when i is 0
{6,7,8,9,10} // when i is 1```
I'll leave you to wonder what type those rows are (hint, it's a pointer) but for your purposes it doesn't matter. You just need to remember to specify which column as well.

12. > printf("This is num:%d\n",arr[i][j]);
Well it's a start.
Now make all your other arr references into arr[i][j]

Also, remove ALL that flag code until you understand the basics of looping.

13. I customize this code as your instruction.Now this program is valid or not.If i compile this code and run .It give me Error.
Code:
```#include <stdio.h>
main()
{
int i,j;
int arr[2][5]={{1,2,3,4,5},{6,7,8,9,10}};
for(i=0;i<10;i++){

if (arr[i][j] == 1)
{
for(j=2;j<=arr[i][j]/j;j++){
if (arr[i][j]%j == 0) {
break;
}
}

printf("This is num:%d \n",arr[i][j]);
}
return 0;
}
}```

14. Code:
`if (arr[i][j] == 1)`
i has been given a value at this point in your previous for loop, but j doesn't get a value until the next line. In order for arr[i][j] to be referenced, j must be known.

15. For a 2D array you need 2 nested loops. 1 for the rows, 1 for the columns. Here is a very simle demo:
Code:
```#include <stdio.h>

#define ROWS  2
#define COLS  5

int main()
{
int row, col;
int arr[ROWS][COLS] =
{
{ 1,2,3,4,5 },
{ 6,7,8,9,10 }
};

for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
printf("%2d ", arr[row][col]);
}
printf("\n");
}

return 0;
}```