Print a 2D Array

This is a discussion on Print a 2D Array within the C Programming forums, part of the General Programming Boards category; How do I print a 2D array. Code: #include <stdio.h> #include <conio.h> #include <iostream> #include <ctype.h> int main() { int ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Print a 2D Array

    How do I print a 2D array.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    #include <ctype.h>
    
    int main()
    {
        int array [5][5]={  {2,4,6,8,10},
            {12,14,16,18,20},
            {22,24,26,28,30},
            {32,34,36,38,40},
            {42,44,46,48,50}
        };
    
        int i,j,temp[5];
    
    //print array
        printf("%d",array[5][5]);
    
    return 0;
    }
    also how do I sum all the value of the array?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    There isn't a standard function to "print an array", in the way you want. What you will have to do is loop over each row, then loop over each column--implying a for loop within a for loop--and in each iteration, you print the element of the array, corresponding to the current row and column.

    For summing all values in the array, of course you need some variable that stores the sum (probably initialized to 0). I imagine you can figure out how to do it, after understanding how to print it first. Work on printing it above, making sure to understand what you are doing (versus just writing arbitrary lines of code), then make an attempt at summing all values. If you have a problem, post your complete, updated code, as well as describe the problem or tell us the error/warnings you're getting.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    How do I get the inverse of col1?

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <iostream>
    #include <ctype.h>
    
    int printArray(int pary[][5]);
    int manipulateArray(int mary[][5]);
    
    int main()
    {
    //Initialize 2D Array
        int array[5][5] =
        {
            {2,4,6,8,10},
            {12,14,16,18,20},
            {22,24,26,28,30},
            {32,34,36,38,40},
            {42,44,46,48,50}
        };
        int reverse;
    //Print Array
        printArray(array);
        printf("\n\n");
    
    //Reverse the first column
    //Print the array on the screen
        manipulateArray(array);
    
    
    //Swap the contents of column 3 and column 4
    //Print the array on the screen
    
    //Sort row 5 in descending order
    
    //Sum the contents of the entire array
    
        return 0;
    }
    
    
    int printArray(int pary[][5])
    {
        int r,c; //row and col counter
    
        for (r = 0; r <= 4; r++)
        {
            for (c = 0; c <=4; c++)
            {
                printf("\t%d", pary[r][c]);
            }
            printf("\n");
        }
    }
    
    
    int manipulateArray(int mary[5][5])
    {
    
        int reverse[5]; //store temp value
        int r,c; //row and col counter
    
        for (r=4;r<=0;r++)
        {
            for (c<=4;c=0;c++)
            {
                reverse[5]=mary[0][c];
            }
                    printf("\t%d",mary[r][c]);
        }
        printf("\n");
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Invert it. You know... backwards.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-25-2010, 03:20 PM
  2. reading from a 2d array
    By roaan in forum C Programming
    Replies: 5
    Last Post: 09-03-2009, 11:42 PM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 08:19 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21