Help solving this problem

This is a discussion on Help solving this problem within the C++ Programming forums, part of the General Programming Boards category; Given the declarations: int n; char ch1; char ch2; and given that n contains a two-digit number, translate n into ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    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, 05:24 PM
  3. JOB: C/C++ Developer with problem solving skills
    By VoltRecruiter in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 01-25-2006, 11:25 PM
  4. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  5. Sign-up Thread: Problem Solving #1
    By ygfperson in forum Contests Board
    Replies: 15
    Last Post: 01-26-2003, 01:55 AM

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