Problem with functions and arrays in C

1

I do not understand why the program does not do what I order in the functions and it simply prints on the screen the two "printf" that I use test in the main.

#include <stdio.h>
#define MAX 10
void leerVect(int vect[MAX]);
void escribirVect (int v[MAX], int tam);


int main ()
{
    int v[MAX], tam=10;
    printf("empezamos\n");
    void leerVect(v);
    void escribirVect (v, tam);
    printf("Terminamos");
    return 0;
}

void leerVect(int v[MAX])
{
    int i;
    for (i=0; i<MAX && v[i]!=0; i++)
    {
        printf("Introduzca el valor de v[%d]\n", i);
        scanf("%d", &v[i]);
    }
}

void escribirVect (int v[MAX], int tam)
{
    int i;
    for (i=0; i<tam; i++)
    {
        printf("El valor de v[%d] es: %d \n", i, v[i]);
    }
}
    
asked by user105105 28.10.2018 в 03:04
source

2 answers

2

As Jack the Ripper said, let's go in parts:

int main( ) {
  ...
  void leerVect(v);
  void escribirVect (v, tam);
  ...
}

There no you are calling any function; you are declaring them again . In fact, the compiler will probably generate you some warning :

  

In function 'main':
  warning: parameter names (without types) in function declaration
void leerVect(v);

  warning: parameter names (without types) in function declaration
void escribirVect (v, tam);

To call them, it is not necessary to indicate the type of return, just your name and your arguments:

int main( ) {
  ...
  leerVect(v);
  escribirVect (v, tam);
  ...
}

Now, let's see your reading loop:

for( i = 0; i < MAX && v[i] != 0; i++ )

That comparison v[i] != 0 that you make there? Since you do not initialize your training , it will contain random values; Do not have a way of knowing whether or not you have a 0 .

So, we remove that curious check :

for( i = 0; i < MAX; i++ )

Ready, it should work as expected: -)

    
answered by 28.10.2018 в 09:47
0

As Trauma says, you must first call the functions (in this case leerVect(v) and escribirVect (v, tam) , do not declare them inside the main), second you must go through the vector in the same way that you go through and calf the variable tam is not necessary

The corrected program would look like this:

#include <stdio.h>
#define MAX 10
void leerVect(int vect[MAX]);
void escribirVect (int v[MAX]);


int main ()
{
    int v[MAX];
    printf("empezamos\n");
    leerVect(v);
    escribirVect (v);
    printf("Terminamos");
    return 0;
}

void leerVect(int v[MAX])
{
    int i;
    for (i=0; i<MAX; i++)
    {
        printf("Introduzca el valor de v[%d]\n", i);
        scanf("%d", &v[i]);
    }
}

void escribirVect (int v[MAX])
{
    int i;
    for (i=0; i<MAX; i++)
    {
        printf("El valor de v[%d] es: %d\n", i, v[i]);
    }
}
    
answered by 28.10.2018 в 14:11