Results 1 to 6 of 6

Thread: Program to print an 2D array

  1. #1
    Join Date
    Dec 2009
    Posts
    41

    Program to print an 2D array

    Hi friend,

    I am getting troubled while coding C program for 2D array. I have one program to print one dimensional array but not able to code program for two dimensional array. I have one program code for 2D array but it's not displaying the desired output. If you have C program to print an 2D array, then please share same program with me. Your help would be greatly appreciable.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Program to print an 2D array

    Hi,
    Please refer following program, which will show you how to print the 2D array:
    Code:
    #include<stdio.h>
    #include<conio.h>
    voizd main()
    {
    int a[50][50],k,row,column;
    printf("enter number of rows & columns of 2D array :");
    scanf("%d%d",&row,&column);
    for(int iz=0;iz<row;iz++)
    {
    for(int q=0;q<column;q++)
    {
    scanf("%d",&a[iz][q]);
    }
    }
    for(iz=0;iz<row;iz++)
    {
    k=0;
    for(q=0;q<column;q++)
    {
    k=k+a[iz][q];
    }
    printf("%d\n",k);
    }
    getch();
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Program to print an 2D array

    To print the 2D array, for this you need to use the nested loop. I think nested for loop is perfect for this situation. In which you need to use two for loops. Assign first first for loop to print the columns for the 2D array. While the assign second for loop to print the rows of the 2D array. See following:
    for(irr=0;ir<row;ir++)
    {
    kr=0;
    for(qr=0;qr<column;qr++)
    {
    kr=kr+ar[ir][qr];
    }
    printf("%d\n",kr);
    }

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Program to print an 2D array

    Hello friend,

    Printing two dimensional array is quit different from printing one dimensional array. Two loops are required to print the values of two dimensional array. One is for rows and another for to print column:
    strArray[][]=// put value for 2D array
    for(int iv=0;iv<strArray.length;iv++) {
    for(int jv=0;jv<strArray[iv].length;jv++) {
    System.out.print(strArray[i][j]);
    }
    System.out.println();
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Program to print an 2D array

    I recommend you use following program code in your program for to print the 2D array:
    Code:
    int AT[3][3];
    
    for(ibx=0;ibx<3;ibx++)
    
    {
    for(jbx=0;jbx<3;jbx++)
    {
    
    printf(" %d" AT[ibx][jbx]);
    }
    printf("\n");
    }

  6. #6
    Join Date
    Dec 2009
    Posts
    41

    Re: Program to print an 2D array

    Thanks friend for your help!!
    I have tried to run the above mentioned code for to print the two dimensional array. There are some miner syntax error in the program code. I corrected that error. All program codes was very easy to understand. It also helped me to understand the use of nested loop in C program. I think it's not very difficult to print the 2D array as it seems. I appreciating you help.

Similar Threads

  1. Program to print GCD of given numbers
    By Bhavesh_seth in forum Software Development
    Replies: 5
    Last Post: 15-03-2011, 08:03 PM
  2. Can't print from Dos program within Vista?
    By WarRen! in forum Vista Setup and Install
    Replies: 6
    Last Post: 03-03-2010, 05:08 AM
  3. Program to print odd and even numbers
    By roodiii in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 02:13 PM
  4. How to store space in a array in a C program
    By NetworkeR in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 12:41 PM
  5. Three Dimensional Array program in Java.
    By nonose in forum Software Development
    Replies: 3
    Last Post: 10-08-2009, 07:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,747,928.11957 seconds with 16 queries