Select notes to be printed

Asked

Viewed 48 times

2

I need to print only the notes above >=6, in which place of the code I put the if or some other instruction?

I tried and I couldn’t find the right place.

#include <iostream>
#include <string.h>
using namespace std;

#define MAX 50

struct aluno {
    char nome[50];
    int matricula;
    float nota;
};

void imprime(aluno v[], int num);

int main(void) {
    // Declara um aluno
    aluno alunos[MAX];

    alunos[0].matricula = 12014432;
    alunos[0].nota = 7.5;
    strcpy(alunos[0].nome,"Fulano");
    alunos[1].matricula = 13022301;
    alunos[1].nota = 5.9;
    strcpy(alunos[1].nome,"Sicrano");
    alunos[2].matricula = 11024122;
    alunos[2].nota = 4.0;
    strcpy(alunos[2].nome,"Beltrano");
    alunos[3].matricula = 11014120;
    alunos[3].nota = 6.1;
    strcpy(alunos[3].nome,"Huguinho");
    alunos[4].matricula = 11024012;
    alunos[4].nota = 8.0;
    strcpy(alunos[4].nome,"Zezinho");
    alunos[5].matricula = 11014117;
    alunos[5].nota = 3.5;
    strcpy(alunos[5].nome,"Luizinho");

    imprime(alunos, 6);

}

void imprime(aluno v[], int num) {
    int i;

    for (i=0; i<num; i++) {

        cout << "Mat.: " << v[i].matricula << endl;
        cout << "Nome: " << v[i].nome << endl;
        cout << "Nota: " << v[i].nota << endl;
    }
}

1 answer

3


Which part is printing what you want? Currently prints the grade of all students, right?

    cout << "Mat.: " << v[i].matricula << endl;
    cout << "Nome: " << v[i].nome << endl;
    cout << "Nota: " << v[i].nota << endl;

This, right?

Then the if should encapsulate all of it and nothing more than it. The stretch that sweeps the whole vector is this:

for (i=0; i<num; i++) {

    cout << "Mat.: " << v[i].matricula << endl;
    cout << "Nome: " << v[i].nome << endl;
    cout << "Nota: " << v[i].nota << endl;
}

Then it would be:

for (i=0; i<num; i++) {
    if (v[i].nota >= 6) {
        cout << "Mat.: " << v[i].matricula << endl;
        cout << "Nome: " << v[i].nome << endl;
        cout << "Nota: " << v[i].nota << endl;
    } 
}

I put in the Github for future reference.

  • I almost got had put if (note[i]>=6), thank you very much again.

  • 1

    To print the note you used v[i].nota, To verify which note is, it doesn’t have to be different. The note is the note, it can’t be one way in one place and another in another place. in this case you’re taking the field nota of the element indicated by i vector v.

Browser other questions tagged

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