You need to indicate how many times the highest number has been read, but you’re not counting that

Asked

Viewed 40 times

0

I’m not finding out how many times a certain higher number has been read.

#include <stdio.h>
int main (){
    int i, num, maior = 0;

    printf ("Ao finalizar, digite '00', sem aspas, para obter o resultado.\n\n");

    i = 1;
    do
    {
        printf ("Insira um numero: ");
        scanf  ("%d", &num);

        if (num == 00)
            break;

        else
        {
            if (num > maior)
            {
                maior = num;
                i++;
            }
            else
                if (num < maior)
                printf ("Erro: Numero invalido. ");
        }

    }
    while (num != 0);

    printf ("Maior numero: %d\n", maior);
    return 0;
}
  • I’m not sure I understand the question. Do you need to count the number of times the "record" has been broken? Or count the number read until the "record" is broken?

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

1

Unless you have something that is not in the question, the invalid number does not make sense. Why would the smaller number be invalid? It can happen invalidity of the number and it would be nice to treat it, but as it is exercise I let it go, just do not take as correct for real codes.

In your account you are counting how many times the larger number has been changed, after all you are increasing a counter every time you find a new bigger one. That’s not what the question is. When exchanged for a larger number the count should start again by that time that larger number only appeared once. This is called logic. When the number is equal to the largest then then you should increment the counter. If the number is smaller it is to do nothing.

You can’t ask the person to type 00, this is not a text, 00 is the same as 0. Or even gives but then would have to ask for a text and then do the conversion, it is a lot of work. Maybe it was the case to ask for a negative number to get out of the loop, if you can a negative too, then complicates and would have to have another exit solution.

The test you were doing didn’t stop anyone from typing something negative as a valid value.

I gave an organized and simplified too.

#include <stdio.h>

int main () {
    int quantidade = 0, maior = 0;
    printf("Ao finalizar, digite '0', sem aspas, para obter o resultado.\n\n");
    while (1) {
        printf("Insira um numero: ");
        int num;
        scanf("%d", &num);
        if (!num) break;
        if (num > maior) {
            maior = num;
            quantidade = 1;
        } else if (num == maior) quantidade++;
    }
    printf("Maior numero: %d - quantidade de vezes: %d\n", maior, quantidade);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I just simplified the question so it would be more objective. The fact of having put "invalid number" refers to the proposed question, in which only positive numbers were asked. In order to make something clearer to the user, I chose to put this message. Yes, it’s just an exercise, it’s not professional at all. The goal is only to learn a little more about the world of programming. I thank you for your attention.

  • So it’s a mistake, it’s not testing if it’s positive, it’s nowhere. You didn’t simplify the question, you stopped putting the requirements, so whoever answers has to guess. See [tour] to understand how the site works and what to do when the answer solved your problem.

Browser other questions tagged

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