Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
Lab_Week2
CPS 216,
Problem Solving and Programming Techniques in C++
Saleh M. Alnaeli, Ph.D.
Spring, 2016
1
My goals
• Goals for this problem set:• write and evaluate expressions to compute numeric values
• use variables to store results of a computation into memory
• use cin to create interactive programs that read user input
• use Strings to represent and manipulate text data
2
Expressions
• Recall that cin has expressions to represent math and other computations. Expressions may use operators, which are evaluated according to rules of precedence. Every expression produces a value of a given type.
• Ex: int, double, bool, char.
• *,/,+,-,%
3
Exercise 1: Expressions (2.1)
• Write the results of each of the following expressions. If you're stuck, ask me or your neighbor
• 12 / 5 + 8 / 4 =
• 3 * 4 + 15 / 2 =
• -(1 + 2 * 3 + (1 + 2) * 3) =
• 42 % 5 + 16 % 3 =
• 2.5 * 2 + 17 / 4 =
• 4.5 / 3 / 2 + 1 =
4
Exercise 1: Expressions (2.1) Answers
• Write the results of each of the following expressions. If you're stuck, ask me or your neighbor
• 12 / 5 + 8 / 4 = 4
• 3 * 4 + 15 / 2 = 19
• -(1 + 2 * 3 + (1 + 2) * 3) = -16
• 42 % 5 + 16 % 3 = 3
• 2.5 * 2 + 17 / 4 = 9.0
• 4.5 / 3 / 2 + 1 = 1.75
5
Exercise 2: More expressions (2.1)
• Write the results of each of the following expressions.
• 2 + 6 + "cse 142" =
• "cse 142" + 2 + 6 =
• 1 + 9 / 2 * 2.0 =
• 46 / 3 / 2.0 / 3 * 4/5 =
• 50 / 9 / 2.0 + 200 / 10 / (5.0 / 2) =
6
Exercise 2: More expressions (2.1) Answers
• Write the results of each of the following expressions.
• 2 + 6 + "cse 142" = Unpredictable
• "cse 142" + 2 + 6 = Unpredictable
• 1 + 9 / 2 * 2.0 = 9
• 46 / 3 / 2.0 / 3 * 4/5 = 2
• 50 / 9 / 2.0 + 200 / 10 / (5.0 / 2) = 10.5
7
VariablesRecall that you can use a variable to store the results of an expression in memory and use them later in the program.
type name; // declare
name = value or expression; // assign a value
...
type name = value or expression; // declare-and-initialize together
Examples: double iphonePrice;
iPhonePrice = 299.95;
int siblings = 3;
cout<< "I have " << siblings << " brothers/sisters. " << endl;
8
Exercise 3: Variable declaration syntax
• Which of the following choices is the correct syntax for declaring a real number variable named grade and initializing its value to 4.0?
a. int grade = 4.0;
b. grade = 4.0;
c. 4.0 = grade;
d. grade : 4.0;
e. grade = double 4.0;
f. double grade = 4.0;
9
Exercise 3: Variable declaration syntax
• Which of the following choices is the correct syntax for declaring a real number variable named grade and initializing its value to 4.0?
a. int grade = 4.0;
b. grade = 4.0;
c. 4.0 = grade;
d. grade : 4.0;
e. grade = double 4.0;
f. double grade = 4.0;
10
Exercise 4: Variable assignment syntax
Suppose you have a variable named grade, set to 1.6:
double grade = 1.6; // uh-oh
Suppose later in the program's code, we want to change the value of grade to 4.0. Which is the correct syntax to do this?
a. grade : 4.0;
b. set grade = 4.0;
c. grade = 4.0;
d. double grade = 4.0;
e. 4.0 = grade;
11
Exercise 4: Variable assignment syntax
Suppose you have a variable named grade, set to 1.6:
double grade = 1.6; // uh-oh
Suppose later in the program's code, we want to change the value of grade to 4.0. Which is the correct syntax to do this?
a. grade : 4.0;
b. set grade = 4.0;
c. grade = 4.0;
d. double grade = 4.0;
e. 4.0 = grade;
12
Exercise 5: Variable mutation
Suppose you have a variable named balance, set to 463.23:
double balance = 463.23
Suppose later in the program's code, we want to add 5 to the account balance. Which is a correct statement to do this?
a. balance <-- 5;
b. 5 + balance = balance;
c. balance + 5;
d. balance = balance + 5;
e. balance = 5;
13
Exercise 5: Variable mutation
Suppose you have a variable named balance, set to 463.23:
double balance = 463.23
Suppose later in the program's code, we want to add 5 to the account balance. Which is a correct statement to do this?
a. balance <-- 5;
b. 5 + balance = balance;
c. balance + 5;
d. balance = balance + 5;
e. balance = 5;
14
Exercise 6: a, b, and c• What are the values of a, b, and c after the following statements? Write
your answers in the boxes on the right.
int a = 5;
int b = 10;
int c = b;
a = a + 1; // a?
b = b - 1; // b?
c = c + a; // c?
15
answers
Exercise 6: a, b, and c• What are the values of a, b, and c after the following statements? Write
your answers in the boxes on the right.
int a = 5;
int b = 10;
int c = b;
a = a + 1; // a?
b = b - 1; // b?
c = c + a; // c?
16
answers
6
9
16
Exercise 7: Syntax errors• The following program contains multiple mistakes! What are they? Copy and
paste the following code into MSVS and correct the various mistakes. • I will provider the answer in 5 minutes. Work in pairs 1. int main(){
2. int x;
3. cout<< "x is" x<<endl;
4.
5. int x = 15.2; // set x to 15.2
6. cout<<"x is now :" + x<<endl;
7.
8. int y; // set y to 1 more than x
9. y = int x + 1;
10. cout<<"x and y are " + x + and + y<<“\n”;
11. }17
Exercise 8: i, j, and k
• What are the values of i, j, and k after the following statements?
• int i = 2;
• int j = 3;
• int k = 4;
• int x = i + j + k;
• i = x - i - j; // i?
• j = x - j - k; // j?
• k = x - i - k; // k?
18
answers
Exercise 8: i, j, and k
• What are the values of i, j, and k after the following statements?
• int i = 2;
• int j = 3;
• int k = 4;
• int x = i + j + k;
• i = x - i - j; // i?
• j = x - j - k; // j?
• k = x - i - k; // k?
19
answers
4
2
1
Exercise 9: Equation
• Suppose you have a real number variable x. Write a c++ expression that computes a variable named y storing the following value:
y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2
• Note: (We haven't learned a way to do exponents yet, but you can simulate them using several multiplications.)
20
Exercise 9- Example codeCopy/paste this program into MSVS to test your solution. // expected output:// y is 7043.7// y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2
Int main(){double x = 5;
double y = put your expression for y here ;cout<<"y is " << y<<endl;
}
21
Exercise 9- Example codeCopy/paste this program into MSVS to test your solution.
// expected output:
// y is 7043.7
// y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2
Int main(){
double x = 5;
double y = 12.3*x*x*x*x - 9.1*x*x*x + 19.3*x*x - 4.6*x + 34.2;
cout<<"y is " << y<<endl;
}
// Refactoring , source code improvement with same outputs
22
Exercise 9- Example codeCopy/paste this program into MSVS to test your solution.
// expected output:
// y is 7043.7
// y = 12.3x4 - 9.1x3 + 19.3x2 - 4.6x + 34.2
Int main(){
double x = 5;
double y = (((12.3 * x - 9.1) * x + 19.3) * x - 4.6) * x + 34.2;
cout<<"y is " << y<<endl;
}
23
Exercise 10: Birthday variables
• Create a complete C++ program that declares four variables and assigns appropriate values to them. • your birthday month (1-12)
• your birthday day (1-31)
• the birthday month of another student sitting near you today (1-12)
• the birthday day of that same student near you (1-31)
• Ask your neighbor (if any ) for their name and for the proper numbers to store in the variables for his/her birthday. Then produce output in this format using your four variables:
My birthday is 9/19, and Suzy's is 6/14.
24
Exercise 11: Syntax errorsThe following CPP program has multiple errors. Can you find them all?
1. Int main() {
2. cout<< "Type your name: ";
3. String name;
4. cin>>”name”;
5. cout>>"You must be really awesome.";
6. int myPartPercent=2.5;
7. double myNetPart= 10000 * myPartPercent;
8. cout<<“my net part is” + myNetPart<<\n;
9. }
• Copy and paste the code into MSVS and see if you can fix the errors.25
Exercise 12: GPA
• Write a complete program MyGPA that reads a student 4 grades and prints her GPA. You can assume that all courses have equal credit hours.
• Use the cin to read user input for a student's grades
• Program’s behavior is shown below :
Please enter 4 grades? 95 89.5 94.5 100
Your GPA is 94.75
26
Exercise 13: temperature converter
• Write a complete program TempConvert that reads a temperature in Fahrenheit and prints it in Celsius.
• Formula: Celsius= (Fahrenheit-32)* (5.0/9.0) ;
• Use the cin to read user input for a temperature in Fahrenheit
• Program’s behavior is shown below :
please enter your temperature in Fahrenheit --> 37
Temperature in Celsius is 2.77778
28
What’s Coming Up Soon
• Assignment 2: Exercise 12 and 13 (slides 26 and 28).
• Will be posted this evening. Due date: Saturday 6th, 7:00 PM
1: Quiz 1, Sunday on D2L
29