College of Engineering & Information Technology
ELE 101 Computer Programming
Tutorial Sheet # 11
Course Learning Outcomes (CLOs)
1. Explain the basics of C programming. X
2. Prepare a flowchart and apply procedure-oriented
programming using the C language.
3. Program in a structured manner by dividing C programs into
subprograms. X
4. Solve problems involving vectors and matrices using C. X
5. Solve basic problems in electrical and biomedical
engineering through C programming assignments and X
course projects.
File input/output operation in C++
Example1: (Prj. 93) to read the data for radius of a circle from
a input text file, then after compilation storing the area and
circumference of the circle in another output text file , also
display on the console as well.
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double radius, area, circumference;
double const PI = 3.14;
ifstream fin;
ofstream fout;
fin.open("circlei.txt");
fout.open("circleo.txt");
fin >> radius;
fout << "The radius is " << radius << endl;
cout << "The radius is " << radius << endl;
fin.close();
area = PI * radius * radius;
circumference = 2 * PI * radius;
fout << "The area is " <<area << endl;
fout << "The circumference is " << circumference << endl;
fout.close();
cout << "The area is " << area<<endl;
cout << "The circumference is " << circumference << endl;
system("pause");
}
circleo.txt
The radius is 4
The area is 50.24
The circumference is 25.12
circlei.txt
4
6
8
Example2: (Prj. 107) to read the temperature data from a
input text file, then after compilation storing the average
temperature value in another output text file , also display on
the console as well.
#include <iostream>
#include <fstream>
using namespace std;
void main()
{
double temp[5];
int i;
double sum = 0;
ifstream fin;
ofstream fout;
fin.open("temp_input.txt");
fout.open("temp_output.txt");
for (i = 0; i < 5;i++)
{
fin >> temp[i];
sum = sum + temp[i];
}
fout << "The average temperature is :" << sum/5 << endl;
cout<< "The average temperature is :" << sum/5 << endl;
fin.close();
fout.close();
system("pause");
}
temp_output.txt
The average temperature is :22.8
temp_input.txt
30
-45
27
20
37
Data Structure in C++
Example 3: (prj. 97) Use of structure to store the specifications
of two branch resistances
#include<iostream>
using namespace std;
struct resistor
{
char type[10];
int value;
float tol;
};
void main()
{
resistor R1, R2;
cout << "Enter types : ";
cin >> R1.type >> R2.type;
cout << "Enter values : ";
cin >> R1.value >> R2.value;
cout << "Enter tolerances : ";
cin >> R1.tol >> R2.tol;
cout << "Resistor R1 specs : \n";
cout << R1.type << endl << R1.value << endl << R1.tol << endl;
cout << "Resistor R2 specs : \n";
cout << R2.type << endl << R2.value << endl << R2.tol << endl;
system("pause");
}
Enter types : branch1
branch2
Enter values : 45
67
Enter tolerances : .006
.003
Resistor R1 specs:
branch1
45
0.006
Resistor R2 specs:
branch2
67
0.003
Example 4: (prj.98) use of structure to store the birth date
#include <iostream>
using namespace std;
void main()
{
struct
{
int month;
int day;
int year;
} birth_mohammed;
birth_mohammed.month = 12;
birth_mohammed.day = 28;
birth_mohammed.year = 86;
//birth_mohammed = { 12, 28, 86 };
cout << "Mohammed's birth date is "
<< birth_mohammed.month << '/'
<< birth_mohammed.day << '/'
<< birth_mohammed.year << endl;
system("pause");
}
Mohammed’s birth date is 12/28/86
Example: 5 (prj. 106) use of structure to store students I.D,
name and marks
#include <iostream>
#include<string>
using namespace std;
void main()
{
struct
{
string id;
string name;
int marks;
} student1, student2;
student1 = { "I.D #120090", "Syed Mohammed", 86 };
student2 = { "I.D #239078", "Omar Mohammed", 90 };
cout << "The records for students are: " << endl;
cout << "student 1:" << endl;
cout<< student1.id << endl;
cout << student1.name << endl;
cout << student1.marks << endl;
cout << "student 2:" << endl;
cout << student2.id << endl;
cout << student2.name << endl;
cout << student2.marks << endl;
system("pause");
}
The records for students are:
student 1:
I.D #120090
Syed Mohammed
86
student 2:
I.D #239078
Omar Mohammed
90
Example 6: (prj.99) Following is a list of employee data in a company.
Use structure to display the first five records of the employees.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int NUMRECS = 5; // maximum number of records
struct PayRec // this is a global declaration
{
int id;
string name;
double rate;
};
void main()
{
int i;
PayRec employee[NUMRECS] = {
{ 32479, "Abrams, B.", 16.72 },
{ 33623, "Bohm, P.", 17.54 },
{ 34145, "Donaldson, S.", 15.56 },
{ 35987, "Ernst, T.", 15.43 },
{ 36203, "Gwodz, K.", 18.72 }
};
cout << endl; // start on a new line
for (i = 0; i < NUMRECS; i++)
cout << setw(7) << employee[i].id
<< setw(15) << employee[i].name
<< setw(6) << employee[i].rate << endl;
system("pause");
}
Output:
Array
Example 7: (prj. 103) Write a program in C++ to store 6 user
input numbers using an array and display the summation of
even and odd numbers separately.
include<iostream>
using namespace std;
void main()
{
int x[6] ;
int ev_sum = 0, odd_sum=0;
cout << "input the elements" << endl;
for (int i=0; i<6; i++)
{
cin >> x[i];
cout<< endl;
}
for (int i = 0; i<6; i++)
{
if (x[i] % 2==0)
ev_sum = ev_sum + x[i];
else
odd_sum = odd_sum + x[i];
}
cout << "The summation of even number is :" << ev_sum
<<endl;
cout << "The summation of odd number is :" << odd_sum <<
endl;
system("pause");
}
input the elements
7
9
2
3
0
5
The summation of even number is : 2
The summation of odd number is : 24