10 views

Original Title: Eee 241 Lecture 11

Uploaded by Şemsettin karakuş

- Java
- Reports_9i_Training
- ABB RET615 Parameters Txt
- BYOND Guide
- Wickham - JSS11 - Plyr
- 0163-CreatingandApplyingRulesinInformaticaAnalyst
- plsql subprograms
- Opal Report
- Fibonacci Mobiles
- reddd
- BonnMotion Docu
- Abap Notes
- 15-4
- Report Painter COPA
- 10.1.1.57.8341
- Scientiﬁc Computation and Functional Programming
- Ascon_IO_XS_EN
- PID with a Siemens S7-1200 PLC _ DMC, Inc.pdf
- Vacon NXP Line Sync APFIF131 Application Manual UD
- Graphic x

You are on page 1of 11

Lecture 11

Summary

and Solved Problems

for the second mid-term exam

need to know for the second mid-term exam.

- First mid-term topics (see first mid-term summary lecture)

- and the following:

Functions

Vectors

File processing

Data structures

EEE 241

summarised in this

document

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

functions that will be defined later.

(remember this was prototyped at the top)

This function separates the details of a task

from the main program, the main program

simply

uses

the

resource

called

CircleArea().

and if parameters exist, the parameters also have types.

The following program contains a programmer-defined function that returns a type double value

that is the result of the division of two integers (without truncation).

Notes:

#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);

}

EEE 241

you need to declare it with a prototype.

The arguments in a function call

should match (in number, order and type) the

parameters in the function definition.

A function returns a value of a specific type.

A void function returns no value:

// function to output n dashes

void dashes(int n) {

for (int i=0; i<n; i++) cout << "-";

cout << endl;

}

summary and solved problems for MT2

5/5/2011

Classic example: swapping two values

Notes:

#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

parameters as references to the arguments. This

appears in the function prototype and function

definition.

In the function swap(), parameters a and b

refer to the x and y variables in the main

program block.

Any changes to a and b in the function also

affect x and y in the main program block.

The output is:

22.2 33.3

33.3 22.2

Vectors

#include <iostream>

#include <vector>

using namespace std;

int main() {

elements all of type double

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

cout << i << "\t" << a[i] << endl;

}

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

Example: mean of the values in a vector.

#include <iostream>

#include <vector>

using namespace std;

double vectorMean(vector<double>);

int main() {

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();

}

5.8

EEE 241

Example: Construct a vector that contains the first 40 Fibonacci numbers.

The Fibonacci numbers are the integer sequence: 0 1 1 2 3 5 8 13 21 34 ...

i.e. Fibk+1 = Fibk + Fibk-1

See http://en.wikipedia.org/wiki/Fibonacci_number

#include <iostream>

#include <vector>

using namespace std;

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 );

for (unsigned int i=0; i<Fib.size(); i++) cout << Fib[i] << " ";

}

EEE 241

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();

}

1

0.5

0.33333

0.25

0.2

0.16667

exampleInput.txt 1.00000

0.50000

0.33333

This file must

0.25000

already exist.

0.20000

0.16667

EEE 241

The object myInputFile points

to the file exampleInput.txt

#include <iostream>

#include <fstream>

#include <vector>

using namespace std;

int main() {

vector<double> x(6);

x[0]=8.4; x[1]=3.6; x[2]=9.1;

x[3]=4.7; x[4]=3.9; x[5]=5.1;

ofstream myOutputFile("exampleOutput.txt");

for (int i=0; i<6; i++) {

myOutputFile << x[i] << endl;

}

myOutputFile.close();

The object myOutputFile points to

the file exampleOutput.txt

Just like

cout << x[i] << endl;

}

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;

.

.

is the data structure:

}

EEE 241

11

Example: a function that outputs a data structure to the screen.

#include <iostream>

using namespace std;

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;

Age: 6 Yr

Height: 561 mm

Weight: 35.232 kg

}

EEE 241

12

5/5/2011

thats all the C++ you need for the second mid-term exam!

But feel free to learn more at:

http://cpp.gantep.edu.tr/

http://www.learncpp.com/

And please practice programming as much as possible,

For example by completing all the labs:

http://www1.gantep.edu.tr/~eee241/labs.php

see in this course, then search C++ expression in google.

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

Column 2 the number of impurities

Column 3 the sample weight (kg)

something like this:

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

#include <iostream>

#include <iomanip>

#include <fstream>

#include <vector>

#include <cmath>

using namespace std;

// Note that these all have global scope.

const int n=8;

// number of samples

vector<string> code(n);

// Sample code

vector<double> stones(n); // Number of stones

// function prototypes

void getData(void);

int

best(void);

void meanSD(double&, double&);

EEE 241

17

int main() {

getData();

int imin = best();

cout << fixed << setprecision(2);

for (int i=0; i<n; i++) {

cout << endl

<< setw(4) << code[i]

<< setw(6) << stones[i];

if (i==imin) cout << " BEST";

}

double mean, sd;

meanSD(mean, sd);

cout << "\n\n" << setprecision(1)

<< " mean = " << mean

<< " sd = " << sd << endl;

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

// Get the data.

void getData(void) {

int s;

// stones

double w; // weight

ifstream lentils("lentils.txt");

for ( int i=0; i<n; i++) {

lentils >> code[i] >> s >> w;

stones[i] = s/w; // stones (normalised).

}

lentils.close();

}

// Calculate mean and standard deviation.

void meanSD(double& mean, double& sd) {

double m1=0.0, m2=0.0;

for ( int i=0; i<n; i++) {

m1 = m1 + stones[i];

m2 = m2 + stones[i]*stones[i];

}

m1 = m1/n; // first moment about the origin

m2 = m2/n; // second moment about the origin

mean = m1;

// mean

sd = sqrt(m2-m1*m1); // standard deviation

}

EEE 241

// the best supplier.

int best(void) {

int imin=0;

double min=stones[0];

for (int i=1; i<n; i++) {

if (stones[i]<min) {

min=stones[i];

imin=i;

}

}

return imin;

}

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/

http://cpp.gantep.edu.tr/

and

http://www.learncpp.com/

Note: after the second mid-term exam we will look at

numerical methods as example applications of C++.

21

http://www1.gantep.edu.tr/~eee241/labs.php

example second mid-term exam questions.

First try solving the questions by hand,

then code your solutions in the lab hour.

DONT FORGET TO BRING A CALCULATOR

TO THE EXAM!

22

11

- JavaUploaded byAvinash Vyas
- Reports_9i_TrainingUploaded byVikranth Kunreddy
- ABB RET615 Parameters TxtUploaded byDuško Tovilović
- BYOND GuideUploaded byAlex Montero
- Wickham - JSS11 - PlyrUploaded byf1r3br4nd9427
- 0163-CreatingandApplyingRulesinInformaticaAnalystUploaded byfabiogomesbr
- plsql subprogramsUploaded bykvasayanam
- Opal ReportUploaded byVillicaña Avila Daniel
- Fibonacci MobilesUploaded byGabriel Huentemil Ortega
- redddUploaded byKarthikeyan Raju
- BonnMotion DocuUploaded bySoup Kataks
- Abap NotesUploaded byjkstar
- 15-4Uploaded byksr131
- Report Painter COPAUploaded byRajashree Sirsikar
- 10.1.1.57.8341Uploaded byAshief Ahmed
- Scientiﬁc Computation and Functional ProgrammingUploaded bycbeust
- Ascon_IO_XS_ENUploaded bythiodoro
- PID with a Siemens S7-1200 PLC _ DMC, Inc.pdfUploaded bynlinhktdk
- Vacon NXP Line Sync APFIF131 Application Manual UDUploaded byTanuTiganu
- Graphic xUploaded byNgro Quinapaxi Gonzalez
- Fanuc_16i_18i_21i_Data_Input_Output.pdfUploaded byTonny Danny
- Fanuc 30i Data Input OutputUploaded byShahzad Anwar
- output from run ScratchUploaded bystibjork
- ExtUploaded byapi-3831626
- Control With Logix PIDE IMC CCUploaded bysdnv529
- healing_addon_v1.7Uploaded byGuillermo Ceos
- 634831 - PID AutoTune Process on Micro800Uploaded bybulonia
- b0193ax3_uUploaded byHatem Ben Gaied
- Pi DatalinkUploaded byErland Cuarite
- AMDPUploaded byNaveen Raj

- exp1Uploaded byŞemsettin karakuş
- EEE 352 ACS Lab Report CoverUploaded byŞemsettin karakuş
- Lab3_dataUploaded byŞemsettin karakuş
- Experiment 7 352Uploaded byŞemsettin karakuş
- exp.8Uploaded byŞemsettin karakuş
- exp 5Uploaded byŞemsettin karakuş
- Exp 9 ReportUploaded byŞemsettin karakuş
- exp 3Uploaded byŞemsettin karakuş
- Step 041026 Oh pUploaded byŞemsettin karakuş
- exp6Uploaded byŞemsettin karakuş
- EEE352EXP2Uploaded byŞemsettin karakuş
- Lad2_dataUploaded byŞemsettin karakuş
- science-2Uploaded byŞemsettin karakuş
- Fakulte Ders Programi Guncelleme 120220157359Uploaded byŞemsettin karakuş
- science-1Uploaded byŞemsettin karakuş
- yazokulucalismaUploaded bygokhan01_101
- Eee 241 Lecture 03Uploaded byŞemsettin karakuş
- c101 Practice ExamUploaded byachibaba
- 4_Boole_CebriUploaded byŞemsettin karakuş
- BOLM_01 turkceUploaded byŞemsettin karakuş
- 143sonUploaded byŞemsettin karakuş
- 143sonUploaded byŞemsettin karakuş
- Quiz Es 7949Uploaded byZübük Zade
- Lecture 3Uploaded byŞemsettin karakuş
- 2007-2008 Devre II Yaz Vi̇zeUploaded byŞemsettin karakuş
- Vector Mechanics for EngineersUploaded byChristian Villena
- 4 BTUploaded byŞemsettin karakuş
- ELktrikMuh.neGiris3.BolumUploaded byŞemsettin karakuş
- exampl c1Uploaded bysowhat-01
- Bolum-7Uploaded byŞemsettin karakuş

- Avrbeginners 40 USB Control Transfers With LUFA 1.1Uploaded byjomenk
- Php.ini PmapperUploaded byAdriano Wendler
- Sap Hana Administration - Course ContentUploaded bybobzsri
- OpenERP Technical Upgrade v6Uploaded byRicardo Salvador Valencia Domínguez
- Fastest Way to Recover or Upgrade Cisco IOS Using TftpdnldUploaded byMuhammad Majid Khan
- IT_SEM7_IT2406LM.docUploaded bySenthil R
- In 910HF1 JODBCConnectionGuide EnUploaded byranusofi
- Programim mobileUploaded byOrnela Lleshi
- Instruction ManualUploaded byHomo Cyberneticus
- PFile_Vs_SPFileUploaded byneelimasrikanth
- SAP_VBSUploaded byRV
- OpenERP Technical Memento v0.7Uploaded byJosé Loureiro
- SAP LSMW guideUploaded byNaveen Reddy
- 10.P2PUploaded byKereta DiAtas Rel
- Holographic Data StorageUploaded byMridul Sachan
- Disaster Recovery for Microsoft SQL Server Using VMware VSphere Site Recovery ManagerUploaded byzekinha.pilinhas
- Sex Book UrduUploaded bysaqib raza
- 1.4.3 the Processor's Instruction SetUploaded byAbbas Haider
- STM32F0xxx Reference ManualUploaded byVishal Panchal
- 097-58001-01-Rev JUploaded byPopovici Cosmin
- SAS PC Files - Reference acpcrefUploaded bymbenitezcol
- Global & Local Causal Factors in DemantraUploaded byGaurav Singh
- Class XII Cscince Practical AssignmentUploaded byDheeraj Vijay
- Add_Relation_Co-Site_949_20160603 (2)Uploaded byrf global1
- TalendOpenStudio BigData UG 5.2.1 EnUploaded byAbderrazekHmidet
- Linux - Security and Optimization SystemUploaded bylcnblzr3877
- 4 Ict Development in the Ph Unpan006170Uploaded byMarie Marnie Bragais
- osUploaded bysb_pondy
- LTRT-65108 MP-11x & MP-124 H.323 User's Manual Ver 5.0Uploaded byJuan Carlos
- Creating Dynamic XML Sitemap Using PhpUploaded byapi-3697621