Receive various values and print only the 3 largest ones in c

Asked

Viewed 428 times

0

Hello, I have an activity to be done that consists of receiving values and only stop receiving them when typing -1, then the program should display the 3 highest values typed as if it were a ranking of first, second and third place. I broke my head a lot and managed to write the program, I receive all values, but at the time of printing it always shows -1 in the 3 printing houses(-1 is the command that for the loop to receive the values)I believe it is a very slight mistake that is compromising the program.Thank you for understanding. The activity describes that vectors should not be used.Before asking the name of the variables plu, slu and tlu are to represent first second and third place.

#include <stdio.h>
#include <stdlib.h>

main()
{
  int pon = 0, plu, slu, tlu;

  while (pon != -1)
  {
    printf("Insira a pontuacao dos jogadores\n");
    scanf("%d", &pon);
    plu = pon ;
    slu = pon ;
    tlu = pon ;
  }

  if (plu >= slu)   
    if (slu >= tlu)
      printf(" 1 Lugar %d\n 2 Lugar %d\n 3 Lugar %d\n",plu,slu,tlu);
    else 
      if (plu >= tlu)
        printf(" 1 Lugar %d\n 3 Lugar %d\n 2 Lugar %d\n", plu, tlu, slu);
      else
        printf(" 3 Lugar %d\n 1 Lugar %d\n 2 Lugar %d\n", tlu, plu, slu);
      else
        if (slu >= tlu)
          if (plu >= tlu)
            printf(" 2 Lugar %d\n 1 Lugar %d\n 3 Lugar %d\n", slu, plu, tlu);   
          else
            printf(" 2 Lugar %d\n 3 Lugar %d\n 1 Lugar %d\n", slu, tlu, plu);
        else
          printf(" 3 Lugar %d\n 2 Lugar %d\n 1 Lugar %d\n", tlu, slu, plu);

  return 0; 
}
  • It would be good to even your code, it would make it very easy to read. From what I see, your loop does nothing, when the user type -1 and stop, the value assigned to the variables plu, slu, tlu will always be -1 why was the user’s last entry

  • The way you did the result will be -1, -1 and -1. When reading a new score you have to check if it fits into one of the 3 maximum scores and scroll below them by placing it in its proper place. Do not need to do this bunch of tests at the end, just print the 3 values.

  • regarding: main() there are only two valid signatures for the main () They are: int main (void) and int main (inc argc, char * argv [])

4 answers

1


  1. first mistake:

the reason you are always giving -1 is that Voce le option then stores in the variables and only after storing or not in them will be checked if it is -1 or n, then the variables will always be the last number that was typed,

  1. second mistake:

while you are assigning the same value to all placements, you have to make checks to see if they are larger or not than the values previously assigned

    #include <stdio.h>
    main() 
    { 
        int pon = 0,
            plu = 0,
            slu = 0,
            tlu = 0;
        while (pon!=-1)
        {
            if(pon >= plu)
            {
                tlu = slu;
                slu = plu;
                plu = pon;
            }
            if(pon >= slu && pon < plu)
            {
                tlu = slu;
                slu = pon;
            }
            if(pon >= tlu && pon < slu)
                tlu = pon;
            printf("Insira a pontuacao dos jogadores\n");
            scanf("%d", &pon);
        }
        printf("%d | %d | %d \n", plu, slu, tlu);
    }
  • This solution has an error. Once equal values are entered they will be assigned to the same player, leaving the remainder with 0. Example: input: 2, 1, 1, -1 --> saída: 2, 1, 0 (erro). In this case there were two players with score 1, but the solution presents only one.

  • 1

    @alandplm really had considered that all would be different but had not put the >= or inves only of the > for equal entries, corrected

0

The main error in your program is that in the main loop it appeals reads the input (Pon) and always assigns it to the plu, slu and tlu variables. Therefore, the last read value (-1) will always be stored in the three variables. Comparisons have to be performed within the loop to know what are the three highest values.

The above @tredeneo solution has an error. Once only equal values are entered they will all be assigned to the same player leaving the rest with 0. Ex:

entrada: 1, 1, -1  --> saída: 1, 0, 0 (erro)
entrada: 2, 1, 1, 1, 1, 1, 1, 1, -1 --> saída: 2, 1, 0 (erro)

Below is a version I made of the program.

#include <stdio.h>

int main(int argc, char *argv[])
{
    int entrada, x1, x2, x3;
    x1 = x2 = x3 = 0;

    do {
        printf("Insira a pontuacao dos jogadores\n");
        scanf("%d", &entrada);

        if (entrada == -1) {
            break;
        } else if (entrada > x1) {
            x3 = x2;
            x2 = x1;
            x1 = entrada;
        } else if (entrada > x2 && entrada <= x1) {
            x3 = x2;
            x2 = entrada;
        } else if (entrada > x3 && entrada <= x2) {
            x3 = entrada;
        }
    } while (entrada != -1);

    printf(" 1 Lugar %d\n 2 Lugar %d\n 3 Lugar %d\n", x1, x2, x3);
    return 0;
}

0

Implementation with scanf testing.

#include <stdio.h>

int main(void)
{
  int n, pon, plu = -1, slu = -1, tlu = -1;

  do
  {
    printf("*\n");
    printf("* pontos (-1 para fim): ");

    n = scanf("%d", &pon);
    scanf("%*[^\n]");

    if (n != 1)
    {
      printf("* entrada de dados invalida!\n");
      pon = 0;
    }

    else if (pon > plu)
    {
      tlu = slu;
      slu = plu;
      plu = pon;
    }

    else if (pon > slu)
    {
      tlu = slu;
      slu = pon;
    }

    else if (pon > tlu)
    {
      tlu = pon;
    }

    printf("* plu=%d slu=%d tlu=%d\n", plu, slu, tlu);
  } while (pon != -1);

  printf("*\n");
}

Testing:

[~/Projects/testes/so]
$./383228
*
* pontos (-1 para fim): 12
* plu=12 slu=-1 tlu=-1
*
* pontos (-1 para fim): 5
* plu=12 slu=5 tlu=-1
*
* pontos (-1 para fim): 7
* plu=12 slu=7 tlu=5
*
* pontos (-1 para fim): v
* entrada de dados invalida!
* plu=12 slu=7 tlu=5
*
* pontos (-1 para fim): 6 7777777777777777777
* plu=12 slu=7 tlu=6
*
* pontos (-1 para fim): -1
* plu=12 slu=7 tlu=6
*

[~/Projects/testes/so]
$

0

Initialize with an invalid value (I used -1) and at each read check if it is one of the first three, move the smallest and put it in place.

#include <stdio.h>

int main() {
    int pon = 0, plu=-1, slu=-1, tlu=-1;
    printf("Insira a pontuacao dos jogadores [-1 para encerrar]: ");
    scanf("%d", &pon);
    while (pon != -1) {
        if (pon > plu) {
            tlu = slu;
            slu = plu;
            plu = pon;
        }
        else {
            if (pon > slu) {
                tlu = slu;
                slu = pon;
            }
            else {
                if (pon > tlu) {
                    tlu = pon;
                }
            }
        }
        printf("Insira a pontuacao dos jogadores [-1 para encerrar]: ");
        scanf("%d", &pon);
    }
    printf(" 1 Lugar %d\n 2 Lugar %d\n 3 Lugar %d\n", plu, slu, tlu);
    return 0; 
}
  • Thank you so much for the attention friend really helped me a lot , I was so long thinking about this problem that my head no longer worked

Browser other questions tagged

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