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

Joemath, or anyone else who is familiar with the concept of programming.. I just need to fix this idea.. how would i do it?

OpenStudy (bahrom7893):

Okay this is what I need.. I need to allow the users to input 4 integers.. And then sort those integers in order

OpenStudy (bahrom7893):

so far I have this: #include<iostream> using namespace std; int main() { int in1, in2, in3, in4; cout << "Please enter the first integer: "; cin >> in1; cout << "Please enter the second integer: "; cin >> in2; cout << "Please enter the third integer: "; cin >> in3; cout << "Please enter the fourth integer: "; cin >> in4; if (in1<in2) cout << in1 << " " << in2 << endl; else cout << in2 << " " << in1 << endl; system("pause"); return 0; } But that's only for two integers...

OpenStudy (bahrom7893):

So is there any way I can take this line.. cout << in2 << " " << in1 << endl; and display something like.. if in3<in4 arrange them in order again just like in this case with ins1 and 2

OpenStudy (bahrom7893):

so once both pairs are arranged then i could say arrange the pairs.. is there any way to do that?

OpenStudy (bahrom7893):

like u know once the numbers in the pairs are arranged i would say arrange the pairs... wait but then it has to test if the numbers within the pairs match too..

OpenStudy (heisenberg):

there are a huge number of sorting algorithms that would be excellent to learn for this. if you're not familiar with any, i'd say start with 'bubble sort' here.

OpenStudy (anonymous):

bahrom, I'm sorry I can't help much but there is a free program that makes programming A LOT easier to manage.. it is found here http://raptor.martincarlisle.com/

OpenStudy (bahrom7893):

OH and also I'm only allowed to use if-else statements.. And heisen i know one way, that's checking every single number.. But for that i need 24 if else statements

OpenStudy (bahrom7893):

i dont want the solution i just want a tip..

OpenStudy (anonymous):

create a new variable, and use a FOR before your if condition, and the new variable will contain the greater number in the comparison that will be compared again with the FOR

OpenStudy (bahrom7893):

like what way can i follow.. and i don't know a lot about C++, this is my 2nd week of class..

OpenStudy (bahrom7893):

I don't think im supposed to use FOR, because I don't even know what it is yet... hmm wait i just saw that my prof posted some hints.. let me take a look.

OpenStudy (anonymous):

use the sleep sort it is very easy

OpenStudy (anonymous):

#!/bin/bash function f() { sleep "$1" echo "$1" } while [ -n "$1" ] do f "$1" & shift done wait

OpenStudy (bahrom7893):

lol I have no idea whatsoever what that is

OpenStudy (heisenberg):

haha that's nasty

OpenStudy (bahrom7893):

okay hold up let me read the hints lol I wanna try this on my own first

OpenStudy (heisenberg):

it's a silly and very unscalable sorting algorithm. basically for each number, it forks a new process that sleeps for the time of the number then returns itself. since all the processes fork at the same time, they return in sorted order. :p

OpenStudy (bahrom7893):

I just wrote this other huge program that calculatest taxes hahah.. imran knows..

OpenStudy (anonymous):

yes but it will work for bahrom7893 because he only has 4 elements to sort, the idea is simple: you take the first element of the array, say n, you fork a new process which sleeps n seconds then displays that number. Repeat for the next element.

OpenStudy (bahrom7893):

my professor is telling me to introduce like new numbers... and santistebanc I need to use if-else only as an assignment haha.. and anyway we didn't even do your stuff yet

OpenStudy (bahrom7893):

* i meant in the hint he gave us he said introduce new variable..

OpenStudy (anonymous):

This seems like an arbitrarily restrictive assignment. Any sane person would just use a vector/array and be done with it.

OpenStudy (anonymous):

so something

OpenStudy (bahrom7893):

Attaching the hints

OpenStudy (heisenberg):

dave, this is completely off topic here and is considered spam. you should stop.

OpenStudy (anonymous):

ok then just do it step by step, first compare the int1 with in2 and then int1 with int3, int1 with int 4, and the greatest in that step use it for another comparison

OpenStudy (bahrom7893):

Finally took forever to upload the hints.. anyone knows what my professor means? I don't feel like writing 24 if else statements..

OpenStudy (anonymous):

no you would only need to use 9 or 10 if statements

OpenStudy (bahrom7893):

no .. first compare 1 to 2, 3 and 4

OpenStudy (anonymous):

even so, why not use a loop?

OpenStudy (bahrom7893):

i dunno i think we can use a loop, depends on whether i know it.. polpak can u like write a segment here?

OpenStudy (anonymous):

hey what are you people in green labels? are you the creators of open study?

OpenStudy (bahrom7893):

moderators

OpenStudy (anonymous):

Have you learned either a for loop or a while loop?

OpenStudy (anonymous):

course you still need arrays I suppose to make the loop work well, and you probably haven't learned that either.

OpenStudy (bahrom7893):

only a while loop

OpenStudy (anonymous):

you may use this logic while not inOrder(elements) do shuffle(elements)

OpenStudy (anonymous):

Well it's not as bad as all that I think: if num1 < num2: tmp = num1 num1 = num2 num2 = tmp if num1 < num3: tmp = num1 num1 = num3 num3 = tmp if num1 < num4: tmp = num1 num1 = num4 num4 = tmp #num1 is the greatest. if num2 < num3: tmp = num2 num2 = num3 num3 = tmp if num2 < num4: tmp = num2 num2 = num4 num4 = tmp #num2 is the second largest if num3 < num4: tmp = num3 num3 = num4 num4 = tmp #num3 and num4 are sorted correctly.

OpenStudy (anonymous):

Only takes 6 checks. This is a typical 'bubble sort' algorithm, and it's pretty terrible efficiency. but it's the simplest to understand if you aren't using arrays.

OpenStudy (bahrom7893):

ok polpak tnx.. I will take a look tomorrow. My brains are dead.. lol if anyone is interested this is what i just finished.. and i had to test a lot of values so im done for the day.. even simplest math is not working haha

OpenStudy (bahrom7893):

but tnx polpak =)!

OpenStudy (anonymous):

Sure, np

OpenStudy (anonymous):

yeah you rock polpak!

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!