Why is my Condition Structure assigning a value to a variable?

Asked

Viewed 85 times

2

My code below represents the scenario of keeping a feedback of 5 questions, where:

  • Each question is worth 2 points
  • Check if 3 students will hit the Feedback
  • Calculate Each Student’s Individual Grade
  • Calculate the Class Average

My question is right there in the addition of student grades, more specifically in this expression: soma <- soma + 2.

The sum "unites" the values of nota[a], but I did not put the value in the sum to receive the same.

  • a) Why this happens?
  • b) Because this place uses the + 2?.
  • c) The note[a] vector would actually hold a value?

The Place of Doubt:

  nota[a] <- nota[a] + 2
  soma <- soma + nota[a]

My Code:

 Var i,a,c: inteiro    
    resp,gab: vetor[1..5] de caractere   
    nome: vetor[1..3] de caractere
    nota: vetor[1..5] de real
    m,soma: real

    Inicio

    Para i <- 1 ate 5 faca
            Escreva("Digite o gabarito ", i)
            Leia(gab[i]) 
    FimPara
    Para a <- 1 ate 3 faca
            EscrevaL("ALUNOS ", a)
            Escreva("Nome: ")
            Leia(nome[a])
            Para c <- 1 ate 5 faca
                 Escreva("Digite a questão ", c)
                 Leia(resp[c])
                 Se (gab[i]=resp[c]) entao
                    EscrevaL(" acertou a ", c)
                    nota[a] <- nota[a] + 2
                    soma <- soma + 2
                 FimSe
            FimPara
            m <- soma/3
    FimPara 
    Para a <- 1 ate 3 faca
            EscrevaL(nome[a]: 10, nota[a])
    FimPara
    Escreva("Média da Turma: ", m)
  • 1

    Welcome Ale, is this your code? Could you edit the ask and put the exercise? Or comment on the main lines of the code? I’d make it easier to help you

  • Luiz, see if it’s easy to understand my doubt now. Please, if it’s not clear tell me that I edit again.

  • It was clear, yes, alias I’m tampering with your code now, I already have some answers to your questions, the problem is that the comparison gab[i]=resp[c] is returning false and I am not getting into the sum loop, you are managing to perform this part?

  • Here it’s working, yeah.

  • I’ll clear your doubts

3 answers

2

Note that here:

 Se (gab[i]=resp[c]) entao

variable i has the value it had at the end of the feedback loop. I think it should be:

 Se (gab[c]=resp[c]) entao

You need to assign an incial value to each position of the note vector, for example nota[a] <- 0 right after reading the name.

You recalculate the average for each student and don’t calculate the class average, just print the calculation of the last student.

Var
    i,a,c: inteiro    
    resp,gab: vetor[1..5] de caractere   
    nome: vetor[1..3] de caractere
    nota: vetor[1..3] de real
    m,soma: real
Inicio
    Para i <- 1 ate 5 faca
        Escreva("Digite o gabarito ", i)
        Leia(gab[i]) 
    FimPara
    soma <- 0
    Para a <- 1 ate 3 faca
        EscrevaL("ALUNOS ", a)
        Escreva("Nome: ")
        Leia(nome[a])
        nota[a] <- 0
        Para c <- 1 ate 5 faca
            Escreva("Digite a questão ", c)
            Leia(resp[c])
            Se (gab[c]=resp[c]) entao
                EscrevaL(" acertou a ", c)
                nota[a] <- nota[a] + 2
                soma <- soma + 2
            FimSe
        FimPara
    FimPara 
    Para a <- 1 ate 3 faca
             EscrevaL(nome[a]: 10, nota[a])
    FimPara
    m <- soma/3
    Escreva("Média da Turma: ", m)
fimalgoritmo

1


welcome (to).

At first, to fix your code you need to change this line:

Se (gab[i]=resp[c]) entao

To:

Se (gab[c]=resp[c]) entao //mudei o 'i' pelo 'c'

Because in the first way, i was not updated, and so you always compared the answer with a value of the template.

On the question of note[a], I did not understand what you meant by

"The "sum" joins the values of every note[a] but I did not put value in it for her to receive nothing. Why does this happen?".

About why to use +2 instead of note[a] in the sum. It turns out, the way the code is, every time you put the first student’s answer to a question, it updates the value of note[a] and sum with the same value. Thus, when answering the 5 questions to the first student, the grade value[1°student] will be equal to the sum of.

When going to the second student, note[2°student] starts at zero, while sum already has the value of the score of the first student, so if you do 'sum <- sum + note[a]' when the second student hits the first question, will be added zero to the sum variable (sum <- sum + note[a], note[a] being zero).

An alternative to using 'sum <- sum + note[a]' would be to do the code as below:

    Var

i,a,c: inteiro
resp,gab: vetor[1..5] de caractere
nome: vetor[1..3] de caractere
nota: vetor[1..5] de real
m,soma: real

Inicio

Para i <- 1 ate 5 faca
        Escreva("Digite o gabarito ", i)
        Leia(gab[i])
FimPara
Para a <- 1 ate 3 faca
        EscrevaL("ALUNOS ", a)
        Escreva("Nome: ")
        Leia(nome[a])
        Para c <- 1 ate 5 faca
             Escreva("Digite a questão ", c)
             Leia(resp[c])
             Se (gab[c]=resp[c]) entao
                EscrevaL(" acertou a ", c)
                nota[a] <- nota[a] + 2

             FimSe
        FimPara
        soma <- soma + nota[a] //essa instrução está fora do 'para' que itera 'a'
FimPara
m <- soma/3 //essa linha pode ficar fora dos dois 'para'
Para a <- 1 ate 3 faca
        EscrevaL(nome[a]: 10, nota[a])
FimPara
Escreva("Média da Turma: ", m)


Fimalgoritmo

I’m not sure if I was clear, or if I answered (right) all your questions, anything, edit your question to address the other question points.

  • The code of Vinicius Girotto was exactly the way I wanted to do, but each one who responded helped me understand what I was trying to do and solved my doubts, thank you all!

  • 1

    I am glad that Ale helped you. For some reason, someone denied this answer and one another question (15 days ago), and I did not understand why. If someone noticed an error in the answers, at least say the error.

0

The "sum" adds the values of note[a] but I did not put value in "sum" for her to receive. Why does this happen?

This happens, because you put the operation + 2 in the same line, as you did not start the sum variable it is worth 0, but in the same line you assign 2 the variable, see below:

soma <- soma + 2.

It would be exactly the same as:

soma = 0 + 2

And one more question, why there is + 2 ?

Because you have 5 questions and the maximum score is 10, so if you had 10 questions the increment would be +1

Why couldn’t it be that way?

nota[a] <- nota[a] + 2

soma <- soma + nota[a]

You’re right the best way to do it is like this!

Then the note[a] goes to "sum" and it would actually keep a value ?

Again it is correct, it would save the value, and you will need both variables soma and nota[a], because they are used in different steps of your code.

I made some corrections to your code:

Inicio
// Seção de Comandos, procedimento, funções, operadores, etc... 
 Para i <- 1 ate 5 faca
        Escreva("Digite o gabarito ", i)
        Leia(gab[i])
FimPara
Para a <- 1 ate 3 faca
        EscrevaL("ALUNOS ", a)
        Escreva("Nome: ")
        Leia(nome[a])
        Para c <- 1 ate 5 faca
             Escreva("Digite a questão ", c)
             Leia(resp[c])
             Se (gab[c]=resp[c]) entao
                EscrevaL(" acertou a ", c)
                nota[a] <- nota[a] + 2
                soma <- soma + nota[a]
                senão
                 Escreval(" errou a questão: ", c)
                                       // Evite colocar apenas o se.. então, sem um  
                                      //fechamento de senão, pois se não entrar no 
                                     //primeiro laço informará o usuário em qual laço 
                                     //entrou
             FimSe
        FimPara
        m <- soma/3
FimPara
Para a <- 1 ate 3 faca
        EscrevaL(nome[a], " de 10 pontos você tirou: ", nota[a], " pontos")   
                                 //Coloquei mais texto para informar bem o usuário de 
                                 //quanto foi a nota e qual seria a nota máxima
FimPara
Escreva("Média da Turma: ", m)

Fimalgoritmo

Browser other questions tagged

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