Thread: Two dimensional arrays

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    Two dimensional arrays

    Hi there guys, im new to the forum.

    Currently a first year programming student, and struggling on arrays as i missed a lecture. Here's what i need to do, and what ive done, before i hit my point of not knowing any more.

    Im trying to write a program that will read this data into a 2 dimensional array (data is 1,2,3,4,5,6,7,8,9) so, i've used this

    Code:
    int arr[3][3]={
                   {1,2,3},
                   {4,5,6},
                   {7,8,9}
                   };
    then ive used

    Code:
    int i,j;
    
    	for (i=0;i<3;++i)
       {
       	for(j=0;j<3;++j)
       	printf("%d",arr[i][j]);
          	printf("\n");
       }
    to make the program produce the data into a nicely formatted array

    1 2 3
    4 5 6
    7 8 9

    Which so far is correct, and working.
    Now i need to have the add the value's within the columns, and have them output a lower total, and a side total. What i mean is,


    Code:
    1   2   3| 6   
    4   5   6| 15
    7   8   9| 24
    ==========
    12  15  18
    Im stumped as to how i do this, and my text book only tells me so much, please, i hope you can help me!

    MANY THANKS,

    Masschino
    Last edited by Masschino; 05-18-2004 at 06:02 PM.

  2. #2
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    consider that a[i][j] in your code snippet is used to access each element of the array, and then consider that you can control the values of i and j to access any element you want.

    it's simple after that. for instance, the sum of the first row:

    sum = a[0][0] + a[0][1] + a[0][2];

    and the second:

    sum = a[1][0] + a[1][1] + a[1][2];

    and the third:

    sum = a[2][0] + a[2][1] + a[2][2];

    etc. write out the code to sum each row and column correctly - you can access any element with [row][row-element], or, if you prefer [column-element][column]. you can write loops to factor out similar code. for instance, the code to sum each row is the same in each case, except that the first index in the array is changed (by that i mean this a[i][j] <-> i is the first index, j is the second index).
    .sect signature

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    i understand what you're pointing out to me, many thanks.
    Didnt know i could do, say, sum=i[1][2] and etc.

    That said, im still struggling to find a way to put it all in correctly, where does everything go?

    ie

    Code:
    sum=i[1][1]+i[2][1]+i[3][1];
          	printf("\n%d",&sum);
    Had the above to work out the sum of 1,4,7 in the array, wont work . Invalid indirection apparently.
    Last edited by Masschino; 05-18-2004 at 06:18 PM.

  4. #4
    C > C++ duders ggs's Avatar
    Join Date
    Aug 2001
    Posts
    435
    in reply to above post:

    array access starts at 0, not 1. so the item at row 1, column 1 is:
    a[1][1], and row 1, column 2 is: a[1][2]. etc. you're reading elements which don't exit (ie a[1][3])
    ----

    fill out these functions:

    int sumrow(int row) { return sum; }
    int sumcolumn(int column) { return sum; }

    then, use them in the appropriate places in your display loop. f'rinstance, modify:

    printf("\n");

    to read:

    printf(" | %d\n", sumrow(i));
    .sect signature

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    6
    oh dear, now im confused
    Sorry, i dont understand Is there any other way you can explain it>?

  6. #6
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    think of your 2d array as a 3 x 3 matrix, then

    col1 etc
    \/

    [ 1, 2, 3 ] -> row 1
    [ 4, 5, 6 ] etc
    [ 7, 8, 9 ]

    the element 1 corresponds to [0,0] in your 2d array
    5 --> [1,1]

    So summing a row is like adding

    row 1 sum = [0,0] + [0,1] + [0,2]

    col 1 sum = [0,0] + [0,1] + [0,2]

    What ggs suggested was to define a function row_sum(int row) which takes a row number as input (say row 1) and adds accordingly the specific elements ([row,0] + [row,1] + [row,2])

    similar for col_sum()

    when u call printf , u can make a call to this function instead of manually adding each element.

    Have a go at what the others have suggested, and if you have problems post your code and we can point you in the right direction. If your are unsure of functions take a look at the FAQ.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    6
    ok, will have a go, thanks

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    6
    Allright! thanks guys i got it.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void main()
    {
    	int arr[3][3];
       int i,j,total,total2;
    
    
    	for (i=0;i<3;++i)
       {
       	for(j=0;j<3;++j)
          {
       	printf("Please enter a number: ");
          scanf("%d",&arr[i][j]);
          }
    	}
       printf("\n\n");
       for(i=0;i<3;++i)
       {
       	total=0;
          for(j=0;j<3;++j)
          {
          	total=total+arr[i][j];
             printf("%3d",arr[i][j]);
          }
       printf("%5d",total);
       printf("\n");
       }
        printf("===============\n");
        for(i=0;i<3;++i)
       {
       	total2=0;
          for(j=0;j<3;++j)
          {
          	total2=total2+arr[j][i];
    		}
       printf("%3d",total2);
       }
    	getch();
    
    }

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    6
    Oh, for anyone who searches this topic, this program takes a users input and stores them into an array which is [3] x [3], which equates to 9 separate inputs of numbers. Once the program has 9 numbers, the program will calculate the sum of the first row, second row, thirdrow, and display them. The same is done for the columns.

    Enjoy

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main()
    Big no-no. main returns an integer by definition. And you really should use spaces instead of tabs so that the formatting of your code isn't ruined when you post.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. passing two dimensional arrays
    By Nova_Collision in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2003, 01:47 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM