I am suppose to write a program to read in 4 integers and then sort them without using arrays or loops. I have no idea where to start, this is an intro class. If someone could lead me in the right direction or start me off it would be greatly appreciated... he sent out a mass email to all students saying....
Do not use loops or arrays to solve the lab assignment. The laborious, brute force approach of:
if ( a < b && b < c && c < d)
cout << a << << b << << c << << d;
else if (.....
is really not the way to go. It is boring and does teach much. Look for other ways to approach the problem.
One way is to find the largest value, save it, and put the remaining three into new variables that can then be run through the same algorithm
Asking us for the answer is cheating! Half the fun of programming is finding creative solutions to your problems.
To give you a hint: look up "recursive algorithms". If that's not sufficient, find the Wikipedia page on sorting algorithms. Many of them can be implemented in an "unlooped" way.
I'll second Gaminic's comment but if you can't implement the recursive algorithms , similar to me :~, you could try a long series of conditionals... It is cleaner than the brute-force if-else set, but still is a brute force method.
result =(n1>n2)?n1:n2;
Result =n1 if it's greater or n2 if n1 is smaller. and for four integers you'd need... 6 or 7 of them and then a set to display in descending form the lesser of those groups. etc. but it gets the greatest two real easy.
I didn't ask for the answer, just need to be led in the right direction. we are only to ch 4 in the book which only covers up to, if then statements. I have never seen c++ in my life. recursive algorithms are in ch 17 of my book so I'm sure i would get a 0 seeing how he doesn't want us using loops or arrays which is ch. 5. This is teaching the class as if we are suppose to have already know this language and I am getting frustrated. So any hints or tips would help
Ouch... not fun. But if you are allowed if-then you should be allowed the conditional, which is just a cleaner two-case if-then statement.
Ask your professor if you are allowed to , if so then continue with the suggestions I made in the previous post, since you can't use recursive. If not then it would seem your only options are a brute-force if-then set, a long code to be certain. Or to try one of Gaminic suggestions: the sorting algorithms.
here is what i have so far. Now what would i do to put the others in correct order
# include <iostream>
using namespace std;
int main()
{
int a, b, c, d, big, other1, other2, other3;
cout << "Please enter 4 numbers: ";
cin >> a >> b >> c >> d;
if ( a > b && a > c && a> d )
{
big = a; other1 = b; other2 = c; other3 = d;
}
else
if (b > a && b > c && b > d)
{
big = b; other1 = a; other2 = c; other3 = d;
}
else
if ( c > a && c > b && c > d )
{
big = c; other1 = a; other2 = b; other3 = d;
}
else
if ( d > a && d > b && d > c )
{
big = c; other1 = a; other2 = b; other3 = d;
}
Unless our profs are pulling their assignments from some central source, I think you and I are in the same class, cashman. It is very silly how we cannot use the more "advanced"(yet simpler) methods of solving this.