Thread: multidimension array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    2

    multidimension array

    Hey guys,
    This is actually a easy problem; I'm just having problems because i just started using it and knowing the concept of arrays. Anyways the program wants to defines a two dimensional array of size [4][4] initialized to zero; for the first row add 5 to each element and for the third row add 2 and multiple by 3, then for the second column add 2 to each element, print out the array, then find the sum of one of the diagonalize.

    This what i have so far:
    Code:
    #include<stdio.h>
    
    int main () {
    
    int my[4][4];
    int i,j;
    
    for(i = 0; i < 4; i++)
    my[0][i] = 5;
    
    for(i = 0; i < 4; i++) 
    my [2][i] = 2;
    my[2][i] = my[2][i] * 3;
    
    
    for(i = 0; i < 4; i++){
    for(j = 0; j < 4; j++)
    printf("%d", my[i][j]);
    printf("\n");
    }
    printf("\n\n");
    
    return 0;
    }
    Any help would be appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're off to a good start. Proper indentation would highlight at least one problem. Your second for loop needs a set of curly braces around the two statements. Some more problems:

    • You never initialize my to all zeros, so some of the elements have garbage in them
    • Technically, you aren't adding 5 or 2 to the elements. Try my[0][i] += 5;, my[2][i] +=2; and my[2][i] *= 3;
    • You don't add 2 to the elements of the second column.
    • You never calculate the sum of a diagonal. The elements of the main diagonal are where the row and column have the same index.
    • You should print your matrix with some space between the numbers, so it's easier to read. Try "%4d" instead of "%d" on line 18.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    2
    Yea that helped me get the result; Thanks alot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use pointer for multidimension array
    By hqt in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2011, 09:43 AM
  2. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  3. Replies: 5
    Last Post: 09-21-2009, 11:48 AM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM