Ask your own question, for FREE!
Mathematics 17 Online
OpenStudy (bahrom7893):

Programmers/mathematicians, I need help with C++ assignment!

OpenStudy (anonymous):

?

OpenStudy (bahrom7893):

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; }

OpenStudy (bahrom7893):

Now that sorts the first 3 integers in ascending order correctly, but I can't figure out the algorithm for the fourth one..

OpenStudy (bahrom7893):

imran got any ideas?

OpenStudy (bahrom7893):

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

OpenStudy (anonymous):

What couldn't you figure out?

OpenStudy (bahrom7893):

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.

OpenStudy (bahrom7893):

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

OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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; } }

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

You can do it with a while loop, though I'm not sure if that really saves you much of anything.

OpenStudy (anonymous):

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.

OpenStudy (bahrom7893):

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!!!

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

Did he say no while loops or only if-else?

OpenStudy (anonymous):

They can use while since they've learned it, but without using arrays, loops don't help much.

OpenStudy (anonymous):

I am thinking about using recursion but that's still a loop

OpenStudy (bahrom7893):

Here's the assignment. Problem 2

OpenStudy (anonymous):

no recursion then

OpenStudy (bahrom7893):

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

OpenStudy (anonymous):

is 'a' intended to be the largest?

OpenStudy (bahrom7893):

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

OpenStudy (anonymous):

It does matter.

OpenStudy (bahrom7893):

it does? So what do u want me to tell it? How does the program know a is the largest though..

OpenStudy (anonymous):

Where do you intend for the largest number to be when you're algorithm is complete?

OpenStudy (anonymous):

s/you're/your/

OpenStudy (bahrom7893):

In the end

OpenStudy (bahrom7893):

I want it to order lowest to highest

OpenStudy (bahrom7893):

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

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

ack.

OpenStudy (bahrom7893):

yes I did all that, I'm telling ya the code works for any three numbers

OpenStudy (anonymous):

if b < a rather you swap them

OpenStudy (bahrom7893):

no im done with all that, what do I do for the last one, for number d..

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

if c > d: swap them.

OpenStudy (bahrom7893):

oh let me try hold on..

OpenStudy (bahrom7893):

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

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!