Thread: 2 dimensional array

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    2 dimensional array

    I cannot see any reason this doesn't work. Here are the errors I get.
    error C2059: syntax error : '['
    error C2059: syntax error : '['
    error C2146: syntax error : missing ')' before identifier 'sumrow5'
    error C2059: syntax error : ')'
    the first two errors brings me to the line of sumrow1.
    insert
    Code:
    #include "stdafx.h"
    #include "math.h"
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    #define rows 5
    #define cols 5
    	int val[rows][cols]={8,3,9,0,10,3,5,17,1,1,2,8,6,23,1,15,7,3,2,9,6,14,2,6,0};
    	int a, b, sumrow, sumrow1, sumrow2, sumrow3, sumrow4, sumrow5, sumcol, sumcol1, sumcol2, sumcol3, sumcol4, sumcol5;
    	
    	printf("\ndisplay of elements\n");
    	{
    	for (a=0; a<rows; a++)
    	
    		printf("\n");
    		for (b=0; b<cols; b++)
    			printf("%d\n", val[a][b]);
    	}
    	 sumrow1= val[0][0]+val[0][1]+val[0][2]+val[0][3]+[0][4];
    	 sumrow2= val[1][0]+val[1][1]+val[1][2]+val[1][3]+[1][4];
    	 sumrow3=val[2][0]+val[2][1]+val[2][2]+val[2][3]+val[2][4];
    	 sumrow4=val[3][0]+val[3][1]+val[3][2]+val[3][3]+val[3][4];
    	 sumrow5=val[4][0]+val[4][1]+val[4][2]+val[4][3]+val[4][4];
    	 sumcol1=val[0][0]+val[1][0]+val[2][0]+val[3][0]+val[4][0];
    	 sumcol2=val[0][1]+val[1][1]+val[2][1]+val[3][1]+val[4][1];
    	 sumcol3=val[0][2]+val[1][2]+val[2][2]+val[3][2]+val[4][2];
    	 sumcol4=val[0][3]+val[1][3]+val[2][3]+val[3][3]+val[4][3];
    	 sumcol5=val[0][4]+val[1][4]+val[2][4]+val[3][4]+val[4][4];
    	printf("the row totals are %d %d %d %d %d\n", sumrow1, sumrow2, sumrow3, sumrow4 sumrow5);
    	printf("the coloumn totals are %d %d %d %d %d", sumcol1, sumcol2, sumcol3, sumcol4, sumcol5);
    	return 0;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you are missin val in the last member
    your sumrow and sumcol should be arrays as well
    here is the proper way to initialize two-dimentional array
    Code:
    int val[rows][cols]={{8,3,9,0,10},{3,5,17,1,1},{2,8,6,23,1},{15,7,3,2,9},{6,14,2,6,0}};
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    thanks

    thanks vart, that was the first time so far in using the board that someones advice lead me to fixing my program so it works. Usually I just end up with more questions or another dead end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM