- 1
Please help with this question đ (I want it without a matrix)
Type C ++ to read 7 numbers and determine if the numbers are stored ascending or not.
12 Answers
+ 1
Simply store the number in array and loop over its length and check if arr[i]<arr[i+1] ,if not print not in ascending order
+ 1
Sorry for disturbing you.... can you help me on how to have codes... because I am a beginner here please
0
Take an input and compare it with the previous input in a for loop
0
Abhay
https://code.sololearn.com/cL7j5P8KwrWa/?ref=app
What is wrong with the program?
Why the result is always ascendingŰ
0
First you didn't stored the a into b for first Loop ,like b=a and second you didn't do i>=1 because when the loop runs second time it will compare with first value of a which is stored in b to recent value of a like a<b ,now I am looking on how to use if else statement in c++ to clearly write a code and then paste it here
0
Abhay Thanks, I hope I know my mistake
0
#include <iostream>
using namespace std;
int main(){
int i,a,b;
for(i=0;i<5;i++)
{
cin>>a;
if(i>=1 && b<a){
cout<<"ascending order"<<endl;
}
else if(b>a){
cout<<"not in ascending order"<<endl;
break;
}
b=a;
}
return 0;
}
0
Abhay
Is the problem from me? When you perform the program .
The output is only non-ascending
0
Abhay
Is the problem from me? When you perform the program .
The output is only non-ascending
0
Sorry I implemented the if Loop wrong way with && condition so it was jumping to else statement since i was 0 for first time ,just edited the code ,run it now
Also you could do with nested if as I explained in python, it is much readable
#include <iostream>
using namespace std;
int main(){
int i,a,b=0;
for(i=0;i<5;i++)
{
cin>>a;
if(i>=1){
if(b<a){
cout<<"ascending order"<<endl;
}
else{
cout<<"not in ascending order"<<endl;
break;
}
}
b=a;
}
return 0;
}
- 1
Abhay
Useful information, but if I want to solve the question without matrices , what should I doŰ
- 1
Here is something like I assume you are expecting but in python
b=0
for i in range(5):
a=int(input())
if i>=1:
if b<a:
print("ascending order")
else:
print("not in ascending order")
break
b=a
for I in range(5) is basically
for(I=0;I<5;i++)
a=int(input()) is converting string to integer input
in c++ I think it's something like
int a;
cin<<a;