Thread: course assignment

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    1

    Question course assignment

    I have been working on this for a week now, I just cant figure out why it won't run correctly.

    This is a multi-dimensional array 5x5. I can't get the scanf input into the appropriate elements. I don't understand why. Any help/direction is greatly appreciated.

    Thanks
    Code:
    #include <stdio.h>
    
    #define a 5
    
    int main()
    {
    	int row, col, rt=0, ct=0, x=0;
    	long int number[a][a] = {1}, num=0;
    
    	for (row=0; row<a; row++) {
    		printf("Enter row %d:", ++x);
    		for (col=0; col<a; col++) 
    				scanf("%l", number[row][col]);
    		
    	}
    
    	printf("\n\nRow totals:");
    	for (row=0; row<a; row++) {
    		for (col=0; col<a; col++) {
    			rt =+ number[row][col];
    		}
    	}
    	printf("\nColumn totals:");
    	for (col=0; col<a; col++) {
    		for (row=0; row<a; row++) {
    			ct =+ number[row][col];
    		}
    	}
    	printf("\n\n");
    	return 0;
    }

    Code tags added by Hammer

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005

    Re: course assignment

    Here are some suggestions.
    Code:
    #include <stdio.h>
    
    #define a 5
    
    int main()
    {
       int row, col/*, rt=0, ct=0, x=0 */;
       long int rt,ct,number[a][a]/* = {1}, num=0 */;
    
       for (row=0; row<a; row++) {
          printf("Enter row %d:", row+1 /* ++x */);
          fflush(stdout);
          for (col=0; col<a; col++) 
             scanf("%ld", &number[row][col]);
       }
    
       printf("\nRow totals:\n");
       for ( row=0; row<a; row++) {
          for (rt=0, col=0; col<a; col++) {
             rt += /* =+ */ number[row][col];
          }
          printf("Row %d: %ld\n", row + 1, rt);
       }
       printf("\nColumn totals:\n");
       for (col=0; col<a; col++) {
          for (ct=0, row=0; row<a; row++) {
             ct += /* =+ */ number[row][col];
          }
          printf("Column %d: %ld\n", col + 1, ct);
       }
       printf("\n\n");
       return 0;
    }
    Sample execution:
    Code:
    Enter row 1: 1 2 3 4 5
    Enter row 2: 2 3 4 5 6
    Enter row 3: 3 4 5 6 7
    Enter row 4: 4 5 6 7 8
    Enter row 5: 5 6 7 8 9
    
    Row totals:
    Row 1: 15
    Row 2: 20
    Row 3: 25
    Row 4: 30
    Row 5: 35
    
    Column totals:
    Column 1: 15
    Column 2: 20
    Column 3: 25
    Column 4: 30
    Column 5: 35
    Last edited by Dave_Sinkula; 01-06-2003 at 05:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM