PIC10A Lecture 3

Reading

1.2,1.3,1.4

Input and Output Stream

A stream is a flow of information.

Output Stream

Output Stream is a stream of output generated by the program. e.g. to screen
output stream

cout

Examples

basic example
cout << "Hello! PIC10A students.";
Here is the output
Hello! PIC10A students.

You can link 2 or more sentences together.
cout << "Hello! " << "PIC10A students. " << "Welcome to UCLA.";
This is same as
cout << "Hello! ";
cout << "PIC10A students. ";
cout << "Welcome to UCLA. ";

The output is
Hello! PIC10A students. Welcome to UCLA.

You can include variables
int appleNum = 5;
cout << "I ate " << appleNum << " apples this morning.";

The output is
I ate 5 apples this morning.
appleNum is substituted by its value (5).

You can include arithmetic expressions.
int speed = 45, time = 2;
cout << "I drove " << (speed*time) << " miles.";

Here is the output
I drove 90 miles.
The expression speed*time is evaluated. The value is then printed.
It is better the put parentheses (but not necessary in Visual C++)  around the expresion.

Escape Sequences

Sequence
Meaning
\n
New line
\t
tab
\a
alert(sounds the alert noise), typically a bell
\\
Backslash (allows you to place a backslash in a quoted expression)
\"
Double quote(mostly used to place a double quote inside a quoted string)
\'
Double quote(mostly used to place a single quote inside single quotes)

Examples

Example
int num1 = 10, num2 = 20;
cout << "The first number is " << num1
     << ".\nThe second number is " << num2  << ".\n";

Output
The first number is 10.
The second number is 20.


Example
cout << "Backslash \\ is used in escape sequences.\n";
Output
Backslash \ is used in escape sequences.

Example
cout<<"\"PIC10A is cool,\" Mary said.\n";
Output
"PIC10A is cool," Mary said.

endl vs "\n"

cout << "Hello World!\n";
is same as
cout << "Hello World!" << endl;

Formatting for Numbers with a Decimal Point

Example
cout<<"1/11 is "<< 1./11. << endl;
Output
1/11 is 0.0909091

Here is the "magic formula". It causes the number to be displayed with the exact number of digits after the decimal point.
#include<iostream>
using namespace std;

int main() {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
  
    cout<<"1/11 is "<< 1./11. << endl;
    return 0;
}

Output
1/11 is 0.09
 Exercise:  Use cout.precision(3) in the program, what is the output?

Input Stream

Input stream is info fed to the computer for the program.
input stream

Comments

Example
/* PIC 10A Sample Program
Written by Charles Li
The program asks the user to input the number of quarters, dimes, nickels
and output the total value of all the coins
*/

#include<iostream>
using namespace std;

int main() {
    double quarterValue = 0.25, dimeValue = 0.1, nickelValue = 0.05;
    int numOfQuarters, numOfDimes, numOfNickels;
    double value; //value of all the coins
    cout << "Input the number of quarters, dimes and nickels.\n";
    // ask the user to input the numbers
    cin >> numOfQuarters >> numOfDimes >> numOfNickels;
    value = quarterValue*numOfQuarters
           + dimeValue*numOfDimes
           + nickelValue*numOfNickels;
    cout << "The value is " << value <<".\n";
 
    return 0;
}

Single line comments

// your comment
All the text between the // and the end of the line is a comment

Multi-Lines comments

/* your comment line 1
your comment line2
your comment line 3 */

More about types

float and double

char

bool

Arithmetic Operations and Expressions

Order of operations in expressions

Mixed types

Question
1) What is the type of totalWork in below?
int workDays = 5;
double hours = 7.5;
?????  totalWork = workDays*hours;

2) What is the return type if a float is added to a double?
3) What is the output of the following program
int a = 7, b = 3;
cout << a/b <<endl;

The % operator

Shorthand

examples
shorthand
meaning
a+=1;
a = a+1;
b-=3.5;
b = b-3.5;
c*=2;
c = c*2;
d/=3.1415;
d = d/3.1415;
e++ ( or ++e)
e = e+1;  
f-- ( or --f)
f = f-1;

Difference between x++ and ++x
Code
#include<iostream>
using namespace std;

int main() {
    int a = 5;
    int b = a++;
    cout << b <<endl;
    return 0;
}
#include<iostream>
using namespace std;

int main() {
    int a = 5;
    int b = ++a;
    cout << b <<endl;
    return 0;
}
Output
5
6
Reason
The code means:
int b = a;
a = a+1;

The code means:
a = a+1;
int b = a;