How to check if an array is in increasing order

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 6910 Views - Last Post: 06 March 2012 - 09:25 PM Rate Topic: -----

#1 oAllElseFailo  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,397
  • 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  Icon User is offline

  • D.I.C Head

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

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

Posted 06 March 2012 - 02:11 PM

View Postr.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  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,397
  • Joined: 27-December 05

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

Posted 06 March 2012 - 03:11 PM

View PostoAllElseFailo, 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  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 37
  • View blog
  • 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  Icon User is offline

  • D.I.C Head

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

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

Posted 06 March 2012 - 08:06 PM

View Postshurd, 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,397
  • 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  Icon User is offline

  • D.I.C Head

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

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

Posted 06 March 2012 - 08:30 PM

View Postr.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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,397
  • 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  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2014
  • View blog
  • Posts: 5,397
  • 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  Icon User is offline

  • D.I.C Head

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

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

Posted 06 March 2012 - 09:01 PM

View Postr.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