Thread: Need help working with an array

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    31

    Question Need help working with an array

    Hi all, I'm having trouble working with an array, and I was wondering if anyone could explain it to me. What I want to do is have a 5x5 array, entered one row at a time, that will add each row and each column.

    For example:

    Enter row 1: 1 2 3 4 5
    Enter row 2: 6 7 8 9 0

    Row totals: 15 30
    Column totals: 7 9 11 13 5

    And so on. I'll post the code I have, but it's pretty basic stuff since I don't know where to go with it. Any help would be greatly appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    int main()
    {
        int a[N][N], i, rowSum, colSum;
    
        printf("Enter %d numbers: ", N);
        for (i = 0; i < N; i++)
            scanf("%d", &a[i]);
       
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    a[N][N] has N*N numbers.
    Use two nested for loops, so you have something like
    scanf( "&#37;d", &a[row][col] );
    in the innermost loop.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Is it possible to use a single dimensional array? It seems that it might be easier to use a one dimensional array because it's easier to add the rows that way, but would it possible to add up the columns that way? Arrays always give me trouble, so I am thoroughly confused about this. =/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Show some code using two loops like I suggest.
    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 C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, there are some methods to use one array. Actually, it will be better using 1D array. But, arrays are ESSENTIAL. You should learn to use them. So I ll post how this could be done with 1D array after you do this with a 2D array.

    First of all do what Salem posted to read a 2D array. I ll give you an example of 2 loops and an array.
    Code:
    for (i=0; i<N; ++i) {
       printf("\n");
       for (j=0; j<N; ++j)
          printf("%d ", a[i][j];
    }
    this will print everything in the 2D array a. Do you understand how the above code works? If not post and we will explain...

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I understand how the code works, but now that I have that code, how do I allow the user to enter the values of the array so I can add up the rows and columns?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    int main()
    {
        int a[N][N] = {0};
        int i, j, rowSum, colSum;
    
        for (i = 0; i < N; ++i)
        {
            printf("\n");
            for (j = 0; j < N; ++j)
            printf("&#37;d ", a[i][j]);
        }
    
        return 0;
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Adding up rows is easy, you just use the same code.

    For columns, index using [j][i] and not [i][j]
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Use the same code? How would I do that? If I can get the row adding part sorted out, I could figure out the column part without too much difficulty.

    I'm still having trouble with entering the values of the array though...what would I write so the user could just enter all 25 values at once instead of 5 at a time? It seems that doing it that way might be a bit easier, but I'm having trouble figuring out how to get it to work.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    for ( i = 0 ; i < N ; i++ ) {
      for ( j = 0 ; j < N ; j++ ) {
        scanf( "&#37;d", &A[i][j] );
      }
    }
    Whether the user types
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5
    1 2 3 4 5

    OR
    1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

    OR
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    1 2 3
    1

    doesn't make a blind bit of difference to the for loops.
    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.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    That's great, but I still don't know how to actually allow the user to enter the values they want. How do I do that? I've read the chapter in my book about arrays a thousand times and I still can't seem to figure it out. =/

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by DCMann2 View Post
    That's great, but I still don't know how to actually allow the user to enter the values they want. How do I do that? I've read the chapter in my book about arrays a thousand times and I still can't seem to figure it out. =/
    You can use scanf(), just like it was shown to you just above this post.

    You need to crawl before you can walk, and you need to learn how to use scanf(), before you start writing a program that requires the user to input values.

    Go back to the earlier chapters in your book, and learn the basics, THEN come back and resume your study and practice with arrays.

    There are many wrinkles with scanf() that are a PITA to deal with. Take your time and learn it well.

    Later, I know you'll want to use fgets() instead, but that's later - not now.

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Alright, I've done some more work on this since I last posted, but it's not working right. I'm pretty sure my rowSum and colSum functions are wrong, but at this point I'm mostly concerned about getting the matrix to work properly. Again, any and all help is greatly appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DIM 5
    
    int main()
    {
        int mat[DIM][DIM] = {0}, row, col, rowSum, colSum;
    
        printf("Enter 25 numbers: ");
        for (row = 0; row < DIM; row++)
            for (col = 0; col < DIM; col++)
                scanf("&#37;d", &mat[row][col]);
    
        rowSum += mat[row];
        colSum += mat[row];
    
        printf("The sum of the rows is: %d", rowSum);
        printf("\n");
        printf("The sum of the colums is: %d", colSum);
    
        return 0;
    }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    mat[row] cannot possibly be what you want. All elements of mat have two subscripts behind them, from [0][0] to [4][4].

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I know it's not what I want, but can you explain why the matrix isn't working properly? I tried doing a printf of the matrix, but all it gives me is one big number that doesn't contain a single element of the array. What am I doing wrong?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't try to print the matrix in the code there. But if you did, again, you can't just type "mat" and expect people (and your compiler!) not to laugh at you. If you want to print an element of the matrix, you must specify which one by using two subscripts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 5
    Last Post: 12-10-2007, 11:36 PM
  3. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  4. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 08:03 AM