Media calculation using C++ Function

Asked

Viewed 860 times

1

I’m doing a program where it calculates arithmetic, harmonic and podenrada media using a function called note in C++. At the time I compile the media result only returns 1.

    #include<iostream>
    #include<cstdlib>
    
    using namespace std;
    
    float nota( float n1, float n2, float n3, char a, char p, char h){
    float m;
    
    cout<<"Escolha uma media: ";
    cin>>m;
    
    if(m == a){
        a = n1+n2+n3/3;
    }
    if (m==p){
        p=n1*5+n2*3+n3*2/3;
    }
    
    if (m==h){
        h=1/n1+1/n2+1/n3 /3;
    }
    
    return m;
    
    }
    char escolha;
    
    
    int main(){
    cout<<"\t        ++++++ MEDIA ++++++\n";
    cout<<"\t ======================================== \n";
    cout<<"\t |1=Aritemtica  2=Ponderada  3=Harmonica| \n";
    cout<<"\t ======================================== \n";
    
    float n1,n2,n3;
    
    cout<<"Nota do aluno:";
    cout<<"\n\n";
    
    nota;
    cout<<"Nota 1: ";
    cin>>n1;
    
    nota;
    cout<<"Nota 2: ";
    cin>>n2;
    
    nota;
    cout<<"Nota 3: ";
    cin>>n3;
    
    nota;
    cout<<"Ecolha uma media: ";
    cin>>escolha;
    
    cout<<"Media: "<<nota;
    cout<<"\n\n";
    system("pause");
    
    }
  • The code even compiles ? In the function call you pass the arguments ?

  • Yes, compile and return zero does not compute the values of the variables

  • Note that in your if you’re comparing float with char. Study operator precedence, e.g. the arithmetic mean is calculated by summing the 3 values and dividing the result by 3: a = (n1+n2+n3)/3; and not adding up the 2 values with 1/3 of the third (a = n1+n2+n3/3; as you did. Idem too medium. This command nota; has no sense, maybe you intended to call a function?

2 answers

1

Boy, here’s a code that’s more accurate, closer to what you want to do. But there are several things (as already commented) that you need to understand better, judging by the code you posted.

#include<iostream>
#include<cstdlib>
    
using namespace std;
    
float nota( float n1, float n2, float n3, int escolha){
    float m;
    
    if(escolha == 1){
        m = n1+n2+n3/3;
    }
    if (escolha == 2){
        m = n1*5+n2*3+n3*2/3;
    }
    
    if (escolha == 3){
        m = 1/n1+1/n2+1/n3 /3;
    }
    
    return m;
    
}

    
    
int main(){
    cout<<"\t        ++++++ MEDIA ++++++\n";
    cout<<"\t ======================================== \n";
    cout<<"\t |1=Aritemtica  2=Ponderada  3=Harmonica| \n";
    cout<<"\t ======================================== \n";
    
    float n1,n2,n3;
    int escolha;
    
    
    cout<<"Nota do aluno:";
    cout<<"\n\n";
    
    cout<<"Nota 1: ";
    cin>>n1;
    
    cout<<"Nota 2: ";
    cin>>n2;
    
    cout<<"Nota 3: ";
    cin>>n3;
    
    cout<<"Ecolha uma media: ";
    cin>>escolha;
    
    cout<<"Media: "<<nota(n1, n2, n3, escolha);
    cout<<"\n\n";
    system("pause");
    
}
  • Thanks Thanks, I’m looking at your code here and seeing my mistakes, I’m starting in programming as you might notice kkkkk. I’ll try to do other examples like this so I can train.

0

Correcting average calculations and removing choice duplicity.

#include<iostream>
#include<cstdlib>

using namespace std;

float calc_media(float n1, float n2, float n3, int tipo) {
    float m;
    
    if(tipo == 1){ /* Aritmética */
        m = (n1+n2+n3)/3;
    }
    if (tipo == 2){ /* Ponderada */
        m = (n1*5+n2*3+n3*2)/(5+3+2);
    }
    
    if (tipo == 3){ /* Harmônica */
        m = 3 / (1/n1 + 1/n2 + 1/n3);
    }
    
    return m;
}
    
int main(){
    cout<<"\t       ++++++ MEDIA ++++++\n";
    cout<<"\t ======================================== \n";
    cout<<"\t |1=Aritemtica  2=Ponderada  3=Harmonica| \n";
    cout<<"\t ======================================== \n";
    
    float n1,n2,n3, media;
    int escolha;

    cout<<"Nota do aluno:";
    cout<<"\n\n";
    cout<<"Nota 1: ";
    cin>>n1;
    cout<<"Nota 2: ";
    cin>>n2;
    cout<<"Nota 3: ";
    cin>>n3;
    
    cout<<"Ecolha uma media: ";
    cin>>escolha;
    media = calc_media(n1, n2, n3, escolha);
    cout << "Media: " << media << endl;
    system("pause");
    return 0;
}

Browser other questions tagged

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