0% found this document useful (0 votes)
98 views14 pages

C++ Tutorial Example Code

This document contains a tutorial on C++ programming. It covers topics such as quadratic equation solving, computing pipe diameters for pressurized flow using iterative methods, variable scopes, arrays, vectors, file I/O, functions, pointers, structures, and linking C++ programs to Epanet and EPA SWMM software. The document provides code examples to demonstrate key concepts for each topic.

Uploaded by

ddhgdhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views14 pages

C++ Tutorial Example Code

This document contains a tutorial on C++ programming. It covers topics such as quadratic equation solving, computing pipe diameters for pressurized flow using iterative methods, variable scopes, arrays, vectors, file I/O, functions, pointers, structures, and linking C++ programs to Epanet and EPA SWMM software. The document provides code examples to demonstrate key concepts for each topic.

Uploaded by

ddhgdhg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

C Plus Plus Tutorial/2009 Run - Intra Page 1 of 14

C Plus Plus Tutorial/2009 Run


From Intra

< C Plus Plus Tutorial

◾ File:Maximum flow in pipe.ppt

Contents
◾ 1 Quadratic equation solving
◾ 2 Compuation of pipe diameter for pressurized flow
◾ 2.1 Version 1: No iteration
◾ 2.2 Version 2: Crude Iteration
◾ 2.3 Version 4: Using a function
◾ 2.4 Version 5: Return a value from a function
◾ 2.5 Version 6: Automatic iteration based on precision
◾ 3 Scope of variables
◾ 4 Arrays
◾ 4.1 10 numbers
◾ 4.2 Variable number of items -- but subject to a maximum
◾ 4.3 Multi-dimensional arrays
◾ 5 Vectors
◾ 5.1 Example 1
◾ 5.2 Example 2
◾ 6 File I/O
◾ 6.1 Reading from files
◾ 7 Functions
◾ 7.1 Using multiple files to define a program
◾ 7.2 With a Header file
◾ 7.3 Prime Numbers
◾ 8 extern
◾ 9 Pointers
◾ 9.1 Passing by value and passing by reference
◾ 9.2 Passing an array
◾ 10 Structures
◾ 11 Epanet with c/c++
◾ 12 EPA SWMM with C/C++

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 2 of 14

Quadratic equation solving


/*
Assela Pathirana (2009-05-25)
Reads three inputs a,b and c from console and
1. Tests whether a!=0
2. Test wheter b^2>=4ac
if a=0, then x1,x2=-c/b
if a!=0 and b^2>=4ac, then
X1=1/2a [-b+sqrt(b^2-4ac)]
X2=1/2a [-b-sqrt(b^2-4ac)]

Print X1 and X2
if a!=0 AND b^2<4ac then X1, X2 are complex. We just don't calculate
*/
#include <iostream>
#include <math.h>
using namespace std;

int main(){
// input a, b and c
cout << "Please input a,b and c.\n";
cout << "a=";
double a;
cin >> a;
cout << "a=";
double b;
cin >> b;
cout << "c=";
double c;
cin >> c;
cout << "You have entered a,b,c=" << a <<
", " << b << ", " << c << "\n";
//now we have a, b and c from the user
double x1, x2;
int flag;
if(a==0){
flag=1;
//a==0
x1=-c/b;
x2=x1;

}else{
if(b*b>=4*a*c){
flag=2;
double tmp=sqrt(pow(b,2)-4*a*c);
x1=(-b+tmp)/(2*a);
x2=(-b-tmp)/(2*a);
}else{
flag=0;
cout << "The equation can not be solved.\n";

}
}

if(flag!=0){
cout << "The solutions are: x1,x2="<< x1 << ", "
<< x2 <<"\n";
}
}

Compuation of pipe diameter for pressurized flow


Version 1: No iteration

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 3 of 14

#include <iostream>
#include <math.h>
#define PI 3.141592654
#define g 9.81
using namespace std;
int main(){
/*input Q, S*/

double Q;
cout <<"please enter Q.\n";
cin >> Q;
cout <<"Q="<<Q<<"\n";
double S;
cout <<"please enter S.\n";
cin >> S;
cout <<"S="<<S<<"\n";
cout <<"Q=,S="<< Q << ","<< S <<"\n";
double k=0.1;
double nu=1.31E-6;
double v=1;
double D=sqrt(4*Q/(PI*v));
cout <<"D="<< D <<"\n";
double Re=v*D/nu;
double Lmd=(0.25/(
pow(
log(
5.1286/pow(Re,0.89)+k/(3.7*D)
)
,2)
));
double vf=sqrt(2*g*D*S/Lmd);
cout <<"vf="<< vf <<"\n";
}

Version 2: Crude Iteration

#include <iostream>
#include <math.h>
#define PI 3.141592654
#define g 9.81
using namespace std;
int main(){
/*input Q, S*/

double Q;
cout <<"please enter Q.\n";
cin >> Q;
cout <<"Q="<<Q<<"\n";
double S;
cout <<"please enter S.\n";
cin >> S;
cout <<"S="<<S<<"\n";
cout <<"Q=,S="<< Q << ","<< S <<"\n";
double k=0.1;
double nu=1.31E-6;
for(int i=0;i<100;i++){
double v;
cout <<"please enter v.\n";
cin >> v;
cout <<"v="<<v<<"\n";
double D=sqrt(4*Q/(PI*v));
cout <<"D="<< D <<"\n";
double Re=v*D/nu;
double Lmd=(0.25/(
pow(
log(
5.1286/pow(Re,0.89)+k/(3.7*D*1000)
)
,2)
));
double vf=sqrt(2*g*D*S/Lmd);

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 4 of 14

cout <<"vf="<< vf <<"\n";


}
}

Version 4: Using a function

#include <iostream>
#include <math.h>
#define PI 3.141592654
#define g 9.81
using namespace std;

void calcV(double hg, double k, double Q, double nu){


double v;
cout <<"please enter v.\n";
cin >> v;
cout <<"v="<<v<<"\n";
double D=sqrt(4*Q/(PI*v));
cout <<"D="<< D <<"\n";
double Re=v*D/nu;
double Lmd=(0.25/(
pow(
log(
5.1286/pow(Re,0.89)+k/(3.7*D*1000)
)
,2)
));
double vf=sqrt(2*g*D*hg/Lmd);
cout <<"vf="<< vf <<"\n";
}

int main(){
/*input Q, S*/

double Q;
cout <<"please enter Q.\n";
cin >> Q;
cout <<"Q="<<Q<<"\n";
double S;
cout <<"please enter S.\n";
cin >> S;
cout <<"S="<<S<<"\n";
cout <<"Q=,S="<< Q << ","<< S <<"\n";
double k=0.1;
double nu=1.31E-6;
for(int i=0;i<100;i++){
calcV(S,k,Q,nu);
}
}

Version 5: Return a value from a function

#include <iostream>
#include <math.h>
#define PI 3.141592654
#define g 9.81
using namespace std;

double calcV(double hg, double k, double Q, double nu, double v){


double D=sqrt(4*Q/(PI*v));
cout <<"D="<< D <<"\n";
double Re=v*D/nu;
double Lmd=(0.25/(
pow(

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 5 of 14

log(
5.1286/pow(Re,0.89)+k/(3.7*D*1000)
)
,2)
));
double vf=sqrt(2*g*D*hg/Lmd);
return vf;

int main(){
/*input Q, S*/

double Q;
cout <<"please enter Q.\n";
cin >> Q;
cout <<"Q="<<Q<<"\n";
double S;
cout <<"please enter S.\n";
cin >> S;
cout <<"S="<<S<<"\n";
cout <<"Q=,S="<< Q << ","<< S <<"\n";
double k=0.1;
double nu=1.31E-6;
for(int i=0;i<100;i++){
double v;
cout <<"please enter v.\n";
cin >> v;
cout <<"v="<<v<<"\n";
double newvelocity=calcV(S,k,Q,nu,v);
cout <<"vf="<< newvelocity <<"\n";
}
}

Version 6: Automatic iteration based on precision

#include <iostream>
#include <math.h>
#define PI 3.141592654
#define g 9.81
using namespace std;

double calcV(double hg, double k, double Q, double nu, double v){


double D=sqrt(4*Q/(PI*v));
cout <<"D="<< D <<"\n";
double Re=v*D/nu;
double Lmd=(0.25/(
pow(
log(
5.1286/pow(Re,0.89)+k/(3.7*D*1000)
)
,2)
));
double vf=sqrt(2*g*D*hg/Lmd);
return vf;

int main(){
/*input Q, S*/

double Q;
cout <<"please enter Q.\n";
cin >> Q;
cout <<"Q="<<Q<<"\n";
double S;
cout <<"please enter S.\n";
cin >> S;
cout <<"S="<<S<<"\n";
cout <<"Q=,S="<< Q << ","<< S <<"\n";
double k=0.1;

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 6 of 14

double nu=1.31E-6;
double v;
cout <<"please enter v.\n";
cin >> v;
cout << "Loop starting ...";
for(int i=0;i<1000;i++){
double newvelocity=calcV(S,k,Q,nu,v);
cout <<"i,v,newvelocity"
<< i << ", "<< v << ", "<< newvelocity <<"\n";
if(abs(v-newvelocity)<.0001){
break;
}
v=newvelocity;
}
}

Scope of variables
#include <iostream>
using namespace std;

void setval(){
double iue;
iue=1;
cout << "value inside: " << iue << "\n";
}

int main(){
double iue=3.0;
cout << "value in main 1st time: " << iue << "\n";
setval();
cout << "value in main 2nd time: " << iue << "\n";

cout << "##################################\n";

int i;
i=100;
cout << "i=" << i << "\n";
for(int i=0;i<5;i++){
cout << "i=" << i << "\n";
}
cout << "i=" << i << "\n";

Arrays
10 numbers

#include <iostream>
using namespace std;
/*
Read 10 numbers from the user and computes the average.
The dumb way ... using an array and two for loops!!*/
int main(){
double marks[10];
for(int i=0;i<10;i++){
cout << "Enter the " << i << "th value:";
cin >> marks[i];
cout << "\n";
}
cout << "reading done.\n";
// now let's calculate aveage
double sum=0;
for(int i=0;i<10;i++){

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 7 of 14

sum=sum+marks[i];
}
cout << "The average : " << sum/10;
}

Variable number of items -- but subject to a maximum

#include <iostream>
using namespace std;
/*
Read 10 numbers from the user and computes the average.
The dumb way ... using an array and two for loops!!*/
int main(){
double marks[1000];
int count;
cout << "Enter marks. When you are done enter negative value.\n";
for(int i=0;i<1000;i++){
cout << "Enter the " << i << "th value:";
cin >> marks[i];
cout << "\n";
if(marks[i]<0){
count=i;
break;
}
}
cout << "reading done.\n";
// now let's calculate aveage
double sum=0;
for(int i=0;i<count;i++){
sum=sum+marks[i];
}
cout << "The average : " << sum/count;
}

Multi-dimensional arrays

/* A program that reads 4x4 matrix coefficients and print them nicely. */
#include <iostream>
using namespace std;

int main(){
double matrix[4][4];
for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cout << "\nEnter the value (" << i << "," << j << ")th place:";
cin >> matrix[i][j];
}
}
cout << "Reading done.\n";

//now print this


for(int i=0;i<4;i++){
for(int j=0;j<4;j++){
cout << matrix[i][j] << "\t";
}
cout << "\n";
}

Vectors

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 8 of 14

Example 1

/** A football team plays in a number of matches in a given season.


At the end of each match, we enter the number of goals scroed.
Later we produce the cumulative number of goals against the match number.
**/

#include <iostream>
#include <vector>
using namespace std;

int main(){
//first we define a storage
vector<int> scores;
//^^ a bag of integers named scores
int value;
do{
cout << "\nEnter the next score: ";
cin >> value;
scores.push_back(value);
}while (value>=0);
cout << "Done!";
scores.pop_back();
cout << "Done!";

// lets say we want to remove the second match's score before proceeding further.
scores.erase(scores.begin()+1);

int total=0;
for(int i=0;i<scores.size();i++){
total=total+scores[i];
cout << "Total :\t" << total << "\n";
}

Example 2

/** A football team plays in a number of matches in a given season.


At the end of each match, we enter the number of goals scroed.
Later we produce the cumulative number of goals against the match number.
**/

#include <iostream>
#include <vector>
using namespace std;

int main(){
//first we define a storage
vector<int> scores;
//^^ a bag of integers named scores
int value;
do{
cout << "\nEnter the next score: ";
cin >> value;
scores.push_back(value);
}while (value>=0);
cout << "Done!";
scores.pop_back();
cout << "Done!";

/* // lets say we want to remove the second match's score before proceeding further.
scores.erase(scores.begin()+1);
*/
int total=0;
for(int i=0;i<scores.size();i++){
total=total+scores[i];
cout << "Total :\t" << total << "\n";

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 9 of 14

}
// now lets find the maximum score
int max=-999999999;
int min=9999999999;
for(int i=0;i<scores.size();i++){
if(scores[i]>max){
max=scores[i];
}
if(scores[i]<min){
min=scores[i];
}
}

cout << "Max score: " << max << "\n";


cout << "Min score: " << min << "\n";
}

File I/O
Reading from files

#include <iostream>
#include <fstream>
using namespace std;

int main(){
ifstream fs;
fs.open("../data/moddata.txt");
if(!fs.is_open()){
cout << "Error. I can't find the file!";
exit(5);
}

int num;
double val;
double sum=0;
int count=0;
while(fs >> num >> val){
if(val<0){
//ignore this value.
continue;
}
sum=sum+val;
count++;
if(count%10000==0){
cout << "Iteraion: " << count << "\n";
}
}

cout << "The mean is: " << sum/count;

Functions
Using multiple files to define a program

File1

#include <iostream>
using namespace std;

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 10 of 14

int Calc();
int Calc2();
double Calc3();
double Raise(double );
/* Raise is a function that
takes ONE double as the argument
and returns a double
*/

int main(){
int a=Calc();
int b;
double c;
b=Calc2();
c=Calc3();
double p=5.2;
double ans=Raise(p);
cout << a << "\n";
cout << b << "\n";
cout << c << "\n";
cout << ans << "\n";

File 2

#include<math.h>
int Calc(){
int k=5*2;
return k;
}
int Calc2(){
return 6;
}

double Calc3(){
return 5.3;
}

double Raise(double k){


return pow(k,5);
}

With a Header file

Note
In visual c++ 2008 (express), include the path of the header file with properties to get this code
running.
File1

#include <iostream>
using namespace std;
#include <slave.h>

int main(){
int a=Calc();
int b;
double c;
b=Calc2();
c=Calc3();
double p=5.2;
double ans=Raise(p);
cout << a << "\n";
cout << b << "\n";
cout << c << "\n";

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 11 of 14

cout << ans << "\n";

File2

/** slave.cpp **/


#include<math.h>
int Calc(){
int k=5*2;
return k;
}
int Calc2(){
return 6;
}

double Calc3(){
return 5.3;
}

double Raise(double k){


return pow(k,5);
}

File3

/** slave.h **/


int Calc();
int Calc2();
double Calc3();
double Raise(double );
/* Raise is a function that
takes ONE double as the argument
and returns a double
*/

Prime Numbers

#include <iostream>
using namespace std;

bool isprime(int number);

int main(){
for(int i=1;true; i++){
if(isprime(i)){
cout << i << "\n";
}
}
}

bool isprime(int number){

for(int i=2;i<number;i++){
if(number%i==0){
return false;
}
}
return true;
}

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 12 of 14

extern
#include <iostream>
using namespace std;

extern int a;

void dumb(){
cout << "a=" << a << "\n";
}

#include <iostream>
using namespace std;
void dumb();

int a=5;

int main(){
dumb();
}

Pointers
Passing by value and passing by reference

#include <iostream>
using namespace std;

void add5(int * a){


*a=*a+5;
}

int main(){
int value=25;
add5(&value);
cout << "answer is " << value;
}

Passing an array

#include <iostream>
using namespace std;

/* calculates the average value of the numbers stored in an array


divides each number by average*/
void normalize(double * arr, int length){
double sum=0;
for(int i=0;i<length;i++){
sum=sum+*(arr+i);
}
sum=sum/10;
for(int i=0;i<length;i++){
*(arr+i)=*(arr+i)/sum;
}

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 13 of 14

int main(){
double values[]={1,2,5,6,8,9,11,12,12,14};
normalize(values,10);
for(int i=0;i<10;i++){
cout << values[i] << "\n";
}
}

Structures
#include <iostream>
#include <string>
using namespace std;

struct pipe{
string ID;
double len;
double dia;
string mat;
};

int main(){
pipe mypipes[5];
for(int i=0;i<5;i++){
cout << "Enter next pipe ";
cout << "\nID:";
cin >> mypipes[i].ID;
cout << "\nlen:";
cin >> mypipes[i].len;
cout << "\ndia:";
cin >> mypipes[i].dia;
cout << "\nMat:";
cin >> mypipes[i].mat;
}

for(int i=0;i<5;i++){
cout << mypipes[i].ID << "\t"
<< mypipes[i].len << "\t"
<< mypipes[i].dia << "\t"
<< mypipes[i].mat << "\nt" ;
}

Epanet with c/c++


File:Visual studio 2008 project to run this.zip

#include <iostream>
#include "epanet2.h"

using namespace std;


void call_epanet();
int main(){
call_epanet();
}

void call_epanet(){
//we call epanet
int res;
res= ENopen( "../data/net1.inp", "../data/net1.rpt","");
cout << "epanet returned: "<< res<<"\n";
res= ENsolveH();
cout << "epanet returned: "<< res<<"\n";

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015
C Plus Plus Tutorial/2009 Run - Intra Page 14 of 14

res=ENclose();
cout << "epanet returned: "<< res<<"\n";

EPA SWMM with C/C++


◾ data : File:Data4epaswmmwithc.zip
◾ code: File:Swmmwithc.zip
◾ code(Final): File:Swmmwithc final.zip

Retrieved from "https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run"

◾ This page was last modified on 29 May 2009, at 14:29.


◾ Content is available under License.

https://dimos.ihe.nl/intra/index.php/C_Plus_Plus_Tutorial/2009_Run 2/5/2015

You might also like