Homework Help!!!!!

This is a discussion on Homework Help!!!!! within the C++ Programming forums, part of the General Programming Boards category; i need code that will arrange a 4 digit number into descending order, for example if... int x; cout<<"enter 4 ...

  1. #1
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69

    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. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    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-
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  3. #3
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    do u mean..

    if user enter 7689


    it has to be arranged as 9876

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

  4. #4
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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.
    What is C++?

  5. #5
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    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. #6
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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.
    What is C++?

  7. #7
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    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. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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. #9
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    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.
    Last edited by Vicious; 09-12-2004 at 07:05 PM.
    What is C++?

  10. #10
    Shake Zula- The Mic Rula!
    Join Date
    Sep 2004
    Posts
    69
    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. #11
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    , 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
    What is C++?

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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)

    3) Don't use google (again)

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

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    8

    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. #15
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Lol, Prelude, how do you do that...

    You gave the whole answer.. yet.. you didnt. Amazing.
    What is C++?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21