Evaluate ascending order with repeating structure

Asked

Viewed 1,348 times

3

Write a program to determine if a sequence of n numbers entered by the user is in ascending order or not. The user must provide how many numbers will be typed, ie the value of n.

I’m having trouble with this code, I couldn’t quite understand how to assemble it, I did one for what I understood, but I couldn’t develop it, if anyone has a tip to edit it, or show how to do it, comments there, thanks already...

#include <stdio.h>
int main()
{
    int Vezes_Digitada, Digitada; /*Vezes que o usuario ia digitar, e quantas vezes ele digitou*/
    float num; /*Numero que usuario ia digitar pra avaliação*/

    printf("Quantos numeros voce ira digitar?");
    scanf("%d", &Vezes_Digitada);
    while(Digitada <= Vezes_Digitada) /*aqui eu queria que saisse do while quando as vezes
    que você digitou chegasse ao numero de vezes que o usuario predefiniu, ex: digitou 15, 
    e ia aparecer pra vc digitar ate atingir o limite de 15 vezes digitadas*/
    {
        printf("%d Digite o numero:");
        scanf("%f", &num);
    }
    if(num > num)
    {
        /*Eu sei que ta errado, mas a intenção era avaliar se os numeros
        esta em ordem crescente ou não, mas nao sei como fazer isso*/
        printf("Ordem crescente");
    }
    else
    {
        printf("Numeros digitados nao estao em ordem crescente");
    }
    return 0;
}

1 answer

2


To compare whether the number is greater than itself is meaningless:

if(num > num)

Because a number will never be larger than itself. You want to actually compare it to the previous number, which lacked to create a variable for this one. We also had to increase the variable Digitada within the while.

Tried to make the most of his logic, and keeping the style could do so:

#include <stdio.h>
int main()
{
    int Vezes_Digitada, Digitada = 0; //Digitada - faltava o começar em 0
    float num, ultimo_num; //ultimo_num faltava

    printf("Quantos numeros voce ira digitar?");
    scanf("%d", &Vezes_Digitada);

    int crescente = 1; //a variável para saber se é crescente

    while(Digitada < Vezes_Digitada)
    //--------------^ sem o igual que estava aqui
    {
        printf("Digite o numero:"); //tinha um %d aqui a mais
        scanf("%f", &num);

        //Se não é o primeiro e o ultimo é maior que o atual
        //então não é uma sequência crescente
        if (Digitada != 0 && ultimo_num >= num)
        {
            crescente = 0; 
        }

        ultimo_num = num; //atualiza o ultimo
        Digitada++; //e os digitados
    }


    //no fim mostra se é crescente com base na variável atualizada no laço/ciclo
    if(crescente == 1) 
    {
        printf("Ordem crescente");
    }
    else
    {
        printf("Numeros digitados nao estao em ordem crescente");
    }
    return 0;
}

See this example working on Ideone

The variable crescente was defined as int however the ideal type would be boolean, which is a type that does not exist in C. Unless you specifically need the user’s decimal values it would be more advisable to declare num as int. And the while that has would be simpler to write with a for.

Soon a better solution would be:

#include <stdio.h>
int main()
{
    int i, n , num, ultimo_num, crescente = 1;

    printf("Quantos numeros voce ira digitar?");
    scanf("%d", &n);

    for (i = 0; i < n; ++i){
        printf("Digite o numero:");
        scanf("%d", &num);

        if (i != 0 && ultimo_num >= num){
            crescente = 0;
        }
        ultimo_num = num;
    }

    if(crescente == 1) {
        printf("Ordem crescente");
    }
    else {
        printf("Numeros digitados nao estao em ordem crescente");
    }

    return 0;
}

See this latest solution also in Ideone

Alternatively you can also put a break within the if who is in the for/while to stop inserting numbers as soon as one makes a nonincreasing order.

  • if I type 1 1 1 1 , from ascending order, is there any way to fix it? and thank you so much for your help

  • @Weslleyaf Yes missed the = in the if to stay >= and not >. It was just a distraction, but I’ll edit it right away

  • Thanks, but in which if?

  • @Weslleyaf What’s inside the while/for. I’ve already edited the answer

  • Ahh got it, thanks again

  • @Weslleyaf No problem, we’re here to help.

  • Do you know any way to fix this repeat part more? I’m having a hard time with this :/

  • @Weslleyaf Fix ? What do you mean ?

  • fix, improve, learn, develop... etc

  • @Weslleyaf The best is the same as for everything, study the theory, be it by books, videos, classes, etc... And practice! Practice a lot, because without practice there is no way.

  • Thank you, and good night :)

  • because the increasing int=1 stood after the scanf? has some reason in particular, or could put that variable together in the other int?

  • @Weslleyaf O crescente has already started with 1 in its statement. The idea is that we are default to assume that the numbers are increasing and we update to 0 if we see that some are no longer.

Show 8 more comments

Browser other questions tagged

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