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

Hey imran I finally figured out how to do that program assignment, give me about ten minutes I will post the solution here.

OpenStudy (bahrom7893):

I mean I will post the program and have you guys test it.

OpenStudy (anonymous):

gj, I figure my own way but that used a little bit more than mere if else

OpenStudy (bahrom7893):

LOL I am using 24 if-elses haha

OpenStudy (bahrom7893):

11 more left..

OpenStudy (anonymous):

My idea was finding min of a,b,c,d and assigning it to a finding min of b,c,d and assigning it to b finding min of c,d and assigning it to c

OpenStudy (bahrom7893):

hmm nice... How woud you do that? I thought of that, but i just brushed it off saying whatever..

OpenStudy (anonymous):

Using some nested if statement and a function.No loops, array needed

OpenStudy (bahrom7893):

ohh 7 more left.. arghhh

OpenStudy (anonymous):

I am going to be taking assembly language this fall which is supposed to be fun

OpenStudy (bahrom7893):

3 to go!!! lol I am minoring in programming now.. i decided it is sort of fun.. easier than math at least

OpenStudy (anonymous):

Mathematical Computer Science related to math since it is problem solving using programming

OpenStudy (bahrom7893):

Okay done. here's the program.

OpenStudy (anonymous):

First test passed

OpenStudy (bahrom7893):

1234 1243 1324 1342 1423 1432 2134 2143 2314 2342 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 Can you test all of these numbers?

OpenStudy (bahrom7893):

Because the conditions are all dependant on the permutations

OpenStudy (anonymous):

How does it handle repeat?

OpenStudy (bahrom7893):

what do u mean repeat? I dunno...

OpenStudy (anonymous):

1 3 4 1

OpenStudy (bahrom7893):

oh snap it's not doing anything.. i should have done <=

OpenStudy (bahrom7893):

hold on...

OpenStudy (bahrom7893):

Okay fixed the bug, try again.. hold on let me reupload

OpenStudy (bahrom7893):

here

OpenStudy (anonymous):

That was quick

OpenStudy (anonymous):

I think it is fool proof now

OpenStudy (bahrom7893):

okay wanna see the source code?

OpenStudy (anonymous):

yea, sure

OpenStudy (bahrom7893):

lol I don't think you want to.. anyway hold on..

OpenStudy (bahrom7893):

That was soo annoying to write, simple but annoying

OpenStudy (anonymous):

I will try to simplify it

OpenStudy (anonymous):

oh, changing to =< was your fix?

OpenStudy (bahrom7893):

yea

OpenStudy (anonymous):

very nice, long but very easy to follow

OpenStudy (bahrom7893):

yea i googled all combinations of ABCD.. so I just used that

OpenStudy (bahrom7893):

http://answers.yahoo.com/question/index?qid=20081005200021AAQs3Hr Used that

OpenStudy (anonymous):

In this case using brute force seems better than using tricks

OpenStudy (bahrom7893):

yea.. oh wait.. i have to reject anything that is not an integer..

OpenStudy (bahrom7893):

ah nevermind, professor's version is worse

OpenStudy (anonymous):

when decimal is entered, it goes off

OpenStudy (bahrom7893):

yea but the requirement says only integers

OpenStudy (bahrom7893):

professor's version also fails

OpenStudy (anonymous):

you can write that when accepting input

OpenStudy (bahrom7893):

yea let me add that

OpenStudy (anonymous):

I don't understand why you didn't do what I said. Or what imran said. Just 6 ifs

OpenStudy (bahrom7893):

what do u mean? lol I just used the brute force method

OpenStudy (anonymous):

So did I.

OpenStudy (anonymous):

What I said in your last thread, or what imran said here: " My idea was finding min of a,b,c,d and assigning it to a finding min of b,c,d and assigning it to b finding min of c,d and assigning it to c "

OpenStudy (anonymous):

But my approach involve moving around value of a,b,c,d which is kind of annoying

OpenStudy (anonymous):

only a little, and he probably is doing it anyway

OpenStudy (anonymous):

No, he is just outputing a,b,c,d in different order

OpenStudy (anonymous):

oh. still our method only has 6 ifs, not 24

OpenStudy (anonymous):

power of copy/paste, lol

OpenStudy (anonymous):

also if he used a function to swap the values its not too bad. c++ has references so changing the variables is easy

OpenStudy (anonymous):

I tried function, but I don't know how global/local variable works in C++

OpenStudy (anonymous):

void swap(int &a, int &b) { int tmp; tmp = a; a = b; b = tmp; }

OpenStudy (anonymous):

then just: swap(c,d) does the right thing

OpenStudy (bahrom7893):

swap function? never heard of it..

OpenStudy (phi):

#include <iostream> using namespace std; void swap(int &x,int &y) { int tmp; tmp= y; y= x; x= tmp; } int main() { int a,b,c,d; cout << "Please enter the 1st integer: " << endl; cin >> a; cout << "Please enter the 2nd integer: " << endl; cin >> b; cout << "Please enter the 3rd integer: " << endl; cin >> c; cout << "Please enter the 4th integer: " << endl; cin >> d; if (a>b) swap(a,b); if (a>c) swap(a,c); if (a>d) swap(a,d); if (b>c) swap(b,c); if (b>d) swap(b,d); if (c>d) swap(c,d); cout << "Sorted: " << a << " " << b << " " << c << " " << d << endl; return 0; }

OpenStudy (phi):

If you haven't got to call by reference, this might be a bit mysterious.

OpenStudy (bahrom7893):

Wow, nice looks soo good!! let me look up what swap does..

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!