Skip to content

Instantly share code, notes, and snippets.

@rszewczyk
Last active February 13, 2018 22:21
Show Gist options
  • Save rszewczyk/58c217f74a53a6426a21c8d181bc9fd1 to your computer and use it in GitHub Desktop.
Save rszewczyk/58c217f74a53a6426a21c8d181bc9fd1 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main()
{
/*******************************************************
*
* PART 1: Review of the assigment operator
*
* The assigment operator, symbolized with '=' stores
* a value in a variable. The statement `int my_value = 3`
* stores the value 3 in the integer variable named `my_value`.
*
* You can assign a single value to a variable, as in the above. Or
* you can assign the result of some calculation like `3 + 5`. In
* this case `3 + 5` is called an expression, a piece of code
* that results in a value.
*/
/*
* One subtle aspect of assigment is what occurs when you assign
* a value of one type to a variable of a different type. In C
* the value is converted implicitly (without the programmer
* having to anything extra). When attempting to store a floating
* point value in an integer variable this can result in a loss of
* information. Observe what occurs when you assign the value 3.3
* to an integer variable.
*
* Note that is is not the same as rounding. The value gets
* truncated - everything after the decimal point is dropped.
*/
double part1_assigment1 = 3.3;
int part1_assignment2 = 3.3;
printf("*** Begin output for PART 1 *********\n");
printf("Value 1: %lf\n", part1_assigment1);
printf("Value 2: %d\n", part1_assignment2);
printf("*** End output for PART 1 ***********\n\n");
/*******************************************************
*
* PART 2: Arithmetic Operators
*
* An arithmetic operator is a symbol used to perform
* numerical calculations (e.g. addition and subtraction)
*
* Operators use operands to calculate their results.
* All arithmetic operators that we will use in this
* course are binary operators in that they operate
* on two operands.
*/
/*
* The following statement assigns the result of 4 + 5 to a
* variable. The symbol '+' is the addition operator. 4 and 5
* in this case are the operands.
*/
int part2_addition = 4 + 5;
printf("*** Begin output for PART 2 *********\n");
printf("4 plus 5 equals %d\n", part2_addition);
printf("*** End output for PART 2 ***********\n\n");
/*******************************************************
*
* PART 3: Arithmetic and data types
*
* Arithmetic operators can operate on both floating
* point and integer values.
*
* If both operands are the same data type, the resulting
* value has the same data type as the operands.
*
* If the operands are different types, the resulting
* value has the data type of whichever operand is
* the more precise type (floating point numbers are more
* precise than integers).
*/
/*
* The following statement demonstrates that addition works
* for floating point numbers as it does integers.
*/
double part3_addition1 = 4.0 + 5.0;
/*
* It is possible to combine operands of different types
* when using arithmetic operators.
*/
double part3_addition2 = 6 + 7.0;
int part3_addition3 = 10 + 4.0;
int part3_addition4 = part3_addition2 + part3_addition3;
/*
* Note that this calculation and assignment results in
* a loss of precision. You are assigning a floating point
* value with a decimal portion to an integer.
*/
int part3_addition5 = 6 + 3.3;
/*
* As with assignment operands can be simple values or
* more complex expressions. In the following statement
* the operands for the first plus sign are 6 and the
* expression `3 + 7`.
*/
int part4_addition6 = 6 + 3 + 7;
printf("*** Begin output for PART 3 *********\n");
printf("4.0 plus 5.0 equals %f\n", part3_addition1);
printf("6 plus 7.0 equals %f\n", part3_addition2);
printf("10 plus 4.0 equals %d\n", part3_addition3);
printf("13.0 plus 14 equals %d\n", part3_addition4);
printf("6 plus 3.3 equals %d\n", part3_addition5);
printf("*** End output for PART 3 ***********\n\n");
/*******************************************************
*
* PART 4:
*
* Add code to this section to declare a variable named
* `part4sum` that stores and integer and assign it the
* the result of adding integers with values 6, 9, 2 and 13.
*/
//add code to add values and assign them to a variable of type int
printf("*** Begin output for PART 4 *********\n");
// after you have declared and initialized your variable, remove the comment from
// the next line which will print the value
// printf("part4sum equals %d\n", part4sum);
printf("*** End output for PART 4 ***********\n\n");
/*******************************************************
*
* PART 5: More arithmetic operators
*
* The other arithmetic operators that we will use in this
* class are:
*
* 1. the division operator symbolized with: /
* 2. the multiplication operator symbolized with: *
* 3. the modulo (remainder) operator symbolized with: %
* 4. the subtraction operator symbolized with: -
*
* follow the instructions in the rest of this section to
* perform the specified arithmetic calculations, store the results
* in a variable and print the result
*/
// store the result of 4.0 divided by 3.0 in a variable of type double
// named `divisionResult1`
// store the result of 4.0 divided by 3.0 in a variable of type int
// named `divisionResult2`
// store the result of 4 divided by 3 in a variable of type double
// named `divisionResult3`
// store the result of 10 modulo 3 in a variable of type int
// named `moduloResult`
// store the result of 22 minus 13 in a variable of type int
// named `subtractResult`
// store the result of 4.0 multiplied by 13.5 in a variable of type double
// named multiplyResult
printf("*** Begin output for PART 5 *********\n");
// uncomment the printf statements to print your results
// printf("divisionResult1: %lf\n", divisionResult1);
// printf("divisionResult2: %d\n", divisionResult2);
// printf("divisionResult3: %lf\n", divisionResult3);
// printf("moduloResult: %d\n", moduloResult);
// printf("subtractResult: %d\n", subtractResult);
// printf("multiplyResult: %lf\n", multiplyResult);
// printf("*** End output for PART 5 ***********\n\n");
/*******************************************************
*
* PART 6: Operator precedence
*
* In a complex expression with multiple operators, the calculations are
* performed in accordance with the precedence (which comes first) of
* the individual operators.
*
* The arithmetic operators we introduced above have the same precedence as they
* do in normal algebra (the remainder has the same precedence as multiplication/division)
* and operators with the same precedence are resolved left to right. You can also use
* parenthesis to give an operation higher precedence.
*
* follow the instructions in the rest of this section to
* perform the specified arithmetic calculations, store the results
* in a variable and print the result
*/
// Change the following assignment statement so that `part6_expression1` stores
// the result of multiplying 3 by the sum of 6 and 8 (the sum should be performed
// before multiplication).
int part6_expression1 = 0;
printf("*** Begin output for PART 6 *********\n");
printf("result: %d\n", part6_expression1);
printf("*** End output for PART 6 ***********\n\n");
return 0;
}
#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 `firstSumOperand` and `secondSumOperand` such that the
* printed sum is 24.
*/
int firstSumOperand;
int secondSumOperand;
int part2sum = firstSumOperand + secondSumOperand;
printf("*** Begin output for PART 2 *********\n");
printf("The sum is %d\n", part2sum);
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;
int rectangleArea = firstSumOperand * rectangleWidth;
printf("*** Begin output for PART 3 *********\n");
printf("The area of the rectangle is %d\n", rectangleArea);
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.
*/
double pi = 3.14159;
// declare and initialize a variable to hold the radius value
// use the `radius` and `pi` variables to calculate the area and store
// it in the `circleArea` variable. Hint: the `*` operator works with more
// than two operands (e.g. 4 * 4 * 2)
double circleArea = 2.0;
printf("*** Begin output for PART 4 *********\n");
printf("The area of the circle is %lf\n", circleArea);
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.
*/
double diameter;
printf("*** Begin output for PART 5 *********\n");
printf("Enter the diameter of the circle and press enter\n");
// remove the comment from the following line, leaving the scanf statement.
// scanf("%lf", &diameter);
// correct the following line so that it calculates the circumference. Hint:
// you can reuse the pi variable from PART 4
double circumference = 0.0;
printf("The circumference of the circle is %f\n", circumference);
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. Use the existing printf
* statements in the program as an example.
*/
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");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment