Loop of repetition for

Asked

Viewed 70 times

-2

My code is only printing the last case. Could someone help me?

My Code...

include<stdio.h>

int main(){
    int teste,total,i;

    float percentpop,percentgeral,percentarq,percentcad,renda,pop,geral,arq,cad;

    scanf("%d",&teste);

    for(i=1;i<teste;i++){

                scanf("%d %f %f %f %f",&total,&percentpop,&percentgeral,&percentarq,&percentcad);
                pop=total*(percentpop/100);
                geral=(total*(percentgeral/100))*5;
                arq=(total*(percentarq/100))*10;
                cad=(total*(percentcad/100))*20;
                renda=pop+geral+arq+cad;
                     }
    for(i=1;i<teste;i++){
            printf("A RENDA DO JOGO N. %d E = %.2f\n",i,renda);
     }

      return 0;
}

inserir a descrição da imagem aqui

  • Face your variables are overwritten during the loop, the printing should be done at the end of each iteration, or you should guard-lás and show afterwards...

  • The statement presented in image form is not very useful, particularly for mobile devices. It would be better to put the same in the form of text facilitating the reading to all.

1 answer

1


Dude, your variable is overwritten every time it goes into the loop. To be able to print the result after the iterations, you can store it in a vector, quite simply. The size will depend on what you need, so I put a variable called MAX that controls the size of it.

#include <stdio.h>
//DEFINE TAMANHO DO VETOR
#define MAX 10
int main(){
    int teste,total,i;
    float percentpop,percentgeral,percentarq,percentcad,pop,geral,arq,cad;
    float renda[MAX];
    scanf("%d",&teste);

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

    scanf("%d %f %f %f %f",&total,&percentpop,&percentgeral,&percentarq,&percentcad);

    pop=total*(percentpop/100);
    geral=(total*(percentgeral/100))*5;
    arq=(total*(percentarq/100))*10;
    cad=(total*(percentcad/100))*20;
    //salvando cada resultado em uma posicao do vetor
    renda[i] = pop+geral+arq+cad;
}

    for(i=0;i<teste;i++) printf("A RENDA DO JOGO N. %d E = %.2f\n",i+1,renda[i]);
    return 0;
}

Browser other questions tagged

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