Basic c programming help

This is a discussion on Basic c programming help within the C Programming forums, part of the General Programming Boards category; I am having a problem with the creating the following program: Create a program that will read in 2 letters, ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    34

    Basic c programming help

    I am having a problem with the creating the following program:

    Create a program that will read in 2 letters, and then print the character half-way between the two. e.g. given A and G, it will print D. If there are an even number of characters between, then either of the 2 middle ones is OK

    I don't understand how I am suppose to have the program print out the character half-way between the two.

    Here is what I have.

    Code:
    int main()
    {
        char ch, ch1, ch2;
        
        printf("Please enter in two characters.\n");
        scanf("%c %c", &ch, &ch1);
    Any help is greatly appreciated.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    ASCII - Wikipedia, the free encyclopedia
    Notice that A is 65 and F is 70

    What is 70 - 65 ?
    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.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Salem View Post
    ASCII - Wikipedia, the free encyclopedia
    Notice that A is 65 and F is 70

    What is 70 - 65 ?
    Code:
    ch2 = ch - ch1;
        printf("The number half-way between the two characters your entered is %c.\n", ch2);
    ?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    OK - print this as a letter (%c)

    char c = 'A' + 5;

    Print the same as an integer (%d)
    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.

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by Salem View Post
    OK - print this as a letter (%c)

    char c = 'A' + 5;

    Print the same as an integer (%d)
    I am following you but what is the equation after the first printf?

    I have this so far

    Code:
    char ch, ch1;
        
        printf("Please enter in two characters.\n");
        scanf("%c %c", &ch, &ch1);
        printf("You entered %c and %c.\n" , ch, ch1);
    I understand your saying to have ch3 or something = - and a printf statement after that saying the middle number but what do I put as the values I am subtracting?

    Code:
    ch2 = ch - ch1;
        printf("The letter half-way between the characters you entered is %c.\n" , ch2);
    does not print out the right answer.
    Last edited by benrogers; 01-24-2011 at 01:50 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by benrogers View Post
    I am following you but what is the equation after the first printf?

    I have this so far

    Code:
    char ch, ch1;
        
        printf("Please enter in two characters.\n");
        scanf("%c %c", &ch, &ch1);
        printf("You entered %c and %c.\n" , ch, ch1);
    I understand your saying to have ch3 or something = - and a printf statement after that saying the middle number but what do I put as the values I am subtracting?

    Code:
    ch2 = ch - ch1;
        printf("The letter half-way between the characters you entered is %c.\n" , ch2);
    does not print out the right answer.
    I am curious as to why you think subtracting is involved. How do you find the midpoint of two numbers?

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by tabstop View Post
    I am curious as to why you think subtracting is involved. How do you find the midpoint of two numbers?
    ahhhhh

    Code:
        char ch, ch1, ch2, sum;
        
        printf("Please enter in two characters.\n");
        scanf("%c %c", &ch, &ch1);
        printf("You entered %c and %c.\n" , ch, ch1);
        sum = ch + ch1;
        ch2 = sum / 2;
        printf("The letter half-way between the characters you entered is %c.\n" , ch2);
    it produces a number not a letter.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    But that only works when your baseline is zero.

    n = ch - 'A';

    Then do some maths

    Then do x + 'A' to get back to letters.
    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.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Salem View Post
    But that only works when your baseline is zero.

    n = ch - 'A';

    Then do some maths

    Then do x + 'A' to get back to letters.
    I have no idea what this means. (Finding the average of 65 and 71 doesn't require subtracting 65 from both numbers.)

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    34
    Quote Originally Posted by tabstop View Post
    I have no idea what this means. (Finding the average of 65 and 71 doesn't require subtracting 65 from both numbers.)
    So what would I do to get the character values in order to divide them by 2?

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Literal example

    'C' - 'A' is 2
    'E' - 'A' is 4

    ( 2 + 4 ) / 2 = 3

    3 + 'A' is 'D'
    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.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by benrogers View Post
    So what would I do to get the character values in order to divide them by 2?
    Nothing....

    ASCII text, like you see in a console screen is really a code, the numbers are offset by 48 the letter by 64 (caps), and 96 (lower)... where you get into trouble is if someone hands your program z and 9 ... not only are these in reverse order per the code, they are at different offsets... so your average might produce an @ sign or something stranger.

    Now for a beginner's program my thought would be to get it to work *at all* then worry about all the complications that come along... But at some point you will have to figure out how to confine both letters entered to the same group.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Literal example

    'C' - 'A' is 2
    'E' - 'A' is 4

    ( 2 + 4 ) / 2 = 3

    3 + 'A' is 'D'

    Oddly enough...

    C = 67 and E = 69...

    67 + 69 = 136... 136/2 = 68

    D is # 68 in the code.

    So... 'C' <-> 'E' = 'D';

    This is one case where conversion into and out of ASCII isn't required.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Programm giving Error
    By dharmil007 in forum C Programming
    Replies: 16
    Last Post: 05-27-2010, 02:01 PM
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 07:41 PM

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