Joemath, or anyone else who is familiar with the concept of programming.. I just need to fix this idea.. how would i do it?
Okay this is what I need.. I need to allow the users to input 4 integers.. And then sort those integers in order
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...
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
so once both pairs are arranged then i could say arrange the pairs.. is there any way to do that?
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..
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.
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/
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
i dont want the solution i just want a tip..
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
like what way can i follow.. and i don't know a lot about C++, this is my 2nd week of class..
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.
use the sleep sort it is very easy
#!/bin/bash function f() { sleep "$1" echo "$1" } while [ -n "$1" ] do f "$1" & shift done wait
lol I have no idea whatsoever what that is
haha that's nasty
okay hold up let me read the hints lol I wanna try this on my own first
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
I just wrote this other huge program that calculatest taxes hahah.. imran knows..
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.
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
* i meant in the hint he gave us he said introduce new variable..
This seems like an arbitrarily restrictive assignment. Any sane person would just use a vector/array and be done with it.
so something
Attaching the hints
dave, this is completely off topic here and is considered spam. you should stop.
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
Finally took forever to upload the hints.. anyone knows what my professor means? I don't feel like writing 24 if else statements..
no you would only need to use 9 or 10 if statements
no .. first compare 1 to 2, 3 and 4
even so, why not use a loop?
i dunno i think we can use a loop, depends on whether i know it.. polpak can u like write a segment here?
hey what are you people in green labels? are you the creators of open study?
moderators
Have you learned either a for loop or a while loop?
course you still need arrays I suppose to make the loop work well, and you probably haven't learned that either.
only a while loop
you may use this logic while not inOrder(elements) do shuffle(elements)
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.
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.
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
but tnx polpak =)!
Sure, np
yeah you rock polpak!
Join our real-time social learning platform and learn together with your friends!