Thread: Addition of the rows and columns in an array.

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    14

    Addition of the rows and columns in an array.

    Ya, I'm still a newb...it's probly because my teacher has an extremely heavy accent and my book sucks, BUT I still try. Now my problem is trying to make a 5x5 array that adds the rows and columns. like

    0 1 2 3 4 5 blah blah, but I need the user to enter the 10 numbers. so far i have
    1 2 3 4 5 6
    2 3 4 5 6 7
    3 4 5 6 7 8
    4 5
    5
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int x, y, i;
    int A[5][5];
    float sum;
    
    printf("enter 5 numbers\n");
    
    for (x = 0; x < 5; x++)
    	scanf("%d", &A[x]);
    
    printf("enter 5 more\n");
    
    	for (y = 0; y < 5; y++)
    	scanf("%d", &A[y]);
    
    	i = -1;
    	while (i<5)
    	{
    
    		i++;
    		sum = A[x][y];
    	}
    
    printf("[%d][%d] = %f\n", x, y, sum);
    
    system("PAUSE");
    return 0;
    }
    i know im not even close, but im lost. I've been reading the forums and reading my book over and over, but I can't find any good examples. Thanks for any help.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Posts
    75
    So, do you want to add up all the elements of the array and display the sum? If yes, the following code may help:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a[5][5], sum;
    	int i, j;
    
    	
    	for (i=0; i<5; i++) /*Get values from user*/
    		for (j=0; j<5; j++) 
    			scanf("%d", &a[i][j]);
    
    	sum = 0;
    	for (i=0; i<5; i++) /*Calculate sum*/
    		for (j=0; j<5; j++) 
    			sum += a[i][j];
    	
    	return 0;
    }
    You can also combine the two for loops into one.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Im looking for more of a table, like what I had in my first post

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    42
    Code:
    #include<stdio.h>
    
    int main()
    {
    
    int x, y, i, j;
    int A[5][5];
    float sum;   // what is this for?
    
    printf("enter 5 numbers\n");
    
    for (x = 0; x < 5; x++)
    	scanf("%d", &A[x][0]);  // reading rows
    
    printf("enter 5 more\n");
    
    	for (y = 0; y < 5; y++)
    	scanf("%d", &A[0][y]);  // reading columns
    
    /* loop to sum & display array */
    for (i=0; i<5; i++) {
         for (j=0; j<5; j++) {
               A[i][j] = A[0][j] + A[i][0];
               printf("%d ", A[i][j]);
         }
         printf("\n");
    }
      
    
    system("PAUSE");
    return 0;
    }
    I believe this is what you're trying to achive.

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    Holy crap, thanks. I see where i've been going wrong. I appreciate the help.

    It would be lovely if my teacher taught this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For loop through a 2D array - just for select rows
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 04-17-2009, 08:34 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. sum rows & columns in array
    By ronenk in forum C Programming
    Replies: 7
    Last Post: 06-20-2004, 04:16 AM
  4. C Filling and Printing a 2-d array
    By Smoot in forum C Programming
    Replies: 3
    Last Post: 11-13-2003, 08:42 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM