Checking if the numbers are arranged.

Merry Christmas! I am attempting to check if the numbers provided by the user is in numerical order or not. The logic seems reasonable but then the program does not run properly. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <ctime>

using namespace std; 



int main() 
{	
	int input[9]; 
	for(int i = 0; i < 10; i++)
	{
		cin >> input[i];
	}


	for(int i = 1; i < 10; i++)
	{
		int b = i - 1; 
		if(input[b] <= input[i])
		{
			if(input[i] == input[9])
			{
				cout << "The numbers are arranged already!"; 
				return 0; 
			}
		else
			break; 
		}
	}
	cout << "The numbers are not arranged."; 
return 0; 
}
Your code formatted incorrectly:
1
2
3
4
5
6
7
8
9
10
for(int i = 1; i < 10; i++) {
    int b = i - 1; 
    if(input[b] <= input[i]) {
        if(input[i] == input[9]) {
            cout << "The numbers are arranged already!"; 
            return 0; 
        } else
            break; 
    }
}
Do you see problem now?

Additionally: int input[9]; contains 9 numbers with indices of 0-8. But you are trying to write 10 numbers and access index 9. Which is undefined behavior.

As you already using algorithm header, why not use it facilities:
1
2
3
4
5
6
7
8
9
int main()
{
    int input[10];
    for(int i = 0; i < 10; i++)
        std::cin >> input[i];
    std::cout << (std::is_sorted(input, input + 10)?
                  "The numbers are arranged already!":
                  "The numbers are not arranged.");
}
Sorry i did not understand your problem correctly but i can correct some of your mistakes.

1st of all :
1
2
3
4
#include <iomanip> // u dont need this
#include <cmath> // nor this
#include <algorithm> // not this
#include <ctime> // nor this 

Your program is good enough without that lib as well...

also
1
2
3
4
5
 if(input[i] == input[9])
			{
				cout << "The numbers are arranged already!"; 
				return 0; // u dont need return 0 if u use this that if statement will jump to the end and will terminate the whole program... 
			}


Note:How to u mean arrange them? Like 1,2,3,4,5,6... or how do u want to arrange the entered numbers.
For me to understand your problem u need to explain a bit better so i can help u
Last edited on
std::is_sorted()
I am sorry for the informality of the next two statements but,

"OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOHHHHHHH MY GOODNESS."

Thank you for your correction. I have been pulling my hair out like nobody's business.

I am very thankful for you. Have a great rest of your day.
@MiiNiPaa
As you already using algorithm header, why not use it facilities:

isnt it good to define your own than using facilities of standard libraries?
isnt it good to define your own than using facilities of standard libraries?
It is opposite of "good". Reinventing the wheel when you have already existing solution meant you are wasting your time, and your employer money, on redundand, usually subpar, potentually bugged solution.

Only reasons to do so:
1) Existing solution has flay which prevents you from utilising it in your program and you want to fix it.
2) You want to implement it as par of exploring language. As soon as you lear all this can potentually gicve you, use already existing solutions.
when learning the core language, use libraries as less as possible.
latter when you will work in projects use libraries as much as possible.
anup30 wrote:
when learning the core language, use libraries as less as possible.
I disagree.

When learning C++, learn to do things with all the facilities of C++. Don't waste time learning to do things the hard (or wrong) way first.

AFAI am concerned, telling people to avoid C++ libraries is about the same as telling people they need to learn C before learning C++.

If you want to learn C++, learn C++.

"Core language"? What's that?
Last edited on
what do you call features not library )?
All the header files you need are:
iostream
vector
algorithm

So what you have to do is:
1) Get user input
2) Make a copy of user input
3) Use a stringstream to turn the copy of user input into a string and insert every element of the string into vector foo.
4) Make this function:
bool sortAscend (int i, int j) {return i < j}
5) Use std::sort like this:
std::sort(foo.begin(), foo.end(), sortAscend)
6) Append each element in vector to a string.
7) Use a stringstream to convert string to int.
8) Compare user input with sorted input.
9) Print result!
Or you can use what Duoas suggested, std::is_sorted(), which I was not aware of.
Topic archived. No new replies allowed.