I wonder what’s wrong with my code?

Asked

Viewed 592 times

1

My code is a question, I’m learning to program and using the site codcad, the question is in the image. When I send my code, it only gives 60 points of 100. I wonder what I’m doing wrong.

Follow the image.

Edit:I tried to solve the issue using only what I learned on the site, more advanced things still do not know very well, so I may have gone for a more complicated logic.Questão

Question Obs:The question is related to a list of exercises on if. Carnival (OBI 2012, Phase 2, Level 1)

Carnival is a holiday usually celebrated in February; in many Brazilian cities, the main attraction are the school parades of samba. The various associations parade to the sound of their sambas-plots and are judged by the league of samba schools to determine the champion of Carnival.

Each association is evaluated in several categories; in each category, each school receives five grades ranging from 5.0 to 10.0. The final grade of the school in a given category is the sum of the three central notes received by the school, excluding the highest and lowest of the five grades.

As there are many samba schools and many inquiries, the president The League asked you to write a program that, given the notes of the association, calculates its final grade in a given category.

Input The entry contains a single line, containing five Ni(1 i 5), all to one decimal place, indicating the notes received by association in one of the items.

Output Your program must print a single line, containing a single line number with exactly one decimal place, the final grade of the school of samba in the category considered.

Restrictions
5.0 Ni 10.0

**Exemplos de Entrada**    ......     *Exemplos de Saída*  
**6.4 8.2 8.2 7.4 9.1**        ......     *23.8*  
**10.0 10.0 5.0 5.0 10.0**     ......     *25.0*

Code

#include <iostream>

using namespace std;

int main(){

 double a, b, c, d, e;

 cin>>a>>b>>c>>d>>e;

 if (a>=b and a>=c and a>=d and a>=e){
    if(b<a and b<=c and b<=d and b<=e){
        cout<<c+d+e;
     }
     if(c<b and c<=d and c<=e) {
        cout<<b+d+e;
     }
     if(d<=b and d<c and d<=e){
        cout<<b+c+e;
     }
    if(e<a and e<b and e<c and e<d){
        cout<<b+c+d;
     }
 }
 if (b>a and b>=c and b>=d and b>=e){
    if(a<b and a<=c and a<=d and a<=e){
        cout<<c+d+e;
    }
    if(c<a and c<=d and c<=e){
        cout<<a+d+e;
    }
    if (d<a and d<c and d<=e){
        cout<<a+c+e;
    }
    if(e<a and e<b and e<c and e<d){
        cout<<a+c+d;
    }
 }
 if (c>a and c>b and c>=d and c>=e){
    if(a<=b and a<=d and a<=e){
        cout<<b+d+e;
    }
    if(b<a and b<=d and b<=e){
        cout<<a+d+e;
    }
    if(d<a and d<b and d<=e){
        cout<<a+b+e;
    }
    if(e<a and e<b and e<c and e<d){
        cout<<a+b+d;
    }
 }
 if(d>a and d>b and d>c and d>=e){
    if(a<=b and a<=c and a<=e){
        cout<<b+c+e;
    }
    if(b<a and b<=c and b<=e){
        cout<<a+c+e;
    }
    if(c<a and c<b and c<=e){
        cout<<a+b+e;
    }
    if(e<a and e<b and e<c and e<d){
        cout<<a+b+c;
    }
 }
 if(e>a and e>b and e>c and e>d){
    if(a<=b and a<=c and a<=d){
    cout<<b+c+d;
    }
    if(b<a and b<=c and b<=d){
    cout<<a+c+d;
    }
    if(c<a and c<b and c<=d){
    cout<<b+a+d;
    }
    if(e<a and e<b and e<c and e<d){
        cout<<a+b+c;
    }

 }
}

BS: I’m sorry for anything I’ve done wrong I’m new to the site and if I’m done something wrong I accept suggestions and tips. Obs2: I accept tips and suggestions tbm for programming issue. Grateful for the attention.

  • 1

    Hello Prometheus. Logic doesn’t have to be this complicated. Read the 5 notes for a vector, sort them (e.g., using std::sort) and add the three middle elements.

  • 1

    Also check that your output always has a decimal place: std::cout << std::fixed << std::setprecision(1) << somaNotas;

  • I’m sorry I lacked information in my question, this tip of yours by what I researched should solve, but I am very new in programming and if it is possible to give me a simpler explanation would be grateful, because I used everything I know so far.

  • A good start would be to post the statement in text, not a screen print.

  • okay, I thought it would look really bad the whole text here, I’ll add the statement.

1 answer

4

Follow an implementation of the exercise:

#include <algorithm>  
#include <array>   
#include <iomanip>
#include <iostream>
#include <numeric>
#include <tuple>

int main()
{
    std::array<double, 5> notas;
    std::generate(notas.begin(), notas.end(), [] {
        double nota;
        std::cin >> nota;
        return nota;
    });

    decltype(notas)::iterator menor_nota, maior_nota;
    std::tie(menor_nota, maior_nota) = std::minmax_element(notas.begin(), notas.end());

    std::iter_swap(menor_nota, notas.end() - 2);
    std::iter_swap(maior_nota, notas.end() - 1);

    double soma_notas = std::accumulate(notas.begin(), notas.end() - 2, 0.0);

    std::cout << std::fixed << std::setprecision(1) << soma_notas;

    return 0;
}

Here I am using the C++ Standard Library to do absolutely everything, but you can - and, from a learning point of view, must - manually reimplement these algorithms using loops and simple logic.

Algorithm logic:

  1. Read all the elements for a container (array, vector, etc.)
  2. Find the highest and lowest note
  3. Remove (or in case, move to the end of the container) the elements found
  4. Add the remaining items
  5. Print the formatted result correctly
  • Thank you friend, I will study this code and implement it. Thank you for your help.

Browser other questions tagged

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