1
CS162 Introduction to Computer Science II
Welcome!
CS162 Topic #1
Today in CS162 Introduction ...what to expect!?! Talk about the - - PowerPoint PPT Presentation
CS162 Introduction to Computer Science II Welcome ! CS162 Topic #1 1 Today in CS162 Introduction ...what to expect!?! Talk about the Syllabus Discuss what Assignments will be like Go over our Objectives The Science of
1
CS162 Topic #1
CS162 Topic #1 2
CS162 Topic #1 3
CS162 Topic #1 4
CS162 Topic #1 5
CS162 Topic #1 6
CS162 Topic #1 7
CS162 Topic #1 8
CS162 Topic #1 9
CS162 Topic #1 10
11
CS162 Topic #1
12 CS162 Topic #1
13 CS162 Topic #1
14 CS162 Topic #1
15 CS162 Topic #1
16 CS162 Topic #1
#include <iostream.h> // *********************************** // Karla S. Fant // Purpose of this program is to convert // inches entered in by the user into // millimeters and display the results // ************************************ int main() { float inches, mm; //variable definitions cout <<“Welcome! We will be converting” <<“ inches to mm today” <<endl; //Get the input (prompt, read) cout <<“please enter the number of inches” <<“ that you wish to convert: “; cin >> inches; //read the # inches
17 CS162 Topic #1
//Echo what was entered cout <<“You entered: “ <<inches <<“in” <<endl; //Convert inches to millimeters mm = 25.4 * inches; //Display the results cout <<inches <<“in converts to “ <<mm <<mm <<“mm” <<endl; return 0; }
18 CS162 Topic #1
#include <iostream.h>
that file was typed at the beginning of our programs!
keyboard (standard in) and output to the screen (standard
19 CS162 Topic #1
// *********************************** // Karla S. Fant
documentation.
to place at least one space between the // and the asterisks....
comment!?!?!
20 CS162 Topic #1
/* *********************************** Karla S. Fant ********************************* */
comment!!!
21 CS162 Topic #1
//Define variables float inches; //to save # inches float mm; //to save the result
22 CS162 Topic #1
– Allocate Memory to store data
– float, int, char – double, short, long
– must start with a letter, be any combination of letters, digits, or underscores.
23 CS162 Topic #1
display is being sent to the OUTPUT device in the direction of the arrow:
24 CS162 Topic #1
coming from the input device in the direction of the arrow and being saved in memory:
25 CS162 Topic #1
//Step #3 Convert inches to millimeters mm = 25.4 * inches;
26 CS162 Topic #1
#include <math.h>
27 CS162 Topic #1
28 CS162 Topic #1
result += 10 result = result+10 result *= x+y result = result *(x+y) result /= x+y (x+y) result result = result/(x+y)
29 CS162 Topic #1
30 CS162 Topic #1
parentheses: (2.5 + (-3.6)
31 CS162 Topic #1
precedence of operators. *, /, % have a higher precedence....than + and -.
32 CS162 Topic #1
right.
33 CS162 Topic #1
34 CS162 Topic #1
35 CS162 Topic #1
input
50 52 100 102
36 CS162 Topic #1
input
50 51 100 101
37 CS162 Topic #1
38 CS162 Topic #1
39 CS162 Topic #1
float test; cout << “Please enter a real number”; cin >> test; cout << test; Input Resulting Output 1.23456789 1.23457 10.23456789 10.2346 100.23456789 100.235 1000.23456789 1000.23 100000.23456789 100000
40 CS162 Topic #1
float test; cout << “Please enter a real number”; cout.precision(3); //3 instead of 6!! cin >> test; cout << test; Input Resulting Output 1.23456789 1.23 10.23456789 10.2 100.23456789 100 10000.23456789 1e+04 (Exponential notation)
41 CS162 Topic #1
#include <iomanip.h> float test; cout << “Please enter a real number”; cin >> test; cout <<setprecision(3) << test;
cout.precision(3) and cout <<setprecision(3)
42 CS162 Topic #1
43 CS162 Topic #1
float test; cout.precision(4); cout.width(10); cin >>test; cout << test; cout <<endl <<test; Input Resulting Output 1.23456789 1.235 1.235
44 CS162 Topic #1
#include <iomanip.h> float test; cout.precision(4); cin >>test; cout <<setw(10) << test; cout <<endl <<test; Input Resulting Output 1.23456789 1.235 1.235
45 CS162 Topic #1
46 CS162 Topic #1
float test; cout.precision(4); cout.setf(ios::showpoint); cin >>test; cout << test <<endl; cout.unsetf(ios::showpos); //reset... cout <<test; Input Resulting Output 1.2300 1.230 1.23
47 CS162 Topic #1
48 CS162 Topic #1
49 CS162 Topic #1
50 CS162 Topic #1
51 CS162 Topic #1
52 CS162 Topic #1
1) One alternative: if (conditional expression) single C++ statement; char selection; cout <<“Enter a selection (m or i): “; cin >> selection; if (selection == „q‟) cout <<“Your selection was incorrect” <<endl;
53 CS162 Topic #1
2) Two alternatives: if (conditional expression) single C++ statement; else single C++ statement; if (selection == „m‟) cout <<“Converting inches -> mm”; else cout <<“Converting mm -> inches”;
54 CS162 Topic #1
55 CS162 Topic #1
it means it is FALSE. In this case, if there is an else the statement following the else is executed.
56 CS162 Topic #1
3) Two or more alternatives: if (conditional expression) single C++ statement; else if (conditional expression) single C++ statement; if (selection == „m‟) cout <<“Converting inches -> mm”; else if (selection == ‘i’) cout <<“Converting mm -> inches”;
57 CS162 Topic #1
4) You might want more than a single statement to be executed given an alternative...so instead of a single statement, you can use a compound statement if (conditional expression) { Many C++ statements; } else //optional
58 CS162 Topic #1
if (selection == „m‟) { cout <<“Enter the # inches: “; cin >>inches; mm = 25.4*inches; cout <<inches <<“in converts to ” <<mm <<“ millimeters” <<endl; } else //selection is not an „m‟ { cout <<“Enter the # millimeters: “; cin >>mm; inches = mm/25.4; cout <<mm <<“mm converts to ” <<mm <<“ inches” <<endl; }
59 CS162 Topic #1
== for equal to != for not equal to
60 CS162 Topic #1
61 CS162 Topic #1
62 CS162 Topic #1
63 CS162 Topic #1
64 CS162 Topic #1
65 CS162 Topic #1
66 CS162 Topic #1
n Because no mater what you type in (m, i, p, q)
n If an m is entered, it won‟t be an i!!!!!
67 CS162 Topic #1
68 CS162 Topic #1
n Notice the parens...you must have a set of
69 CS162 Topic #1
70 CS162 Topic #1
char grade; cout <<"Enter the grade..." <<endl; cin >>grade; switch (grade) { case 'A': cout <<"Excellent" <<endl; cout <<“Keep up the good work!”; break; case 'B': cout <<"Very Good"; break; case 'C': cout <<"Passing"; break; case 'D': case 'F': cout <<"Too Bad"; break; default : cout <<"No match was found...try again"; break; }
71 CS162 Topic #1
72 CS162 Topic #1
73 CS162 Topic #1
74 CS162 Topic #1
int count; cout <<"Please enter the number of asterisks:"; cin >>count; switch (count) { //these { } are mandatory! case 1: cout <<"*"; case 2: cout <<"**"; case 3: cout <<"***"; case 4: cout <<"****"; default: cout <<"!"; } cout <<endl;
75 CS162 Topic #1
int count; cout <<"Please enter the number of asterisks:"; cin >>count; switch (count) { //these { } are mandatory! case 1: cout <<"*"; break; case 2: cout <<"**";break; case 3: cout <<"***"; break; case 4: cout <<"****"; break; default: cout <<"!";break; } cout <<endl;
76 CS162 Topic #1
77 CS162 Topic #1
....let’s see....
78 CS162 Topic #1
79 CS162 Topic #1
80 CS162 Topic #1
while (loop repetition condition) <body>
81 CS162 Topic #1
82 CS162 Topic #1
char response = „n‟; while (response == „n‟) { cout <<“Please enter ... “; cin >> data; cout <<“We received: “ <<data <<“\nIs this correct? (y/n)”; cin >>response; }
83 CS162 Topic #1
while (response == ‘n’ || response == ‘N’) { ... }
84 CS162 Topic #1
while (response != ‘y’ && response != ‘Y’) {
... }
85 CS162 Topic #1
#include <ctype.h> while (tolower(response) != „y‟) { ... }
86 CS162 Topic #1
#include <ctype.h> while (toupper(response) != „Y‟) { ... }
87 CS162 Topic #1
do single statement; while (conditional expression); do { many statements; } while (conditional expression);
88 CS162 Topic #1
89 CS162 Topic #1
char response; do { cout <<“Please enter ... “; cin >> data; cout <<“We received: “ <<data <<“\nIs this correct? (y/n)”; cin >>response; } while (response != „y‟ && response != „Y‟);
90 CS162 Topic #1
int i; for (i=1; i <= 9; ++i) cout <<i <<endl;
91 CS162 Topic #1
for (initialize; conditional exp; increment) <body>
by a semicolon or a compound statement surrounded by {}.
92 CS162 Topic #1
93 CS162 Topic #1
for (i=0; i < 10; ++i) j+=i ;//remember this is j = j+1;
i = 0; while (i < 10) { j += i; ++i; }
94 CS162 Topic #1
for (char response = „n‟;
response != „y‟ && response != „Y‟;
cin >>response) { cout <<“Please enter ... “; cin >> data; cout <<“We received: “ <<data <<“\nIs this correct? (y/n)”; }
95 CS162 Topic #1
96 CS162 Topic #1
97 CS162 Topic #1
98 CS162 Topic #1
99 CS162 Topic #1
100 CS162 Topic #1
101 CS162 Topic #1
102 CS162 Topic #1
103 CS162 Topic #1
104 CS162 Topic #1
105 CS162 Topic #1
106 CS162 Topic #1
107 CS162 Topic #1
108 CS162 Topic #1
argument cin.get use
109 CS162 Topic #1
110 CS162 Topic #1
111 CS162 Topic #1
112 CS162 Topic #1
113 CS162 Topic #1
strcmp(first_array, second_array);
114 CS162 Topic #1
115 CS162 Topic #1
116 CS162 Topic #1
117 CS162 Topic #1
char str[20]; char ch; int index = 0; ch = cin.get(); while (ch != „\n‟) { str[index] = ch; //str[index] is a char ++index; ch = cin.get(); } str[index] = „\0‟; //why is this important?
118 CS162 Topic #1
const int MAX = 20; char str[MAX]; char ch; int index = 0; ch = cin.get(); while (index < MAX-1 && ch != „\n‟) { str[index] = ch; //str[index] is a char ++index; ch = cin.get(); } str[index] = „\0‟; //why is this important?
119 CS162 Topic #1
const int MAX = 20; char str[MAX]; int index = 0; while (index < MAX-1 && (ch= cin.get()) != „\n‟)) str[index++] = ch; //Remember postfix???? str[index] = „\0‟; //Still important
120 CS162 Topic #1
const int MAX = 20; char str[MAX]; int index = 0; while (index < MAX-1 && (str[index++]= cin.get()) != „\n‟)); str[index] = „\0‟;
121 CS162 Topic #1
122 CS162 Topic #1