Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WARM-UP PROGRAM
- /*
- * This is a simple example of how characters are treated similarly to
- * integer values and demonstrate you can use math operations on them.
- * By: Larry Snedden
- */
- #include <stdio.h>
- int main(void)
- {
- int aNum = 32;
- char aChar = 'A';
- int aNumChar = aNum + aChar;
- printf("The numerical value of 'A' is %d\n", aChar);
- printf("The value of 10 + 'A' = %d and the character is '%c'\n", aNumChar, aNumChar);
- return 0;
- }//end main
- PRACTICE PROBLEM 1
- Write a program that prompts the user for an integer input and then prints the integer
- input first as a character and then as a decimal integer and then as a float. Now looking at
- the ASCII chart enter in numbers that represent ASCII values in the chart. Did you get the
- output you expected? https://www.asciitable.com/
- /*** SKELETON CODE ***/
- #include <stdio.h>
- int main(void)
- {
- int numInput = 0; //DON'T CHANGE THIS VARIABLE DECLARATION
- // YOU START PROGRAMMING HERE
- return 0;
- }
- PRACTICE PROBLEM 2
- Write a program using only printf statements that will print a right arrow. There should be
- 5 rows and 7 columns and the characters should be an ‘X’ (you can experiment). Use new
- lines ‘\n’ to provide empty lines where needed to improve appearance. Remember that a ‘ ‘
- is a character with a fixed width just like ‘X’.
- Output:
- X
- XXXXXX
- XXXXXXX
- XXXXXX
- X
- PRACTICE PROBLEM 3
- Write a program that uses defined constants for each of the vowels: A, E, I, O, U. now use
- memory constants inside the main function to hold the even values: 0, 2, 4, 6, 8. Now
- provide 3 print statements to print a first line with the vowels, the second with the even
- values and then finally the third line should use literals to print the odd numbers under the
- positionally equivalent even numbers as shown. The objective here is to distinguish
- between a defined constants and memory constants and to practice using constants and
- variables. It should also be noted that a defined constant is not the same thing as a global
- variable which is a big “no no” in programming.
- Output:
- A E I O U
- 0 2 4 6 8
- 1 3 5 7 9
- Problem #4: Using the sizeof() operator
- Compose a program and in the local declarations area of the main function declare and initialize the
- following items:
- int var1 = 10;
- float var2 = 10.121;
- double var3 = 20.255;
- char var4 = ‘A’;
- Now, in the local statements area use the printf() function and the sizeof() operator to output the
- number of bytes of memory each variable declared contains. Addtionally, in the same printf()
- statement also output the value of the variable using the correct conversion modifier (i.e. %d, %c, etc) as well as
- using a minimum width modifier of 5.2. Observe the output for alignment and rounding.
- CHALLENGE PROBLEM
- The scanf and printf functions return values. Notice we have not made use of them thus far.
- Return values from functions are optionally used. We will cover functions in Module 5. The
- scanf function will return the number of values that are successfully read or scanned in.
- This can be used to detect a failed data read. The printf function will return the number of
- characters processed to standard output.
- Compose a program that declares 4 integer variables initialized to ‘0’. 3 of these will hold
- inputs and the 4th will hold the return value from the printf function call described below.
- Your program should use the printf function to show the number of conversions for a scanf
- call reading in the 3 integer values and assign the return value to the 4th variable. See the
- example below for using a function call as input to the printf() function call.
- Helper code:
- X = printf(“Number read %d:”, scanf(“%d%d%d”, &a, &b, &c));
- You can use simple variable names for this example program. Here is your algorithm.
- 1.) Prompt the user to input 3 numbers.
- 2.) Store the input values.
- 3.) Output the number of items read/converted/scanned.
- 4.) Output the values read and the number of characters output in step 3.
- Test the program:
- 1. Run the program by entering in 3 non decimal values and observe the results.
- 2. Run the program but enter in a decimal value for the first value. Observe the result.
- 3. Discuss the output and count the number of characters processed by the printf() statement.
- Are they correct and why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement