Need help working with an array

This is a discussion on Need help working with an array within the C Programming forums, part of the General Programming Boards category; I wanted it to print the whole matrix, though, not just a single element. I tried doing printf("%d", mat[row][col]), but ...

  1. #16
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    I wanted it to print the whole matrix, though, not just a single element. I tried doing printf("%d", mat[row][col]), but that didn't work...it gave me that big number I was telling you about earlier. Is this wrong? If so, what would be the correct way to print the whole matrix?

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Wait -- did you print out all 25 numbers without any spaces between them?

  3. #18
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    No, I'd enter them with spaces between them, like 1 2 3 4 5 etc.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So? Show the actual code that you used to print them out.

  5. #20
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    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]);
    
        printf("\n%d\n", mat[row][col]);
    
        rowSum += mat[row];
        colSum += mat[col];
    
        printf("The sum of the rows is: %d", rowSum);
        printf("\n");
        printf("The sum of the colums is: %d", colSum);
    
        return 0;
    }
    Here's what happens when I run the program:

    Enter 25 numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    25

    2621304
    The sum of the rows is: -594119456
    The sum of the colums is: 13106020
    Process returned 0 (0x0) execution time : 14.185 s
    Press any key to continue.

    2621304 is supposed to be the matrix. Ignore the sums; I know they're wrong.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    At the end of the for loop, both row and col have been incremented past legal subscripts. You have to put the print statement inside a double-for loop, because it's not as though going through an array and printing it differs from going through an array and reading it in in any way whatsoever.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Like I explained a week ago, you have a pair of for loops to read the data in.

    Now use a pair of for loops to scan all the elements of the array. Then figure out at which point where
    sumOfSomething[row] += mat[row][col];
    would be a good place.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #23
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Take a look at this, it may help you out a bit

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define N 5
    
    int main(void)
    {
     int i,j,k=1,row_tot,col_tot;
     int a[N][N];
     //initialising the matrix
    
     for(i=0;i<N;i++)
    	for(j=0;j<N;j++)
    		a[i][j]=k++;
     //printing matrix and calculating row totals
     for(i=0;i<N;i++)
     {
    	row_tot=0;
    	for(j=0;j<N;j++)
    	{
    		printf("&#37;3d ",a[i][j]);
    		row_tot+=a[i][j];
    	}
    	printf("|| %d ",row_tot);
    	printf("\n\n");
     }
     printf("==========================\n");
     for(i=0;i<N;i++)
     {
    	col_tot=0;
    	for(j=0;j<N;j++)
    		col_tot+=a[j][i];
    	printf("%3d ",col_tot);
     }
     printf("\n\n");
     system("PAUSE");
     return 0;
    }
    Last edited by chottachatri; 10-29-2008 at 05:38 AM.

  9. #24
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    s/bit/totally
    You could have at least left a bit of mystery for the OP to work on.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #25
    Registered User
    Join Date
    Apr 2008
    Posts
    31
    Okay, a put a printf in the nested for loop, but instead of printing the 5x5 matrix, it just prints all 25 numbers right in a row. How do I fix this?

    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]);
                    printf("%d ", mat[row][col]);
                }
        }
    
        rowSum += mat[row][col];
        colSum += mat[row][col];
    
        printf("The sum of the rows is: %d", rowSum);
        printf("\n");
        printf("The sum of the colums is: %d", colSum);
    
        return 0;
    }

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want a new line, you have to print it.

Page 2 of 2 FirstFirst 12
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, 10:36 PM
  3. Replies: 6
    Last Post: 11-09-2006, 02: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

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