\( % theorem \newenvironment{pf}{\textbf{Proof.}}{\rule{1ex}{1ex}} \newtheorem{qu}{Question} \newtheorem{thm}{Theorem} \newtheorem{rmk}{Remark} \newtheorem{go}{Goal} \newtheorem{df}{Definition} \newtheorem{prop}{Proposition} \newtheorem{mot}{Motivation} \newtheorem{ex}{Example} \lstnewenvironment{code}{}{} \newcommand{\nc}{\newcommand} %% env \nc{\env}[2]{ \begin{#1} #2 \end{#1} } \nc{\arr}[2]{ \begin{array}{#1} #2 \end{array} } \nc{\arrb}[2]{ \left\{ \begin{array}{#1} #2 \end{array} \right. } \nc{FTC}[3]{ \left[#3\right]^{#2}_{#1} } \nc{\bracs}[1]{ \left[#1\right] } \nc{\brac}[1]{ \left(#1\right) } \nc{\bracp}[1]{ \left\{#1\right\} } \nc{\intd}[4]{\int^{#2}_{#1}\left(#3\right) #4} % shortcut \nc{\Lra}{\Leftrightarrow} \nc{\Ra}{\Rightarrow} \nc{\La}{\Leftarrow} \nc{\C}{\mathbb{C}} \nc{\R}{\mathbb{R}} \nc{\Fc}{\mathcal{C}} \nc{\bds}{\boldsymbol} \nc{\ds}{\displaystyle} % begin end \nc{\bit}{\begin{itemize}} \nc{\eit}{\end{itemize}} \nc{\bcode}{\begin{code}} \nc{\ecode}{\end{code}} \nc{\bsage}{\begin{sageblock}} \nc{\esage}{\end{sageblock}} %pic \nc{\pic}{\includegraphics} \nc{\svg}{\includesvg} \nc{\pica}{\includegraphics[width=100px,height=100px]} \nc{\picb}{\includegraphics[width=150px,height=150px]} \nc{\vs}{\vspace} \nc{\tbf}{\textbf} %arrow \nc{\upa}{\nearrow} \nc{\downa}{\searrow} \nc{\upw}{\rcurvearrowright} \nc{\downw}{\curvearrowright} \)

Review Ch3/ch4

TEXTBOOK DEFINITION(C++)

  • object: a region of memory
  • variable: a named object
  • type: the kind of values it can store
string box;

string is a type and box is a variable name.

Assignment vs initialization

Which one is assignment?

int a=3;

vs

a=5;

Type safety

C++ compiler cannot guarantee complete type safety.

double x;
double y=x;//?
double z=2.0+x;//?

safe conversion

  • bool -> char, int or double
  • char -> int or double
  • int -> double

(narrow) unsafe conversion

  • double -> int, char or bool
  • int -> char
  • int or char -> bool

Example Narrow Conversion

double x=2.7;
int y=x;
int z=5/2;
double w=2.5/2;

y is 2. z is 2. (round-down.) w is 1.25.

int a=1000;
char b=a;

b depends on machine.

How to check narrowing conversions?

Use { }-list notation (C++11)

double x {2.7};
int y {x}; //ERROR!

int a{1000};
int y {x}; //ERROR!

char b1 {1000};// ERROR!

Expression(4.3)

Expression computes a value.

  • 10, 'a', 3.14 or "Hi"
  • variable
  • assignment
    length=9

Statement(4.4)

Statements Statement is the smallest standalone element that expresses some action.

a=b; 
++b;
a=b++b; //Syntax error:missing semicolon
int a=2;
cout<<a<<endl;
1+2; // Ok! Nothing.

Common mistake!

if

if (x==5);//empty statement!
{
    y=3;
}

In general,

if (expression) statement else statement

switch

switch (int, char or enum type){
    case (constant expression):
        statement
        ...
        break;// important!
        ...
    default:
        ...
        break;
}

Constant expression(4.3.1)

constexpr double pi=3.141592; // Compile time
int i=2;
const j=i; // running time


constexpr int max=100;
void use(int n){
    constexpr int c1=max+7;//OK!
    const int c2=n+7; //OK!

    constexpr int c3=n+7///Error!
    c2=3;// Error
}

Vector(4.6)

What is vector?

Vector is a sequence of elements that you can access by an index(iterator). Yes. It is a better array. We actually say it as a container. Vector also knows its.

How to declare or initialize

#include <vector>
#include <string>
vector<int> vi(6); // vector of 6 ints initialized to 0
vector<string> vs(4); // vector of 4 strings initialized to ""
vector<double> v; //empty vector
vector<int> v={1,2,3,4,5,6};// vector of 6 ints
vector<string> people={"Jobs","Gates","Larry"}
vector<int> {1,2,3}//{} initialization

size of a vector

vector<int> v={1,2,3,4};
for(int i=0;i<v.size();i++)  // Range is [0,v.size())
    cout<<v[i]<<endl;

or we can use Range-based for loop (c++11).

for(int x: v) // for each x in v
    cout<<x<<endl;

or

for(auto x: v) // auto keyword will find a correct type.(c++11)
    cout<<x<<endl;

Vector is dynamic.

You can increase the size of a vector by using push_back member function. push_back ad adds an element to the end.

vector<double> v; //empty
v.push_back(2.7);//Add 2.7
v.push_back(5.6);//Add 5.6

```c++
You can also delete the last element by `pop_back` member function.
```c++
v.pop_back();//Remove the last one, 5.6.

vector has many extra member functions.

Check 4 again.

CHECK 4. GET N INPUTS (ASSUME INTEGERS) AND STORE THEM INTO AN VECTOR. CALCULATE THE AVERAGE.

int sum(const vector<int>& v){
    //your code
}
int main(){
    //your code
}

Try

Again!

TYPE INTEGERS AND STOP UNTIL TYPE A NON-INTEGER like 'a'. STORE THEM INTO AN VECTOR. CALCULATE THE AVERAGE.

vector<int> v;
for(int t=0;cin>>t;){
    v.push_back(t);
}

If you type something which is not integer, it will stop since cin>>t will be converted to false. Otherwise it will be converted to true.