Function parameter error

The problem is the n, 15:24: error: 'n' undeclared here (not in a function)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include <stdio.h>

 

void simetrica(int mat[][n], int n);

int main (void) {
  
  int n=3;
  int mat[n][n];
  simetrica(mat,n);
}


void simetrica(int mat[][n], int n) {
  
  int j;
  int i;
  
  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    mat[i][j]=(j+i);
    }}
  int cuenta=0;
  for(i=0; i<n; i++){
    for(j=0; j<n; j++){
    
      if(mat[i][j]==mat[j][i]){
	cuenta++;
	
      }}}
      if(cuenta==(n*n)){
	printf("la matriz es simétrica");
      }
      else{
	printf("La matriz no es simétrica");
      }}
    
Last edited on
declare n before its first use:
1
2
3
4
5
6
const int n=3; // Use const to make it legal c++
void simetrica(int mat[][n], int n);

int main (void) {
  
  int n=3;
Also it's worth noting that the two occurrences of 'n' in the declaration:
 
void simetrica(int mat[][n], int n);
are completely separate and have a different meaning.

Here int mat[][n] n is a constant, known at compile time
whereas int n simply says the function will take an integer variable as its second parameter.

In fact, when declaring the constant as suggested above,
const int n=3; // Use const to make it legal c++
the second parameter is no longer required, the function declaration can look like this:
 
void simetrica(int mat[][n]);

(remember to change the function definition to match).
Topic archived. No new replies allowed.