Repeat loop in C++

Asked

Viewed 62 times

1

Good night, my friends!

I’m starting the ADS course and I have a question. I need to create a loop of repetition for 50 students by collecting 4 grades of 4 exams, and at the end, present a report with the grades of each of the individuals and the overall average of the class. Well, I already pulled the job (for those who are thinking I want to cheat the teacher and by table fool me along rs. I managed to make the average, however, I could not declare the individual notes :(... In my logic, to do this, I would have to create a var for each of the students and keep the results there, but that would be an unnecessary job and I couldn’t think of another way. Does anyone have any idea how to do it? I use the book Damas' Language C and I couldn’t find anything like it either.

My code only with general media:

#include <stdio.h>

main()
{
    char q1,q2,q3,q4;
    int alunos,totalunos,notaturma,notaq1,notaq2,notaq3,notaq4;
    float media;
    for (alunos=1;alunos<=2;alunos++)
    {
        printf("\nSua resposta da questao 1: "); scanf(" %c",&q1);
        printf("\nSua resposta da questao 2: "); scanf(" %c",&q2);
        printf("\nSua resposta da questao 3: "); scanf(" %c",&q3);
        printf("\nSua resposta da questao 4: "); scanf(" %c",&q4);
        
        if (q1=='A')
        notaq1=notaq1+2;
        if(q2=='C')
        notaq2=notaq2+2;
        if(q3=='A')
        notaq3=notaq3+2;
        if(q4=='C')
        notaq4=notaq4=2;
        totalunos++; //Coletará o total de alunos para ser usado no calculo da média de notas//
        
    }
    notaturma=notaq1+notaq2+notaq3+notaq4;
    totalunos=totalunos-1; //Retirei um pois estava sendo contato um aluno a mais//
    media=notaturma/totalunos;
    printf("\n%2.f E A MEDIA DA TURMA",media);

}
  • 1

    Note that defining individual variables for each grade of each of the 50 students is unproductive and impractical, so we use an array. For this case we can declare char nota[50][4]; where each row of this array represents a student and each column the concept of a proof. What you haven’t explained is what are the possible concepts and how you turn the concept into a note for calculating the average. Maybe float nota[50]4]; is more suitable for the calculation of the average and each note could easily be transformed into a concept considering ranges of values.

  • 1

    Perfect! The teacher had not given this concept, but charged kkkkkkkkkkkkkkkkkkk Thank you very much, I will research on array.

2 answers

2

Hello good evening, follow my resolution to your problem, the only difference is that I used the C++ language for the resolution, if I can leave the exercise in sample I will be able to help you better, (at the time I was able to extract from your statement). If you want to test the code, I’ll leave the IDE link online REPL C++ for you to test the code :) https://repl.it/languages/cpp

#include <iostream>
using namespace std;

int main() 
{
    char notasAlunos[50][4];
    float notaGeralTurma = 0;
    int i;
    int j;
    
    for (i = 0; i < 50; i++)
    {
        for (j = 0; j < 4; j++)
        {
            cout << "A " << j+1 << "° do aluno: ";
            cin >> notasAlunos[i][j];
                
            if (notasAlunos[i][j] == 'A' || 'a' ||'C' || 'c')
                notaGeralTurma += 2;
        }
    }
    
    cout << "A media da turma é: " << notaGeralTurma / 50 << endl;
    
    cout << "As notas lidas foram :" << endl;
    for (i = 0; i < 50; i++)
    {
        for (j = 0; j < 4; j++)
        {
            cout << notasAlunos[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

0

In C++ has a lot of rules and coding patterns

1 - Create a structure and overwrite << and >>

struct respostas : array<char, N_QUESTOES> {
    friend ostream& operator<<(ostream& o, const respostas& n ) {
       return o << "Nota do aluno: " << string_view(n.begin(), n.end());
    }
    friend istream& operator>>(istream& i, respostas& n ) {
       for ( auto& el : n ) 
          i >> el;
       return i;
    }
};

2 - Avoid loops for/while in your code, in which case it will best encapsulate the loops by creating a generic algorithm to check how many Matches two containers have and use it to see how many points each student has made:

template<typename It1, typename It2>
int matches(It1 first, It1 last, It2 first2) {
    int result = 0;
    while( first != last ) {
        if ( *first++ == *first2++ ) {
            ++result;
        }
    }
    return result;
}

3 - And the "core" of the program is a call to the algorithm accumulate making it evident that you are doing some sort of totalization in the elements.

auto acertos = accumulate( respostasDosAlunos.begin(), respostasDosAlunos.end(), 0, [&in, &gabarito](auto result, auto& resp){
    cout << "\nNota do aluno: ";
    in >> resp;
    return result + matches(resp.begin(), resp.end(), gabarito.begin());
});

https://godbolt.org/z/Wc1MWo

Browser other questions tagged

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