# How to check if an array is in increasing order

• (2 Pages)
• 1
• 2

## 17 Replies - 8342 Views - Last Post: 06 March 2012 - 09:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269716&amp;s=589aad393ed21d879a3218abc2f497a5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 oAllElseFailo

• D.I.C Head

Reputation: 0
• Posts: 74
• Joined: 23-September 11

# How to check if an array is in increasing order

Posted 06 March 2012 - 01:48 PM

I have to write this code that will let the user input 10 numbers then it has to check if the user inputted those 10 numbers in increasing order and if they didn't it'll return an error statement. However I can't think how to check if the array of numbers are increasing in order or not. I'm assuming i need to put an if statement in there?

int main()
{
int userArray[10];
cout << "Please enter ten numbers: " << endl;

for(int x = 0; x<=10; x++)
{
cin >> x;
}
}

int outofOrder(int userArray[])
{
if(?)
}

ex: 1, 2, 3, 4, 5
cout << Horray increasing

else 1, 3, 6, 9, 2, 10
cout << number 2 is out of order

Something like that..

Is This A Good Question/Topic? 0

## Replies To: How to check if an array is in increasing order

### #2 jdavi134

• D.I.C Head

Reputation: 42
• Posts: 225
• Joined: 26-October 11

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 01:59 PM

well you would need to use a loop in order to do this, probably a for loop.

And then you would need to compare each individual value of the array to the one before it to make sure that it is greater than it. Then if you find one that is out of order, you print out your message. And you would print out your message by calling the spot in the array in which the value is held.

Jack
Was This Post Helpful? 0

### #3 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 02:01 PM

From the looks of that, you should probably concentrate first on getting the input into the array, then write a loop that runs through the array and simply prints all of the elements so you can verify that you're doing that much correctly.

Then, you can work on modifying it to test whether or not the elements are in order.
Was This Post Helpful? 0

### #4 oAllElseFailo

• D.I.C Head

Reputation: 0
• Posts: 74
• Joined: 23-September 11

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 02:11 PM

r.stiltskin, on 06 March 2012 - 02:01 PM, said:

From the looks of that, you should probably concentrate first on getting the input into the array, then write a loop that runs through the array and simply prints all of the elements so you can verify that you're doing that much correctly.

Then, you can work on modifying it to test whether or not the elements are in order.

Oh.. I thought that's what I did to begin with. I thought having the for loop with the cin >> x was storing those user inputted numbers into the array above. I guess it's just simply letting them input 10 numbers instead?
Was This Post Helpful? 0

### #5 shurd

• D.I.C Head

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 02:25 PM

@oAllElseFailo - Exactly, you should take a look at this it will explain to you how to use arrays, and you might understand how to get them in order too.

Edit: Just noticed you are using x to control your for and to get the input, that's wrong.

This post has been edited by shurd: 06 March 2012 - 03:18 PM

Was This Post Helpful? 0

### #6 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 03:11 PM

oAllElseFailo, on 06 March 2012 - 04:11 PM, said:

I guess it's just simply letting them input 10 numbers instead?

It's not even doing that. Look at comments in the code below.

/* In this for loop you're using x as a control variable to count the number
*  of iterations and decide when to drop out of the loop ...
*/
for(int x = 0; x<=10; x++)  // ... so you're incrementing and testing x here
{
/* and therefore you should NOT do anything that will change the
* value of x here.  Suppose the user enters "20"?  What happens when
* we get back to line 4 and test if x < = 10?
*/
cin >> x;
}

There are exceptions, but "usually" you shouldn't modify the value of the control variable inside the body of your for loop -- certainly not until you understand exactly what you're doing and are doing that for some specific reason.
Was This Post Helpful? 0

### #7 izic

• D.I.C Head

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 07:40 PM

I think I have a partial solution.

#include <iostream>
using namespace std;

int main()
{
int x, y;
int userArray[10];
int tempArray[10];

cout << "Please enter ten numbers: " << endl;
for(int x = 0; x <10; x++)
{
cin>>userArray[x];
}

/*Array printed out
cout<<" "<<endl;
for(x=0; x<10; x++)
{
cout<<userArray[x]<<endl;
}*/

for(x=1, y=2; x<10, y<10; x++, y++)
{
if(userArray[y] > userArray[x])
{
cout<<"Increasing in order"<<endl;
}

else if(userArray[y] < userArray[x])
{
cout<<"Not increasing"<<endl;
}
}

system("pause");
return 0;
}

Was This Post Helpful? 0

### #8 shurd

• D.I.C Head

Reputation: 37
• Posts: 162
• Joined: 05-February 12

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 07:55 PM

for(x=1, y=2; x<10, y<10; x++, y++)
{
if(userArray[y] > userArray[x])
{
cout<<"Increasing in order"<<endl;
}

else if(userArray[y] < userArray[x])
{
cout<<"Not increasing"<<endl;
}
}

You dont need the second if, just the else, because if the first is not higher than the second it has to be lesser.
Also you dont need to variables, you can do
if(userArray[x]>userArray[x+1])

And you are declaring a tempArray you are not using, so no need for that.
Was This Post Helpful? 0

### #9 izic

• D.I.C Head

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:06 PM

shurd, on 06 March 2012 - 07:55 PM, said:

for(x=1, y=2; x<10, y<10; x++, y++)
{
if(userArray[y] > userArray[x])
{
cout<<"Increasing in order"<<endl;
}

else if(userArray[y] < userArray[x])
{
cout<<"Not increasing"<<endl;
}
}

You dont need the second if, just the else, because if the first is not higher than the second it has to be lesser.
Also you dont need to variables, you can do
if(userArray[x]>userArray[x+1])

And you are declaring a tempArray you are not using, so no need for that.

Yeah forgot to delete the tempArray. Thanks for the corrections.
Was This Post Helpful? 0

### #10 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:22 PM

Why are you skipping the first element in your "increasing order" test?
Was This Post Helpful? 0

### #11 izic

• D.I.C Head

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:30 PM

r.stiltskin, on 06 March 2012 - 08:22 PM, said:

Why are you skipping the first element in your "increasing order" test?

I'm basically comparing the first and second elements to each other.
If element 2 is greater than element 1 then the pointers to both will increase or move up one.

This post has been edited by izic: 06 March 2012 - 08:33 PM

Was This Post Helpful? 0

### #12 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:32 PM

Test this input:

2 1 3 4 5 6 7 8 9 10
Was This Post Helpful? 0

### #13 izic

• D.I.C Head

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:53 PM

Oh so x should equal 0 and y should equal 1. I see what I did wrong.

for(x=0, y=1; x<10, y<10; x++, y++)
{
if(userArray[x] > userArray[y])
{
cout<<"Not increasing in order"<<endl;
}

else if(userArray[x] < userArray[y])
{
cout<<"increasing in order"<<endl;
}
}

Was This Post Helpful? 0

### #14 r.stiltskin

• D.I.C Lover

Reputation: 2032
• Posts: 5,435
• Joined: 27-December 05

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 08:56 PM

Correct.

Now, here's something else. Doesn't your compiler give you a warning about this expression
for(x=0, y=1; x<10, y<10; x++, y++)

Something like "left-hand operand of comma expression has no effect"?

And are there two of you (izic <--> oAllElseFailo)? Sort of like left-brain and right-brain?

This post has been edited by r.stiltskin: 06 March 2012 - 09:01 PM

Was This Post Helpful? 0

### #15 izic

• D.I.C Head

Reputation: 0
• Posts: 223
• Joined: 15-January 09

## Re: How to check if an array is in increasing order

Posted 06 March 2012 - 09:01 PM

r.stiltskin, on 06 March 2012 - 08:56 PM, said:

Correct.

Now, here's something else. Doesn't your compiler give you a warning about this expression
for(x=0, y=1; x<10, y<10; x++, y++)

Something like "left-hand operand of comma expression has no effect"?

Nope no warnings. I'm using Dev C++ by the way.

And also, there's just me. I'm just trying to improve programming skills by helping others on the forum.

This post has been edited by izic: 06 March 2012 - 09:03 PM

Was This Post Helpful? 0

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }