|
#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: %f\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 store the result of adding |
|
* together 4 integers with values 6, 9, 2 and 13. Print |
|
* the result. |
|
*/ |
|
|
|
//add code to add values and assign them to a variable of type int |
|
|
|
printf("*** Begin output for PART 4 *********\n"); |
|
// add code to print the result here |
|
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 |
|
// store the result of 4.0 divided by 3.0 in a variable of type int |
|
// store the result of 4 divided by 3 in a variable of type double |
|
|
|
// store the result of 10 modulo 3 in a variable of type int |
|
|
|
// store the result of 22 minus 13 in a variable of type int |
|
|
|
// store the result of 4.0 multiplied by 13.5 in a variable of type double |
|
|
|
printf("*** Begin output for PART 5 *********\n"); |
|
// add code to print the results of all the calculations in this section here |
|
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; |
|
} |