programming help in c please

Discussion in 'OT Technology' started by CXHatchback, Apr 17, 2003.

  1. CXHatchback

    CXHatchback Guest

    I'm trying to sum the numbers in a 2d array. Can anyone see what I'm getting wrong? I keep getting an error.

    Code:
    #include <stdio.h>
    #define MAXROW 10
    #define MAXCOL 10
    
    int matrix[MAXROW][MAXCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},row,col;
    
    int addition(int matrix[MAXROW][MAXCOL],int i,int j);
    int main(void)
    	{
    	addition(matrix[MAXROW][MAXCOL],row,col);
    	return 0
    	}
    
    int addition(int matrix[MAXROW][MAXCOL],int i,int j)
    	{
    	for (i=0;i<MAXROW;++i)
    		for (j=0;J<MAXCOL;++j);
    			sum+=matrix[i][j];
    	printf("\nsum=%d",sum);
    	return 0
    	}
     
    Last edited by a moderator: Apr 17, 2003
  2. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    omg it's been a longgg time since I looked at code...heh

    whats the error your getting???

    for (j=0;J<MAXCOL;++j);

    hmmmmmm........aint it a big ass nono to have ; at the end of for statments?

    I havent touched C in a long time, I am pretty sure that wouldn't compile though, and if it does, wont run right...

    |eb_CRX
     
  3. Leb_CRX

    Leb_CRX OT's resident terrorist

    Joined:
    Apr 22, 2001
    Messages:
    39,994
    Likes Received:
    0
    Location:
    Ottawa, Canada
    also use
    for (X, X, X)
    {
    AAAAAA
    }

    {} ownz j00
     
  4. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    leme take a stab at it (havent done c in about a year, too busy with my new love java.)

    Code:
    #include <stdio.h>
    #define MAXROW 10
    #define MAXCOL 10
    
    int matrix[MAXROW][MAXCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},row,col;
    
    int addition(int matrix[MAXROW][MAXCOL],int i,int j);
    
    int main(void){
    	addition(matrix[MAXROW][MAXCOL],row,col);
    	return 0
    }//main(void)
    
    int addition(int matrix[MAXROW][MAXCOL],int i,int j){
    	for (i=0;i<MAXROW;++i){
    		for (j=0;J<MAXCOL;++j){
    			sum+=matrix[i][j];
                              }//for(j<MAXCOL++j)
                 }//for(i<MAXROW++i)
    
    	printf("\nsum=%d",sum);
    	return 0
    }//addition(int matrix,int,int)
    
    
    it may work, it may not. i dont have my vs6 or vs.net installed to test it with.
     
  5. kcarlson78

    kcarlson78 Meanwhile, Jesus continued to shake his head in ex

    Joined:
    Jun 27, 2002
    Messages:
    4,490
    Likes Received:
    0
    Location:
    Phoenix, AZ
    here ya go..

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #define MAXROW 10
    #define MAXCOL 10
    
    int matrix[MAXROW][MAXCOL]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20},row,col;
    
    void addition(int matrix[MAXROW][MAXCOL],int i,int j);
    [b][color=blue]     // return type void b/c you're not using any results from calculations
         // in the function.[/color][/b]
    
    int main(void)
    	{
    	addition(matrix,row,col);  [b][color=blue]// when passing arrays only the array name is used[/color][/b]
    
        system("PAUSE");     [b][color=blue]//Don't worry about this, I add it b/c my machine likes to close my 
                                     // command window after the program executes so I can't see results..[/color][/b]
    	return 0;        [b][color=blue]// missing semicolon[/color][/b]
    
    	}
    
    void addition(int matrix[MAXROW][MAXCOL],int i,int j)
    	{
    int sum;  [b][color=blue]// sum was previously undeclared, can be declared within the function in this instance[/color][/b]
    	for (i=0;i<MAXROW;++i)
    		for (j=0;j<MAXCOL;++j) [b][color=blue]// capital J != little j
                                 // if you put a ; after a for() then you are going to loop NOTHING[/color][/b]
    			sum+=matrix[i][j];[b][color=blue]// sum must be delclared either globally, or locally[/color][/b]
    	printf("\nsum=%d",sum);
    	return;                            [b][color=blue]// missing semicolon
                                            // "return 0" not necessary[/color][/b]
    	}
    
    
    that'll fix the syntax errors.. pay attention to the compiler errors you get.. they actually generally point you right at your errors and typos..
     

Share This Page