Repeat loop for [closed]

-2

My code is only printing the last case. Could anyone 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;
}

    
asked by anonymous 04.10.2018 / 21:17

1 answer

1

Dude, your variable is overwritten every time you enter the loop. In order to print the result after the iterations, you can store this 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;
}
    
04.10.2018 / 22:55