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

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    22

    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. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    783
    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. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    So try it with
    Code:
    int arr[5][2] = {
      // figure it out
    };
    and
    Code:
    int arr[2][5] = {
      // figure it out
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    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. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    Well no, because you need a nested for loop, and two subscripts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    I am new in c programming.can i get a example how to do this kind of declaration in this programming.
    Last edited by md_nayeem; 11-15-2017 at 02:31 PM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    I need an Example.I can not understand how to do that.

  10. #10
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    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. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    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. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,703
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Nov 2017
    Posts
    22
    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. #14
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    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.
    Last edited by jack jordan; 11-16-2017 at 01:09 AM.

  15. #15
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I can not write a value into an array of strings
    By kriukoff.vanya in forum C Programming
    Replies: 2
    Last Post: 08-21-2017, 09:53 AM
  2. Replies: 1
    Last Post: 01-11-2016, 07:37 AM
  3. Replies: 1
    Last Post: 11-28-2014, 07:13 AM
  4. Replies: 9
    Last Post: 09-09-2014, 08:23 AM
  5. Write array to to BMP
    By bnickerson in forum C Programming
    Replies: 11
    Last Post: 06-06-2013, 10:55 AM

Tags for this Thread