1. ## Homework Help!!!!!

i need code that will arrange a 4 digit number into descending order, for example if...

int x;
cout<<"enter 4 digit number<<endl;
cin>>x;
*// here's where i need to have the program arrange the 4 digit number into descending order...

any help would be greatly appreciated!! thanks!!

2. Welcome to the forums bcianfrocca, As far as doing h/w, cīmon, you really need to give it a try, and when you run into a problem then ask a specific question. Hey, if you really want to be a programmer, you need to learn this stuff.
Hope that helps.
-d-

3. do u mean..

if user enter 7689

it has to be arranged as 9876

And I dont think you will get homework help here unless you put in some effort..
[/edit]

4. Heres the best way to get help..

Try to make a program.. If it doesnt work as you expected, look over your code very carefully. Go through it as your computer would saying outloud what each step is doing. If you do that and still cant figure it out, post your code, then you will get some hints on things to try.

That way your not hurting yourself by having someone do the work for you.

5. i am doing my own work, the program is alot bigger than this, but i am stuck on this part only, i've tried alot of different function calls for this, and nothing is working... i just need to get over this little hiccup and proceed with the rest of the program, but i can't until i figure out how to do this...

6. Hmm, the only way that I know of, would be to have the user input it as a string. Then you could break the string up and rearrange them and print them back out.

7. that's what i thought too, but would i have to do something like this...

char mystring [1] =
char mystring [2] =
char mystring [3] =

and so on so forth...?? and if so, how would the program be able to distinguish which digit is the highest of the 4?

i.e. to arrange 3748 to 8743..?? this is confusing!!

8. Using conditionals. To be honest bcianfrocca, I doubt this is a much larger project if you do not understand the uses for conditionals. Try some code yourself.

9. You could do something like this.

Code:
```// Create a string
char num_string[4];

// get user input into the string "cin >> num_string;"

// break the string into smaller parts and convert them to ints
int num1 = atoi(num_string[0]);

// check each number keeping track of which is larger - Use some ifs and > < signs

// Create a separate string to hold the result - (you could use itoa() to convert them back to chars.

// and print them out.```
This should be enough for you to make a nearly running program. I dont want to give you complete code because you really need to figure out the majority for yourself.

10. thanks for the help guys, but you all don't have to be so mean about it, this is only my 2nd semester in C++ and i'm new to all this, take it easy on me, geesh...

11. , I wasnt mean about it. Er, I didnt mean to come across mean rather.

We are saying "Do it yourself" not in a mean way. We say it because it wouldn't help you at all if we gave you the answer. It would hurt you actually.

I know it sounds mean but since its internet its hard to tell the posters real intentions.

Just know that I wasnt mean at all

12. I really don't mean this in a mean way, so I apologize if you are offended by this, but I am trying to help you. People on this board get very ........ed of when people do several things:

1) Come to us with homework, or insult our inteligence (our egos are feeble as it is) by saying it's not homework when it's Sunday night, you just joined the board, and you offer no evidence otherwise. In short, when people expect us to do the work for them.

2) Don't use code tags (you didn't do a huge job of ignoring this one, but just for future reference)

I would suggest reading the announcements and stickies at the top of the forum. They'll explain all of this in detail.

13. >but you all don't have to be so mean about it
Here, I'll be nice and give you the entire solution:
Code:
```#include <cctype>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <string>

bool is_valid ( std::string num )
{
typedef std::string::const_iterator c_iter;

if ( num.size() != 4 )
return false;

for ( c_iter it = num.begin(); it != num.end(); ++it ) {
if ( !isdigit ( static_cast<unsigned char> ( *it ) ) )
return false;
}

return true;
}

int main()
{
std::string num;

std::cout<<"Enter a 4 digit number: ";

if ( std::getline ( std::cin, num ) ) {
if ( is_valid ( num ) ) {
std::sort ( num.rbegin(), num.rend() );
std::cout<< num <<std::endl;
}
else {
std::cerr<<"Invalid input"<<std::endl;
return EXIT_FAILURE;
}
}
else {
std::cerr<<"Input error"<<std::endl;
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}```
Of course, I can't guarantee that if you turn it in your teacher will accept it, this being only your second semester and all. So you will still have to do your own work, this code is designed to give you a starting point. And it's designed to get you a failing grade if you turn it in based on some subtle techniques that require advanced knowledge of C++. So unless you learn enough to know what I did and why and can explain it in detail if approached with a plegiarism accusation, I suggest you study the code for concepts and use those concepts to work through your own solution.

14. ## here, this should do it, only 2 lines =) of code

good old fashoned c code:
Code:
```#include <stdio.h>

void main(void)
{
char char1,char2,char3,cahr4;
scanf("%c%c%c%c",char1,char2,char3,char4);
printf("%c%c%c%c",char4,char3,char2,char1);
}```

15. Lol, Prelude, how do you do that...

You gave the whole answer.. yet.. you didnt. Amazing.