Last active
February 28, 2017 21:24
-
-
Save rszewczyk/246336541ee46f52cc4ce36719d8d720 to your computer and use it in GitHub Desktop.
CMSC 104 - Classwork 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* PART 1: Replace this section with a properly formatted header | |
*/ | |
#include <stdio.h> | |
int main() | |
{ | |
/******************************************************* | |
* | |
* PART 2: | |
* | |
* Before making changes, compile the program. Then run it several | |
* times. Notice what happens to the value printed for PART 2. | |
* This is a result of using uninitialized variables. After you have | |
* run the program and observed the results, add the code needed to | |
* assign values to `firstOperand` and `secondOperand` such that the | |
* printed sum is 24. | |
*/ | |
int firstOperand; | |
int secondOperand; | |
printf("*** Begin output for PART 2 *********\n"); | |
printf("The sum is %d\n", firstOperand + secondOperand); | |
printf("*** End output for PART 2 ***********\n\n"); | |
/******************************************************* | |
* | |
* PART 3 | |
* | |
* PART 3 has a bug. It should print the area of a rectangle with a height | |
* and width as stored in the variables `rectangleHeight` and | |
* `rectangleWidth`. Find the mistake and correct it. | |
*/ | |
int rectangleHeight = 3; | |
int rectangleWidth = 12; | |
printf("*** Begin output for PART 3 *********\n"); | |
printf("The area of the rectangle is %d\n", firstOperand * secondOperand); | |
printf("*** End output for PART 3 ***********\n\n"); | |
/******************************************************* | |
* | |
* PART 4 | |
* | |
* In PART 4, declare and initialize another variable of type float that | |
* represents the radius of a circle. Then modify the second `printf` | |
* statement to use the radius and the value stored in `pi` to print the | |
* area of the corresponding circle. | |
*/ | |
float pi = 3.14159; | |
// declare and initialize a variable to hold the radius value | |
printf("*** Begin output for PART 4 *********\n"); | |
// fix the following line so that it prints the area of the circle | |
// using the radius variable that you defined along with the variable | |
// named `pi`. Hint: the `*` operator works with more than two operands | |
// (e.g. 4 * 4 * 2) | |
printf("The area of the circle is %f\n", pi); | |
printf("*** End output for PART 4 ***********\n\n"); | |
/******************************************************* | |
* | |
* PART 5 | |
* | |
* Complete the section below so that the user is prompted for the | |
* diameter of a circle and the program prints the circumference. | |
*/ | |
float diameter; | |
printf("*** Begin output for PART 5 *********\n"); | |
printf("Enter the diameter of the circle and press enter\n"); | |
// add code to get the diameter from the user | |
// modify the next line to print the circumference of the circle. Hint: | |
// you can reuse the pi variable from PART 4 | |
printf("The circumference of the circle is %f\n", 2); | |
printf("*** End output for PART 5 ***********\n\n"); | |
/******************************************************* | |
* | |
* PART 6 | |
* | |
* Run the program and examine the ouput for PART 6. Notice that the | |
* `printf` statements are printing different values for a variable with | |
* the same name. This is because variables are visible to different parts of | |
* the code depending on what block the code and variables appear in. This | |
* visibility of variables is called "scope". In short code can see | |
* variables that are defined before that particular part of code and that | |
* appear either in: | |
* a) the same block of code, or | |
* b) in a block of code containing the block in which the code appears. | |
* | |
* There is nothing to change in PART 6. Just read the explanations below, | |
* and run the program as many times as you need in order to understand | |
* what is going on. But, feel free to modify it if you think it will help | |
* you understand. | |
* | |
*/ | |
// this is the "outer" variable | |
int part6_value1 = 20; | |
printf("*** Begin output for PART 6 *********\n"); | |
// the following statement can see the outer variable because it follows the | |
// declaration and because the declaration appears in the same block of code | |
// as the statement. | |
printf("Value 1, the first time: %d\n", part6_value1); | |
// Note the start of an inner block of code | |
{ | |
// the outer variable is also visible to the second `printf` statement | |
// because it appears in a block of code that contains the block in which | |
// the statement appears. | |
printf("Value 1, the second time: %d\n", part6_value1); | |
// this is the "inner" variable | |
int part6_value1 = 5; | |
// the third `printf` statement cannot see the outer variable. This is | |
// because it appears after a declaration of the inner variable of the | |
// the same name. The inner variable in this case hides, or "shadows" | |
// the outer variable. | |
printf("Value 1, the third time: %d\n", part6_value1); | |
} | |
// the fourth `printf` statement cannot see the inner variable. This is | |
// because the inner variable does not appear in the same block of code | |
// as the statement nor does it appear in a block of code that contains | |
// the block where the statement appears. It can however see the outer | |
// variable because the statement appears after the declaration and because | |
// the declaration occurs in the same block as the statement. | |
printf("Value 1, the fourth time: %d\n", part6_value1); | |
printf("*** End output for PART 6 ***********\n\n"); | |
/******************************************************* | |
* | |
* PART 7 | |
* | |
* Be sure you understand PART 6 first | |
* | |
* Modify PART 7 by adding two `printf` statements such that the program | |
* will print the value of `part7_value1` when it is 20 and the value of | |
* `part7_value2` when it is 5. Do not modify the existing code in any way | |
* except to add a line as needed for the additional `printf` statements. | |
* Start each line with something like "The value of part7_value1 is...". | |
* End each line of output with a new line. | |
*/ | |
int part7_value1 = 20; | |
int part7_value2 = 15; | |
{ | |
printf("*** Begin output for PART 7 *********\n"); | |
int part7_value1 = 10; | |
int part7_value2 = 5; | |
} | |
printf("*** End output for PART 7 ***********\n\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment