Thread: Help solving this problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    5

    Help solving this problem

    Given the declarations:
    int n;
    char ch1;
    char ch2;

    and given that n contains a two-digit number, translate n into two single characters such that ch1 holds the higher-order digit, and ch2 holds the lower-order digit.
    For example, if n=59, ch1 would equal '5', and ch2 would equal '9'. Then output the two digits as characters in the same order as the original numbers.
    (Hint: Consider how you might use the / and % operators in your solution)

    Any ideas? Thanks.

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    . .. so the hint is given, what can you do with it?

    Code:
        int n=64;
    
        short a=(n%?)/?;
        short b=(n%?);
    Figure that out and your output is 6, 4.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    5

    ...

    thanks Oldman47, I couldn't exactly understand how you were trying to solve it but what I ended up doing worked...here is the solution in case you wanna know...
    int n;
    char ch1;
    char ch2;

    n = 27;
    ch1 = n / 10 + '0';
    ch2 = n % 10 + '0';

    cout << ch1 << ch2;

    Salem- get something better to do.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by marcelomdsc View Post
    Salem- get something better to do.
    Something better than your homework, hopefully.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well I won't be wasting any more time reading the same thing on multiple forums.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    there's even already a thread on this in this forum...from yesterday, though that person gave the impression it was not a HW assignment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-26-2006, 12:25 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 02:55 AM