Setok
Setok

Reputation: 1

Implicit declaration of function in main/error:expected ';', ',' or ')' before numeric constant|

#include <stdio.h>
#include <stdlib.h>
#include "simpio.h"

#define cols 10
#define rows 10

void populate_data(int rows,int cols,int A[rows][cols],int R,int C);
void print_array(int rows,int cols,int A[rows][cols],int R,int C);
void change_array(int rows,int cols,int A[rows][cols],int R,int C);

int main(){
    
int R,C,A[rows][cols];

/*Eisagwgh twn grammwn/sthlwn*/
printf("Dwse ton arithmo twn grammwn: ");
R=GetInteger();
printf("Dwse ton arithmo twn sthlwn: ");
C=GetInteger();

populate_data(rows,cols,A,R,C);
printf("ARXIKOS PINAKAS");
print_array(rows,cols,A,R,C);
change_array(rows,cols,A,R,C);
printf("ALLAGMENOS PINAKAS");
print_array(rows,cols,A,R,C);

return 0;

}


void populate_data(int rows,int cols,int A[rows][cols],int R,int C){

int i,j;

for (i=0; i<R; i++){
    for (j=0; j<C; j++){
        A[i][j]=rand()%100;
    }
}

}
void print_array(int rows,int cols,int A[rows][cols],int R,int C){

int i,j;

for (i=0; i<R; i++){
        printf("\n");
    for (j=0; j<C; j++){
        printf("%d",A[i][j]);
    }
}
}
void change_array(int rows,int cols,int A[rows][cols],int R,int C){

int i,j,index,max;

for (i=0; i<R; i++){
        max=A[i][0];
        index=0;
    for (j=1; j<C; j++){
        if (A[i][j]>max){
            max=A[i][j];
            index=j;
        }
    }
    for (j=0; j<index; j++){
        A[i][j]=max;
    }
}

}

So I just wrote this code for a project at my university. When I compile it, it says "Implicit declaration of function ", but the lines where the error occured are these: "populate_data(rows,cols,A,R,C); print_array(rows,cols,A,R,C); change_array(rows,cols,A,R,C);". Also, it says "error:expected ';', ',' or ')' before numeric constant|." at the #define lines. What is wrong?

Upvotes: 0

Views: 56

Answers (1)

John Kugelman
John Kugelman

Reputation: 361977

The cols and rows preprocessor constants are conflicting with the cols and rows variables. You can't use the same names for both. The preprocessor is replacing all the occurrences with 10, yielding this syntactically invalid code:

void populate_data(int 10,int 10,int A[10][10],int R,int C);
void print_array(int 10,int 10,int A[10][10],int R,int C);
void change_array(int 10,int 10,int A[10][10],int R,int C);

I advise always using uppercase for preprocessor macros. That's the norm in C to make it obvious which identifiers are macros.

#define COLS 10
#define ROWS 10

Upvotes: 1

Related Questions