Lesson 1
Pic 10B, Ricardo Salazar
Syllabus Highlights
Midterms:
Friday, January 29.
Friday, February 19.
From 10:00 to 10:50 am at MS 5200.
Final Exam:
Friday, March 18.
From 3:00 to 6:00 pm at MS 5200.
Grading system
Your final score in the class will be the maximum of:
Schema A
25% Homework
20% Midterm 1
20% Midterm 2
35% Final
or
Schema B
25% Homework
30% Highest midterm
44% Final exam
1% Participation (details to be explained throughout the
quarter)
Assignments
The 'official' compiler we will use is the one available at
the PIC Lab: Currently Visual studio 2013.
You can use other compilers but
The usual: "I got 0 points but my program works on my
computer"
IS NOT A VALID EXCUSE.
something's missing
The comments (of course).
<iostream>
<iomanip>
<string>
<cmath>
<fstream>
<vector>
sqrt(pow(4,2)+pow(3,2))
cin
cout
myList.size()
name.length()
setw(10)
<ctime>
<cstdlib>
<algorithm>
"ccc_win.h"
ofstream fout;
Your turn! Fill in the boxes with statements associated to the corresponding libraries.
string s;
getline(cin, s);
endl;
E.g:
/* *********************************************
multiplyTwoNumbers: Multiplies two doubles.
Parameters: doubles a and b to be multiplied.
Returns: the product of the parameters.
********************************************* */
double multiplyTwoNumbers(int a, int b){
return a*b;
}
fin.open("file.txt"); // "file.txt" is a
// [c]-string
string fileName;
// fileName is a
cin >> fileName;
// [c++]-string
myFout.open( fileName.c_str() ); // <-- why?
Exercise:
From the class syllabus:
Write a program (right now) that reads the scores from a file (see
the setup on the board), and computes the final score of a student.
Use at least one function,
pass at least one parameter by reference, and
save the final score in the file: grade.txt
value
object name
Important!!!
Definition of constants
Declaration of other classes
Definition of member functions
Declaration of non-member functions
Declaration of global variables
class.h
#ifndef CLASS_H
#define CLASS_H
class Class{
. . .
};
#endif
class.cpp
#include "Class.h"
Class::Class(){
. . .
}
void Class::fun(){
. . .
}
Note that both hw1.cpp and class.cpp include the header file
class.h
Things (arrays)
An array is a list values of the same type.
The general form of an array declaration is:
type arrayName[size];
Example:
An array of 5 doubles is declared:
double list[5];
or use a loop.
Elements are accessed by index:
arrayName[index]
Things (vectors)
Similar to arrays but slightly 'better'.
The library <vector> is needed.
Syntax:
vector<dataType> variableName(size);
size
Remark:
Vectors can be passed by reference!
f
2
-4
-4
Description
vector(int n)
int size()
void push_back(data_t d)
void pop_back()
void resize(int n)
Exercises
Write snippets of code that:
Reads the contents of a file into a vector.
Erase an element from a vector (without using the
member function erase(int position).
Inserts an element at the beginning of a vector.
Merges two ordered vectors into another ordered vector.
Use your favorite data type.
Mine is int (in case you were wondering!).
Things (pointers)
We can store memory addresses using pointers.
A pointer is denoted by *.
Keep this in mind:
* = "points to"
& = "address of"
int x = 23; // Creates an integer x and sets its value
int* p;
// Creates a pointer to an integer
// p exists but does not point anywhere
p = &x;
p
23
int x = 23;
int* p = &x;
double w = 3.14;
double* q = &w;
3.14
q
Card card1;
Card* r = &card1;
string str = "Hi Manolo!"
string* s = &str;
s
23
card1
r
rank=1
suit=clubs
str
Hi Manolo!
// displays address of x
// displays address of x
// displays value of x
// displays value of x
// displays "Manolo"
// changes string to "Joaquin"
*p.get_suit();