Programmers/mathematicians, I need help with C++ assignment!
?
So Polpak, I couldn't figure out the code you gave me, but this is what I was doing: #include<iostream> using namespace std; int main() { int a, b, c, d; cout << "Please enter the first integer: "; cin >> a; cout << "Please enter the second integer: "; cin >> b; cout << "Please enter the third integer: "; cin >> c; cout << "Please enter the fourth integer: "; cin >> d; if (b<a) { int temp; temp = a; a = b; b = temp; } if (c<a) { int temp; temp = c; c = b; b = a; a = temp; } else if (c<b && c>a) { int temp; temp = b; b = c; c = temp; } cout << a << " " << b << " " << c << " " << d << endl; system("pause"); return 0; }
Now that sorts the first 3 integers in ascending order correctly, but I can't figure out the algorithm for the fourth one..
imran got any ideas?
well i gotta go will be back in about an hour, if anyone can help i would appreciate it. Note, I can only use if else statements
What couldn't you figure out?
The algorithm for sorting the last part.. U know that program is complete and it sorts 3 integers. I need it to be able to sort 4 integers.
I tried following the same pattern, like let c go to temp, let b go to c, etc.. like in the code above.. I just need it to extend to the fourth number
You need to approach this from a pseudocode perspective first. I don't think the above will work for scalability to a 4th variable. Just glancing at the code. (P.S. More tabbing please on if statements ;) ) My classes have only been in Pascal and Java, so can't tell you specific c code. I also read in your other thread that there are limits to what you are allowed to use?
I liberally borrowed this from bubblesort replacing the array with variables. I think I read you could use a while statement. Pseudocode: L1=a; L2=b L3=c L4=d; Flag=False; While (Flag != True) { If (L1<L2) { Temp = L1; L1 = L2; L2 = Temp; } If (L2<L3) { Temp = L2; L2 = L3; L3 = Temp; } If (L3<L4) { Temp = L3; L3 = L4; L4 = Temp; } If ((L1<L2) && (L2<L3) && (L3<L4)) { Flag = True; } }
Ok, basically what I did before was this: First find the largest value and place it in num1. To do that we check to see if num1 is smaller than num2, if it is, we swap it. Now for sure num1 is larger than num2. Then we check to see if num1 is smaller than num3. If it is, we swap it. Now for sure num1 is larger than num2 and num3. Then we check to see if num1 is smaller than num4 if it is, we swap it. Now for sure num1 is the largest number. Then we do the same process for num2 making sure it's the largest of num3 and num4. And then finally we check to ensure that num3 is larger than num4. Now the numbers are sorted.
You can do it with a while loop, though I'm not sure if that really saves you much of anything.
I had a similar handicap in my Java calss. They had us use a chain of ifs with no loops to compare a list. The if statement got pretty ugly at the end. Loops are your friend.
Osiris that doesn't help, I'm not supposed to use while... Oh well I guess I will have to follow polpak's method. I'm actually trying to use that. Let me try again.. oh goddd so much hw!!!
Unforunately you will need to chain some very long if-then-else statements then with a series of and/or statements. I found it difficult to keep the and/or statements clear when I did mine.
I used a bubblesort when I did mine first but my professor said they hadn't introduced it and sent me back to the drawing board.
It's possible your professor is trying to help you appreciate loops. Having to do a very manual solution for something like this will do that.
Did he say no while loops or only if-else?
They can use while since they've learned it, but without using arrays, loops don't help much.
I am thinking about using recursion but that's still a loop
Here's the assignment. Problem 2
no recursion then
Polpak, I'm trying your method.. if (b<a) { int temp; temp = a; a = b; b = temp; } see if num1 is larger than number two, then swap them
is 'a' intended to be the largest?
well it doesn't matter, I'm saying basically, accept a,b,c and d. Then I said suppose the guy enters two numbers, a and b. If b is smaller than a, then reorder the numbers
It does matter.
it does? So what do u want me to tell it? How does the program know a is the largest though..
Where do you intend for the largest number to be when you're algorithm is complete?
s/you're/your/
In the end
I want it to order lowest to highest
So basically a user enters: a, b, c, d and depending on the values of a/b/c/d I want them ordered lowest to highest
Ok then I'd start with a, make sure that if b > a you swap them. That way for sure a is lower than b. Then if c > a you swap them then for sure, a is less than c or b. etc.
ack.
yes I did all that, I'm telling ya the code works for any three numbers
if b < a rather you swap them
no im done with all that, what do I do for the last one, for number d..
Well if you already know for sure that all your previous numbers are sorted in acending order you just need to make sure that c < d otherwise swap them.
if c > d: swap them.
oh let me try hold on..
okay that didn't work... Apparently im not doing it ur way.. hold on i just got home from tennis, let me go take a shower, i will post a new thread
Join our real-time social learning platform and learn together with your friends!