Académique Documents
Professionnel Documents
Culture Documents
Lecture 11
Summary
and Solved Problems
for the second mid-term exam
EEE 241
5/5/2011
Functions
#include <iostream>
#include <cmath>
using namespace std;
double CircleArea(double);
int main() {
double r;
cin >> r;
double A = CircleArea(r);
cout << "Area = " << A << endl;
return 0;
}
double CircleArea(double r) {
if (r<0) {
cout << "Error: Negative radius"
<< " in CircleArea()"
<< endl;
return -1.0;
} else {
return M_PI*r*r;
}
}
EEE 241
#include <iostream>
using namespace std;
// prototype
double fraction(int, int);
int main() {
int a = 4, b = 5;
double f = fraction(a, b);
cout << a << " / "
<< b << " = "
<< f << endl;
}
// definition
double fraction(int x, int y) {
return double(x)/double(y);
}
5/5/2011
#include <iostream>
using namespace std;
void swap(double&, double&);
int main() {
double x = 22.2, y = 33.3;
cout << x << " " << y << endl;
swap (x, y);
cout << x << " " << y << endl;
}
void swap(double& a, double& b) {
double c = a;
a = b;
b = c;
}
EEE 241
Vectors
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<double> a(5);
a[0]=8.4; a[1]=3.6; a[2]=9.1; a[3]=4.7; a[4]=3.9;
Output
0
1
2
3
4
8.4
3.6
9.1
4.7
3.9
values
indices
Note that vector a has 5 elements, and the index goes from 0 to 4.
For example the 4th element is a[3] which contains the value 4.7
EEE 241
5/5/2011
To pass by reference,
simple add & to the
function argument
and parameters.
vector<double> a(6);
a[0]=8.4; a[1]=3.6; a[2]=9.1;
a[3]=4.7; a[4]=3.9; a[5]=5.1;
cout << vectorMean(a) << endl;
}
double vectorMean(vector<double> x) {
double sum=0.0;
for (int i = 0; i < x.size(); i++) sum = sum+x[i];
return sum/x.size();
}
EEE 241
Output
0 1 1 2 3 5 8 13 21 34 55 89 144
233 377 610 987 1597 2584 4181
6765 10946 17711 28657 46368
75025 121393 196418 317811
514229 832040 1346269 2178309
3524578 5702887 9227465 14930352
24157817 39088169 63245986
int main() {
vector<int> Fib;
Fib.push_back(0);
Fib.push_back(1);
Fib.push_back(1);
do {
int k = Fib.size()-1;
Fib.push_back( Fib[k] + Fib[k-1] );
} while ( Fib.size() < 40 );
5/5/2011
File processing
To perform I/O with files you need to include the fstream (file stream) header.
Example: Input six values from a file and store them in a vector.
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main() {
vector<double> x(6);
ifstream myInputFile("exampleInput.txt");
for (int i=0; i<6; i++) {
myInputFile >> x[i];
cout << x[i] << endl;
}
myInputFile.close();
}
exampleInput.txt 1.00000
0.50000
0.33333
This file must
0.25000
already exist.
0.20000
0.16667
EEE 241
}
The result of executing the program is the creation of the
file exampleOutput.txt with the following contents:
EEE 241
8.4
3.6
9.1
4.7
3.9
5.1
10
5/5/2011
Data structures
Below is a list of all data types we have used in this course, including data structures:
Using data structures
Scalars
int
double
string
bool
body
#include <iostream>
using namespace std;
x;
x;
x;
x;
x;
struct body{
int age;
int height;
double weight;
};
Vectors
vector<int>
vector<double>
vector<string>
vector<bool>
vector<body>
x;
x;
x;
x;
x;
int main () {
body dog, cat;
cout << "Input values for the dog: ";
cin >> dog.age >> dog.height >> dog.weight;
cout << "Input values for the cat: ";
cin >> cat.age >> cat.height >> cat.weight;
.
.
EEE 241
11
To pass by reference,
simple add & to the
function parameters.
struct body{
int age;
int height;
double weight;
};
void displayBody(body x) {
cout << "
Age: " << x.age
<< " Yr" << endl;
cout << "Height: " << x.height << " mm" << endl;
cout << "Weight: " << x.weight << " kg" << endl;
return;
}
int main() {
body dog = {6, 561, 35.232};
displayBody(dog);
return 0;
}
EEE 241
12
5/5/2011
EEE 241
13
Solved Problems
EEE 241
14
5/5/2011
Problem 1 - description
Eight samples of lentils are sourced from different suppliers. For each sample the number of
impurities (stones etc) are counted.
The results are stored in a file lentils.txt; the contents are shown below:
EPN
APT
NAS
UKB
TTN
SAN
QUE
PAS
12
16
17
26
21
19
12
13
1.843
1.947
1.432
2.841
2.213
3.391
1.369
2.131
Write a C++ program that reads the data from the file
and outputs to the screen the supplier code and number
of impurities with each sample normalised to 1 kg.
Also, the output corresponding to the sample that has
the least normalised amount of impurities should be
marked with BEST, and the mean number of
normalised impurities and their standard deviation
should be output at the end.
EEE 241
EPN 6.51
APT 8.22
NAS 11.87
UKB 9.15
TTN 9.49
SAN 5.60 BEST
QUE 8.77
PAS 6.10
mean = 8.2 sd = 1.9
15
Problem 1 - Notes
1. The number of stones normalised to 1kg is:
stonesN = stones / weight(kg)
2. The mean and standard deviation of sample data is given by
http://en.wikipedia.org/wiki/Mean
http://en.wikipedia.org/wiki/Standard_deviation
EEE 241
16
5/5/2011
EEE 241
17
Output
EPN 6.51
APT 8.22
NAS 11.87
UKB 9.15
TTN 9.49
SAN 5.60 BEST
QUE 8.77
PAS 6.10
mean = 8.2 sd = 1.9
EEE 241
18
5/5/2011
19
Problem 1 sharepoint
You can find the data file:
and source code:
lentils.txt
lentils.cpp
http://www1.gantep.edu.tr/~eee241/sharepoint/
EEE 241
20
10
5/5/2011
Finally
In this lecture we summarised the C++ syntax and structure you
need for the second mid-term exam.
You can find the lecture slides in the course website:
http://eee241.gantep.edu.tr/
21
22
11