Question:
Write a program to determine whether a sequence of n numbers entered by the user is in ascending order or not. The user must provide how many numbers will be entered, ie the value of n.
I'm having difficulty with this code, I couldn't understand how to assemble it, I made one for what I understood, but I couldn't develop it, if anyone has a tip to edit it, or show how I can do it, comment there, thank you in advance…
#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;
}
Answer:
Comparing whether the number is greater than itself is meaningless:
if(num > num)
For a number will never be greater than itself. You actually want to compare it to the previous number, which was missing to create a variable for this one. It was also necessary to increase the Digitada
variable inside the while
.
Trying to make the most of your logic, and keeping the style I could do it like this:
#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 crescente
variable 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 decimal values from the user it would be better to declare num
as int
. And the while
that has would be simpler to write with a for
.
So 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 last solution also on Ideone
Alternatively you can also put a break
inside the if
in for
/ while
to stop entering numbers as soon as one makes a non-ascending order.