do I get exited with non-zero status in that pq code?

Asked

Viewed 103 times

0

#include<stdio.h>

int main(){
int i,jog,rod,placar[500],ponto=0,p,maxPonto=0,vencedor=0,rodCopia;

  scanf("%d %d",&jog,&rod);

  rodCopia = rod;

  for(i=1;i<=jog;i++){
          scanf("%d",&p);
          placar[i]+=p;
       if(i==jog && rod>0){
         rod-=1;
         i = 0;
       }

  }
       for(i=1;i<=rodCopia;i++){
         if(maxPonto < placar[i]){
           maxPonto = placar[i];
           vencedor = i;
         }

       }


     printf(vencedor); 
    return 0;
    }
  • 1

    The last printf is supposed to be printf("%d",vencedor);. What input data are you using ? What should the program do that doesn’t ?

  • Can provide an input example?

1 answer

0

The problem in its code is in the part where it starts from 1, the C language does not allow a vector to have a null zero position, so its first is, just as the second ignores the vector’s 0 position. To fix the problem start i do for as 0 and instead of letting "<=" make it smaller

Follow the tidy code

#include<stdio.h>

int main(){
int i,jog,rod,placar[500],ponto=0,p,maxPonto=0,vencedor=0,rodCopia;

  scanf("%d %d",&jog,&rod);

  rodCopia = rod;

  for(i=0;i<jog;i++){
    printf("Ok");
          scanf("%d",&p);
          placar[i]+=p;
       if(i==jog && rod>0){
         rod-=1;
         i = 0;
       }

  }
       for(i=0;i<rodCopia;i++){
         if(maxPonto < placar[i]){
           maxPonto = placar[i];
           vencedor = i;
         }

       }


     printf(vencedor); 
    return 0;
    }
  • It actually depends on the entrance to burst in jog or rod. If the AP is doing it in the hand, it will not test with large numbers, at most a few dozen, which would then rule out its answer. There is also the case that if the entrance is 100000 your solution will also break. Most likely is what @Isac pointed out in the comment, which you replicated here

Browser other questions tagged

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