Thread: Computing the sum of each diagonal in an array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    Computing the sum of each diagonal in an array

    So, I am trying to make a program that computes and outputs the sum of each diagonal in a [10][10] array. This is what I have so far. Yes I know it only adds up the thing row by row then column by column. That's my problem, I have no idea how to find the sum of the diagonals!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void printArray( const int a[][10]);
    
    int main (void)
    {
    	int a;
    	int i;
    	int x;
    	int total;
    	int num;
    	int array[10][10];
    	
    	for (i = 0; i < 10; i++)
    	{	for ( x = 0; x < 10; x++)
    		{	num = 1 + rand() % 10;	
    			array[i][x] = num;	
    		}
    	}
    
    line 22->	printArray ( array );
    
    	for ( a = 0; a < 10; a++)
    	{        for (i = a; i < 10; i++)
    		{       for ( x = 0; x < 10; x++)
    			{	total += array[i][x];
    				printf("%4d\n", total);		
    			}
    		}
    	}
    
            for ( a = 1; a < 10; a++)
            {        for (i = 0; i < 10; i++)
                    {       for ( x = a; x < 10; x++)
                            {       total += array[i][x];
                            	printf("%4d\n", total);
    			}
    		}
    	}
    
    return 0;
    }
    
    void printArray( const int a[][10])
    {
    	
    	int i;
    	int x;
    
             for (i = 0; i < 10; i++)
            {       for ( x = 0; x < 10; x++)
                    {       printf("%d", a[i][x]);      
    		}
                    printf("\n");
            }
    }
    Last edited by Seeshi_suin; 11-22-2010 at 11:47 PM. Reason: problem changed

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    7
    well, isn't exactly an error, is a warning so the compilation must have finish ok.

    Anyway, didn't look at the code, just pointing a detail about your error message.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Yeah, it compiles and runs, but my results are just slightly off. I don't know if that is due to poor arithmetic, or if it has to do with the error message.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Oh wow, I just realized my code doesn't even do near that it is supposed to.
    Last edited by Seeshi_suin; 11-22-2010 at 11:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Sum of all elements in an array
    By SilentPirate007 in forum C Programming
    Replies: 6
    Last Post: 05-02-2010, 08:32 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM

Tags for this Thread