Upload
joanna-ray
View
215
Download
2
Embed Size (px)
Citation preview
1
CS 1430: Programming in C++
2
Literal Values
Literal values of int 5 0 -100
Literal values of float 3.1415926 0.0 -12.5
3
Positive, Negative, and Zero
Is 0 positive or negative? 0 is neither positive nor negative.
-2 -1 0 1 2
4
Literal Values (II)
Literal values of char
‘C’
‘A’
‘D’
Literal values of string
“Enter an integer: ”
“Your Grade is ”
“A”
5
Variables// Variables can have initial valuesint num1, total = 0;
// Variables can have different values cin >> num1;
num1 = 58;
total = total + num1;
6
Symbolic Constants// Symbolic constantconst int MAX_SCORE = 60; // Must give a value// Store in memory
// Can we do this?
MAX_SCORE = 100;
cin >> MAX_SCORE;
// Error: constant cannot change its value
7
No Magic Numbers// Define symbolic constantconst int MAX_SCORE = 60;
int myScore;
myScore = 60; // Is this good?// NO!// Magic number!// Half points off!
myScore = MAX_SCORE;// Very good!
8
The Cast Functionsint num1, num2; float quotient;
cin >> num1 >> num2; //No input prompt!// 3 5quotient = num1 / num2; // what’s the value of quotient?
// How to get float quotient?quotient = float(num1) / num2;// Use cast function.// coercion
quotient = float(num1 / num2);// Integer division or float division?// Integer division!
9
The IF statementint num1, num2; float quotient;
cin >> num1 >> num2;
quotient = float(num1) / num2;// Any possible issues?
// What if num2 is zero? if (num2 != 0) quotient = float(num1) / num2;
10
The IF statementint num1, num2; float quotient;
. . .
if (num2 != 0) quotient = float(num1) / num2;
// Semantics// do computation only when num2 is not zero// the statement is skipped when num2 is zero// Syntax// condition inside ()// != for not equal// Style// on separate lines// indent 3 spaces
11
If-Elseif (num2 == 0)
cout << endl << "Cannot divide by zero!";
else
{
quotient = float(num1) / num2;
cout << endl
<< "The quotient is " << quotient << ‘.’;
}
// Semantics// do different things when num2 is zero or not// Syntax// braces for multiple statements (statement block)// == for equal// Why use “==” instead of “=”?// Style// braces on separate lines
12
If-Elseif (num2 == 0)
{
cout << endl << "Cannot divide by zero!";
}
else
{
quotient = float(num1) / num2;
cout << endl
<< "The quotient is " << quotient << ‘.’;
}
// Syntax// braces are optional for single statement
13
Statement Blockif (num2 == 0) cout << endl << "Cannot divide by zero!"; else quotient = float(num1) / num2; cout << endl << "The quotient is " << quotient;
// Will the program do output when num2 is 0?// YES!// Same as the following:if (num2 == 0) cout << endl << "Cannot divide by zero!"; else quotient = float(num1) / num2;
cout << endl << "The quotient is " << quotient;
// Indentation and blank line is a style issue // It does not make a block
14
If-Elseif (num2 == 0)
{
cout << endl << "Cannot divide by zero!";
}
else
{
quotient = float(num1) / num2;
cout << endl
<< "The quotient is " << quotient << ‘.’;
}
// Syntax// braces are optional for blocks of single statement// braces are required for blocks of multiple statements
15
Comparison Operators
==
!=
>
>=
<
<=
No Space between!
16
Logical Operators
&& // And
|| // Or
! // Not
17
Comparison Operators!=
!>
// NO!
<=
// Yes!
≤
// NO!
<=
// Yes!
18
Comparing characters
const char UPPER_A = 'A';
char inputChar;
cout << endl << "Enter a char: ”;
cin >> inputChar;
if (inputChar == UPPER_A)
cout << endl << “It's an A!”;
19
Examples
if (inputChar >= UPPER_A && inputChar <= 'Z') cout << endl // What should be the message? << "It's an upper case letter.";
inputChar = ‘C'; inputChar = inputChar + 1; cout << endl << "The next char is " << inputChar; // The next char is D
inputChar = ‘C'; cout << endl << "The next value is " << inputChar + 1; // The next value is 68
cout << endl << "The next char is " << char(inputChar + 1); // The next char is D
20
Multiple Conditions
int num1, num2;
cin >> num1 >> num2;
if (num1 > 0 && num2 > 0) cout << “Both numbers are positive.” << endl;
if (num1 > 0 || num2 > 0) cout << “At least one number is positive.” << endl;
if (num1 && num2 == 0) cout << “\nBoth numbers are zero.”;// NO!
if (num1 == 0 && num2 == 0) cout << “\nBoth numbers are zero.”;// Good!
if (num1 = 0 && num2 = 0) cout << “\nBoth numbers are zero.”;// NO!// “=” is assignment!
21
Checking Input
int score1, score2;
Cout << “Enter two scores between 0 and 60, inclusive.”;cin >> score1 >> score2;
if (score1 >= 0 && score1 <= 60 && score2 >= 0 && score2 <= 60) cout << “Both numbers are in the range.” << endl;
// Magic numbers!
const int MAX_SCORE = 60;const int MIN_SCORE = 0;
if (score1 >= MIN_SCORE && <= MAX_SCORE && score2 >= MIN_SCORE && score2 <= MAX_SCORE) cout << “Both numbers are in the range.” << endl;
22
Example: Find the MAX of two numbers
int num1, num2, max;
cin >> num1 >> num2;
if (num1 > num2) max = num1; else max = num2;
cout << “The max value is ” << max << endl;
// Another wayif (num1 >= num2) max = num1; else max = num2;
cout << “The max value is ” << max << endl;
23
Positive or Non-Positive
int x = 10, y = 7;float z = -5.0;
z = y / x; z = y / float(x);
if (z > 0) cout << “Positive."; else cout << "Non-Positive.";
// what is the output?
24
Even and Odd Numbers
const int EVEN_NUMBER_DIVIDER = 2;
int theNum;
cout << "Enter an integer: "; cin >> theNum;
// How do we know an integer is an even number?// The remainder operator %!if ((theNum % EVEN_NUMBER_DIVIDER) == 0) cout << theNum << " is an even number."; else cout << theNum << " is an odd number.";
25
Square Root Function
float theNum;
cout << "Enter a number: "; cin >> theNum;
// How do we compute the square root of a number?// Call function sqrt()!if (theNum >= 0) cout << “The square root of ” << theNum << “ is ” << sqrt(theNum); else cout << theNum << " is a negative number.";
// Need to include header file <cmath>
26
Power Function#include <iostream>#include <cmath>
using namespace std;
int main(){ float base, expo;
cout << "Enter two numbers: "; cin >> base >> expo;
// How do we compute baseexpo
// Call function pow()! if (base > 0) cout << “Base ” << base << “ raised to power ” << expo << “ is ” << pow(base, expo); else cout << “Base is not positive.";
return 0;}
27
Quiz 1-3
1 point
Submit to the Grader
Differences: NONE
Due 5 PM, today
28
Quiz 2-1
1 point
Submit to the Grader
Differences: NONE
Due 5 PM, Monday
29
Where Are the Materials?
Labs/ProgsK:\Courses\CSSE\CourseFiles\CS1430
https://xray.ion.uwplatt.edu/Files/
Notes/QuizzesSection Home Page:
http://people.uwplatt.edu/~yangq/cs143/
Your ScoresDesire2Learn
30
Getting Help
Student Helpers
Come to me for help
Name your file: UserName_Labx
Drop your program in K:\Courses\CSSE\yangq\Dropbox