ERROR C++ IVALID OPERANDS TYPES 'FLOAT' and 'FLOAT' to Binary 'Operator+'

Asked

Viewed 17 times

-2

#include <iostream>
#include<stdio.h>
#include <stdlib.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

float media (int n, float *vetor[]){
    float m = 0, soma = 0;
    
    //fazendo soma do vetor
    for (int i = 0; i < n; i++){
        soma = soma + vetor[i];
    }
    //calcular media
    m = soma / n;
    //retornar 
    return m;
}

int main(int argc, char** argv) {
    int n;
    float vetor[n];
    float mediaFinal;
    //Leitura das notas
    cout << "Informe o tamanho do vetor: ";
    cin >> n;
    cout << "Preencha o vetor com " << n << " inteiros: ";
    for (int i = 0; i < n; i++) {
        cin >> vetor[n];
    }
    //Chamado do metodo
    mediaFinal = media(n, vetor);
    cout << "Media = " << mediaFinal;
    return 0;
}

When compiling at line 13 it displays the error "invalid operands of types 'float' and 'float*' to Binary 'Operator+'", I don’t know how to resolve.

  • Please clarify your problem or provide additional details in order to highlight exactly what you need. The way it’s written these days it’s hard to tell exactly what you’re asking.

  • I already tidied up I didn’t know how to work the platform

1 answer

0

Firstly, in its function media, you are declaring that it receives as a parameter basically a matrix, or you declare as:

float media (int n, float vetor[])

or how:

float media (int n, float *vetor)

Second, the moment you’re reading the data:

for (int i = 0; i < n; i++) {
        cin >> vetor[n];
    }

You are assigning the input value only to a vector position, the position n.

What is expected for the vector to be populated position the position would be:

for (int i = 0; i < n; i++) {
        cin >> vetor[i];
    }

At the end the code would look similar to this:

#include <stdlib.h>
#include <iostream>
#include<stdio.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

float media (int n, float vetor[]){
    float m = 0, soma = 0;
    
    //fazendo soma do vetor
    for (int i = 0; i < n; i++){
        soma = soma + vetor[i];

    }
    //calcular media
    m = soma / n;
    //retornar 
    return m;
}

int main(int argc, char** argv) {
    int n;
    float vetor[n];
    float mediaFinal;
    //Leitura das notas
    cout << "Informe o tamanho do vetor: ";
    cin >> n;
    cout << "Preencha o vetor com " << n << " inteiros: ";
    for (int i = 0; i < n; i++) {
        cin >> vetor[i];
    }

    //Chamado do metodo
    mediaFinal = media(n, vetor);
    cout << "Media = " << mediaFinal;
    return 0;
}

Browser other questions tagged

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