Error computing mean with vector C

Asked

Viewed 184 times

0

I am trying to calculate the note average of an array in which I made dynamic allocation, but the following error appears:

line 66- [Error] invalid Conversion from 'int*' to 'int' [-fpermissive]

line 66- [Error] Too few Arguments to Function 'int media(int, int*)'

#include <stdio.h>


#include <stdlib.h>

int media(int n, int *pN);

int main(void) {
    
    int *pA, *pN; //criando ponteiros para os vetores
    int i,j;
    int qtdAlunos,notas;
    int media_notas;



//Aqui dou inicio ao vetor de alunos.
    printf("Numero de alunos: ");
    scanf("%d", &qtdAlunos);



    pA = (int *)(malloc(qtdAlunos * sizeof(int)));

    
    for (i = 0; i< qtdAlunos; i++)
    {
        printf("Digite o numero referentes aos alunos: [%d] = ", i);
        scanf("%d", &pA[i]);
    }

//percorrendo o vetor para mostar os valores armazenados
    for (i = 0; i< qtdAlunos; i++)
    {
        printf("\nAlunos: [%d] = %d", i, pA[i]);
        
    }


//Agora estarei criando valores do vetor nota

    
    printf("\n\n");
    printf("Quantas notas? ");
    scanf("%d", &notas);
    
  
    
    pN = (int*)(malloc(notas * sizeof(int)));
        
    for (j = 0; j<notas; j++)
    {
        printf("Digite as notas: [%d] = ", j, pN[j]);
        scanf("%d", &pN[j]);
    }
    

    //percorrendo o vetor para mostrar valores de notas
    
    for (j = 0; j<notas; j++)
    {
        printf("\nNotas: [%d] = %d", j,pN[j]);
    }
    
//chamada da função
    media_notas = media(pN);
    
    scanf ("\nMedia = %.1f \n", media_notas);
    
    system("pause");
    return 0;
    
}

int media (int n, int *pN)
{
    int j;
    int m = 0, soma = 0;
    
    for ( j = 0; j < n; j++ )
    soma = soma + pN[j];
    
    m= soma / n;
    return m;
}
  • Note that you use media_notas = media(pN); but defined the media function with 2 parameters, the amount of elements and the array, I believe it should be: media_notas = media(notas, pN);.

1 answer

2

  1. The first error happens because you set the "media" function as receiving two parameters, an integer and a pointer to integer. However, the function call is made by passing only one parameter, which is a pointer to an integer, the first parameter being an integer. The second error occurs because the function expects two parameters, but only one has been passed. To resolve these errors, it is necessary to correct the function call:

    media_notes = media(notes, Pn);

In addition, the code has some logic errors:

  1. The "media" function should be of the type double or float, since an arithmetic mean can contain numbers after the comma;

  2. The variable "m" within the "media" function must also be of the type double or float, because it will contain the return value of the function;

  3. The variable "media_notes" within the "main" function must also be of the type double or *float", because it will receive the return value of the function "media";

  4. Probably by mistake, you wrote

    scanf ("\nMedia = %.1f \n", media_notas);
    

    to show the average of the notes, the command used should be the printf:

    printf ("\nMedia = %.1lf \n", media_notas);
    

In addition, there was an extra *%d" in the line:

printf("Digite as notas: [%d] = ", j, pN[j]);

You should only show the value of j, since the value of Pn[j] will still be read:

The whole corrected code looks like this:

#include <stdio.h>


#include <stdlib.h>

double media(int n, int *pN);

int main(void) {
    
    int *pA, *pN; //criando ponteiros para os vetores
    int i,j;
    int qtdAlunos,notas;
    double media_notas;



//Aqui dou inicio ao vetor de alunos.
    printf("Numero de alunos: ");
    scanf("%d", &qtdAlunos);



    pA = (int *)(malloc(qtdAlunos * sizeof(int)));

    
    for (i = 0; i< qtdAlunos; i++)
    {
        printf("Digite o numero referentes aos alunos: [%d] = ", i);
        scanf("%d", &pA[i]);
    }

//percorrendo o vetor para mostar os valores armazenados
    for (i = 0; i< qtdAlunos; i++)
    {
        printf("\nAlunos: [%d] = %d", i, pA[i]);
        
    }


//Agora estarei criando valores do vetor nota

    
    printf("\n\n");
    printf("Quantas notas? ");
    scanf("%d", &notas);
    
  
    
    pN = (int*)(malloc(notas * sizeof(int)));
        
    for (j = 0; j<notas; j++)
    {
        printf("Digite as notas : [%d] = ", j);
        scanf("%d", &pN[j]);
    }
    

    //percorrendo o vetor para mostrar valores de notas
    
    for (j = 0; j<notas; j++)
    {
        printf("\nNotas: [%d] = %d", j,pN[j]);
    }
    
//chamada da função
    media_notas = media(notas, pN);
    
    printf ("\nMedia = %.1lf \n", media_notas);
    
    return 0;
    
}

double media (int n, int *pN)
{
    int j;
    int soma = 0;
    double m;
    
    for ( j = 0; j < n; j++ )
     soma = soma + pN[j];
    
    m = (double)soma / n;

    system("pause");
    return m;
}

See working on repl.it

Browser other questions tagged

You are not signed in. Login or sign up in order to post.