Solved

Sequence of input numbers

Posted on 2011-03-05
13
739 Views
Last Modified: 2012-05-11
Hello you more experienced programmers,
I am learning c++ programming on my free time, and now I have the following problem:
User is prompted to enter five numbers. The program should determine the highest value and lowest value of user’s input. Additionally, the program should determine the sequence of input numbers (i.e. ascending order, descending order, or no order).  Everything else is working fine but no matter in which order I input the numbers the message displays, “ Numbers are in ascending order”.  Any advice?

#include <iostream>

using namespace std;

int main()
{

int userInput; //number from user
int lowest_value = 9999999; 
int highest_value = 0;

bool ascending = true;
bool descending = true;

for(int i=1;i<=5; i++) //loop 5 times
{
cout<<"Enter your number:  ";
cin>>userInput;
//determine lowest and highest values
 if ( userInput < lowest_value )
 {
  lowest_value = userInput;
 }
 if ( userInput > highest_value )
 {
  highest_value = userInput;
 }
//determine if numbers are in ascending order,in descending order or in no order
    if(i <= i-1 && ascending == 1) {
            ascending = 0;
        }
    if (i >= i-1 && descending == 1){
        descending = 0;
    }
} // end loop
 cout <<"Lowest number is  " <<lowest_value <<endl;
 cout <<"Highest number is  " <<highest_value <<endl;

if (ascending == 1){
  cout <<"Numbers are in ascending order" <<endl;
}
else if (descending == 1){
  cout <<"Numbers are in descending order" <<endl;
}
else{
    cout <<"Numbers are in no order" <<endl;
}
return 0;
}

Open in new window

0
Comment
Question by:Mathilda10
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35042778
Isn't this the same question as here :

        http://www.experts-exchange.com/Programming/Languages/CPP/Q_26860961.html

If not, what's different ? What is it that you need advice about ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 35042918
i <= i-1 is always false
and i >= i-1 is always true.
neither are affected at all by userInput
0
 

Author Comment

by:Mathilda10
ID: 35042951
Infinity08,
this is not the same exercise. I need advice on how to determine the sequence of user's input numbers (i.e. if the numbers are in ascending, descending, or in no order).
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35042957
>> I need advice on how to determine the sequence of user's input numbers (i.e. if the numbers are in ascending, descending, or in no order).

If you compare each entered value with the previous entered value, you can see if it's smaller, bigger or the same. If for each entered value, it is smaller than the previous one, then the numbers are descending. If for each entered value, it is bigger than the previous one, then the numbers are ascending. Otherwise the numbers have an indetermined ordering.
0
 
LVL 2

Expert Comment

by:preraksheth
ID: 35043006
You are probably using wrong variable in deciding whether ascending or descending. Use userInput instead of i to decide the order. It should work
0
 

Author Comment

by:Mathilda10
ID: 35043079
Thank you preraksheth!
I changed part of the code as:
//determine if numbers are in ascending order,in descending order or in no order
if (i>1);
    if(userInput <= i-1 && ascending == 1) {
            ascending = 0;
        }
    if (userInput >= i-1 && descending == 1){
        descending = 0;
    }
    i+1;

and now the program displays the correct message for numbers that are in ascending order and for numbers that are in no order. However, when I enter the numbers is descending order, the message displays "Numbers are in no order." Any advice?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 35043091
Try numbers greater than 5 in descending order
0
 
LVL 84

Expert Comment

by:ozo
ID: 35043102
or try numbers greater than 5 in ascending order, and numbers less than 0 in descending order
0
 
LVL 2

Accepted Solution

by:
preraksheth earned 500 total points
ID: 35043142
Mathilda10,

You still have a problem with the code.

Try the following
- Remove your current checking for ascending and descending checks
- use the following logic instead
- add a new variable noOrder, initialize to 0

 if ( userInput < lowest_value )
 {
  lowest_value = userInput;
  // added lines
  if(ascending == 0 && noOrder == 0) {descending = 1;}
  else {noOrder = 1;}
 }
 if ( userInput > highest_value )
 {
  highest_value = userInput;
  // added lines
  if(descending == 0 && noOrder == 0) {ascending = 1;}
  else {noOrder = 1;}

 }
0
 

Author Comment

by:Mathilda10
ID: 35043472
Thank you, I will change my code.
0
 

Author Comment

by:Mathilda10
ID: 35045754
I did change the code as you suggested but am still gettig the wrong displays. Maybe the reason for it is the last part of my code:
if (ascending == 1){
  cout <<"Numbers are in ascending order" <<endl;
}
else if (descending == 1){
  cout <<"Numbers are in descending order" <<endl;
}
else{
    cout <<"Numbers are in no order" <<endl;
}

I  guess I need to modify  the above code and ,hopefully, will get the right results.

0
 
LVL 84

Expert Comment

by:ozo
ID: 35045804
You are making it unnecessarily  complicated.
A descending sequence will change lowest_value each time.
So a non descending sequence will leave lowest_value unchanged at least once.
0
 

Author Comment

by:Mathilda10
ID: 35046187
Yes, you are right ozo: I think I make this “unnecessarily complicated”.  And I think that I should not try to learn C++ on my own; I should sign up for a C++ class. I understand  the rules for the expert's postings: you guys are not allowed to help people with their school homework/projects. Since I am not asking any advice with my school assignments,  I think I am in a wrong forum.  I talked with my friend who is experienced with C++ and he pointed me to the right direction (free of charge).
Thank you all for your comments and advice, I really appreciate your time and effort.

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now