help new to programming

This is a discussion on help new to programming within the C Programming forums, part of the General Programming Boards category; I am completely confused on how to follow these directions and have only gotten half correct so far "Write a ...

  1. #1
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643

    help new to programming

    I am completely confused on how to follow these directions and have only gotten half correct so far

    "Write a C program that implements the following algorithm:
    Declare one variable to will store a single character (char).
    Declare two variables to store integers (int).
    Declare two variables to store real (floating point) numbers (double).
    Display appropriate prompts and get the five values from the user:
    One character (upper case), two integers and two doubles.

    Display the following information with clear messages:
    The character (displayed as a character)
    The ASCII code for the character (display the character as an integer)
    The character + 32 (add 32 to the character and display as a character)

    The two integers
    The average of the two integers (use integer arithmetic and display as an integer)
    NOTE: if the two integers are 23 and 26 the average should be 24
    The average of the two integers (use double arithmetic and display as a double)
    NOTE: if the two integers are 23 and 26 the average should be 24.5

    The two doubles
    The average of the two doubles (use integer arithmetic and display as an integer)
    The average of the two doubles (use double arithmetic and display as a double)

    The average of all four numbers (use double arithmetic and display as a double)
    NOTE: for this one display the result with exactly two decimal places, field width of eight"

    I have made it to displaying as a character What am i suppose to do next Ive tried everything, forgive my noobieness

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    can you post your code?
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    what do you mean?

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    post your C code.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    #include <stdio.h>

    int main()
    {
    char letter;
    printf(" please enter uppercase letter \n");
    scanf(" %c", &letter);

    int a, b;
    printf(" please enter an integer \n");
    scanf (" %i", & a);
    printf(" please enter integer \n");
    scanf(" %i", & b);

    double num;
    printf(" please enter double number \n");
    scanf(" %lf", &num);



    }

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    first, since your'e reading only a character. just use getchar(). Moreover, check out isupper() to check whether the input character is uppercase.
    Code:
    letter = getchar();
    You have to to read in TWO double numbers as the assignment says;
    to print the character just use %c, but you want ascii code as well, which is integer.
    Code:
    printf("%c",letter); // to print the input letter
    printf("%d,letter); // to print the ASCII code.
    The average of the two integers (use double arithmetic and display as a double)
    to do that you have to type cast your integer into double.
    Code:
    (double) a ; // cast int a into double; 
    (double) b; // cast int b into double;
    Last edited by nimitzhunter; 01-12-2011 at 02:04 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    thanks, but what goes before (double) anything>? or just start new line like that/

  8. #8
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    460
    (double) is just the type cast. you can do everything in printf;
    Code:
    printf(" average int using double arith : %lf", ((double) a + (double) b) /2);
    or you can create a new variable if you like
    Code:
    double double_a = (double) a; 
    double double_b = (double) b;
    Last edited by nimitzhunter; 01-12-2011 at 02:20 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    25
    . . . I have made it to displaying as a character What am i suppose to do next . . .
    I think the point of that exercise is to show that a variable can be displayed in a printf as a character, an integer or a double value.

    Reference: K&R C, ANSI C, "The C Programming Language," p.244, Appendix B, Standard Library, Table B-1.

    For example, let's say you've created a situation where the user has responded to your prompts, and now variable c is equal to "A".

    That ascii character, as a character, is "A". As an integer, it will have a numerical value. This number value will probably be 65.

    Reference: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    The ascii character "a", the lowercase value, has an integer value of 97. (See chart).

    So, by answering those questions, you would probably be going through a pattern where you: have a character input, assigned to a variable, report that variable's value as a character in a printf, report that variable's value as an integer in a printf, then add the 32, report those values, showing that you can change the letter by changing the integer value.

    Some key ideas to getting this done will include:
    knowing the modulo, the "%d" for decimal or integer reporting;
    knowing the modulo, the "%c" for character reporting;
    knowing how to type some arithmetic into the code.

    Take a look at this example, which applies different types of PrintF conversions to a variable, x.

    Notice how the percent signs and letters after them specify what kind of reporting we want from that variable.

    Code:
    #include <stdio.h>
    main ()
    {
    
            int x;
    
            x = 65;
    
            printf ("Printing the integer value of x: %d \n", x);
            printf ("Printing the character value of x: %c \n", x);
    
            x += 32;
    
            printf ("Printing the integer value of x with 32 added: %d \n", x);
            printf ("Printing the character value of x after adding 32: %c \n", x);
    
            return 0;
    
    }
    I compiled and ran that program, and then I received this result:

    Code:
    Printing the integer value of x: 65 
    Printing the character value of x: A 
    Printing the integer value of x with 32 added: 97 
    Printing the character value of x after adding 32: a
    This works because the value of a variable can be interpreted in a variety of ways. It can hold and use its value as it was declared; and, it can use more restrictive subsets of that kind of variable. That is, you can always limit more, but you can't easily and correctly expand beyond the initial declaration.

    An int, integer variable, can hold integers. But a char, character variable, is the value of one letter only; so, an int can hold the value of a char; picking what types of variables to use is usually a lesson that comes a little later. It looks like this lesson is about printf; so, it may be more to your advantage to concentrate on the "%d" or "%c" aspects of using that printf statement.

    So, maybe you can apply that example above to solving your project problem.

    There are also some standard library "stdlib.h" utilities which do some number conversions. I don't know if those are applicable for your lesson. I think the whole thing can probably be done with variations of printf, as above. Like, when you get into averaging those doubles, I would expect to see an answer that involved declaring the variable as double, and then maybe reporting it as a float using a "%f" or something like it (e, f, g, etc.).
    Last edited by agxphoto; 01-12-2011 at 03:54 PM.

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Wow I have lots to learn thank you guys for your help and time. I will be on here for the rest of the sememster so it was nice meeting you my name is Camel-man lol, youll be hearing from me.

Popular pages Recent additions subscribe to a feed

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