Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MYSTERY PROGRAM
- /*
- * What is happening line-by-line in this program? And why does it work?
- * Particularly, what are the format specifiers in both printf statements doing?
- */
- #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("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.
- Then, print the integer input as a character. (DO NOT CHANGE THE ORIGINAL VARIABLE'S DATA TYPE)
- Then, on the next line, print it as a decimal integer.
- Lastly, on the next line, print it as a float.
- Now, looking at the ASCII chart, enter in decimal numbers that represent ASCII characters 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 that prints an output that resembles the following with all the correct
- spaces and characters (Also there are NO variables needed. Hard code this):
- NO PARKING
- 2:00 - 6:00 a.m.
- PERMITS ENFORCED
- 8:00 - 6:00 p.m.
- PRACTICE PROBLEM 3
- Write a program that uses DEFINED constants for each of the vowels: A, E, I, O, U.
- Then use MEMORY constants (The ones that use "const") 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 1-9.
- Match your format to the output below.
- The objective here is to distinguish between a defined constants and memory constants and to practice using constants and
- variables.
- OUTPUT:
- A E I O U
- 0 2 4 6 8
- 1 3 5 7 9
- PRACTICE PROBLEM 3A
- What is the difference between a defined constant and a global variable?
- 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).
- CHALLENGE PROBLEM
- The scanf and printf functions don't just print and scan, they also 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.) Scan 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