Lecture#01
Object Oriented Programming
Topic: Loops & Functions
ASMA JADOON
Output:
#include <iostream>
1 1
Loops using namespace std;
Multiple initialization #include <iomanip>
2 8
• Program to be repeated a certain
and test number of times.using namespace std;
expression
int main() {
3 27
• The repetition continues with a condition. int numb;
Output: for(numb=1; numb<=10; numb++)
4 • 64
0 1 4 9There
16 25 are
36 49three kinds
64 81 100of
121loops
144 in
169C++:
196 {
i. for loop, cout << setw(4) << numb;
5 125
int cube = numb*numb*numb;
ii. the while loop, cout << setw(6) << cube << endl; //display 2nd column
6 216
iii. the do loop. }
#include return 0; }
7 343<iostream>
using namespace std;
int 8main()
512 {
int j;
for(j=0;
9 729 j<15; j++)
cout << j * j << " "; //displaying the square of j
cout
10 1000<< endl;
return 0;
}
Output: #include <iostream>
#include <iomanip>
The while Loop
1 1 using namespace std;
int main() {
2 16 int pow=1;
#include <iostream> • simplifiedintversion
numb=1; of the for loop.
3 81using namespace std; while(
• contains just testpow<10000
expression)
int main() { {
4 256int n = 99; cout << setw(2) << numb;
while( n != 0 ) cout << setw(5) << pow << endl; //display fourth power
5 625 cin >> n; ++numb;
cout << endl; pow = numb*numb*numb*numb;
6 1296 return 0; }
} cout << endl;
7 2401 return 0;
}
8 4096
9 6561
The do Loop:
#include <iostream>
using namespace std;
int main() {
long dividend, divisor;
char ch;
do
{
cout << "Enter dividend: "; cin >> dividend;
cout << "Enter divisor:"; cin >> divisor;
cout << "Quotient is "<< dividend / divisor;
cout << ", remainder is " << dividend % divisor;
cout << "\nDo another? (y/n): "; //do it again?
cin >> ch;
}
while( ch != 'n'); //loop condition
return 0;
}
#include <iostream>
using namespace std;
Functions : void starline();
int main() {
starline();
• A function groups a number of program statements coutinto
<<a"Data
unit. type Range" << endl;
starline();
• This unit can then be invoked from other parts of the program.
cout << "char -128 to 127" << endl
• more powerful way to organize programs. << "short -32,768 to 32,767" << endl
<< "int System dependent" << endl
• << "long
Another reason to use functions is to reduce pro- gram size. -2,147,483,648 to 2,147,483,647" << endl;
starline();
return 0;
}
// function definition
void starline()
{
for(int j=0; j<45; j++)
cout << '*';
cout << endl;
}
Calling the Function:
• The function is called, invoked or executed from main().
The Function Definition:
• The definition consists of a line called the declarator, followed by the function body
• The declarator must agree with the declaration:
• The declarator is not terminated by a semicolon.
struct Distance //English distance
{
• int feet;
Passing Arguments to Functions
Passing Constants Enter feet: 4
• float inches;
Passing Variables
};
• Passing by Value
void engldisp( Distance ); //declaration Enter inches: 8
• Structures as Arguments
int main() {
Distance d1, d2; //define two lengths Enter feet: 5
//get length d1 from user
cout << "Enter feet: "; cin >> d1.feet; Enter inches: 8
• cout
An <<
argument is a piece
"Enter inches: of >>
"; cin datad1.inches;
passed from a program to the function.
//get length d2 from user d1 = 4'-8"
• Arguments allow a function to operate with different values, or even to do different things, depending on
cout << "\nEnter feet: "; cin >> d2.feet;
the requirements
cout of the
<< "Enter inches: program
"; cin calling it.
>> d2.inches; d2 = 5'-8"
cout << "\nd1 = ";
engldisp(d1);
cout << "\nd2 = ";
engldisp(d2);
cout << endl;
return 0;
}
void engldisp( Distance dd ) //parameter dd of type Distance
{
cout << dd.feet << "\'-" << dd.inches << "\"";
}
#include <iostream>
using namespace std;
Structure:
struct part {
int modelnumber;
int partnumber;
float cost;
• A structure is a collection of simple variables.
};
//declare
• The variables in a structure can be of different types.a structure
//ID number of widget
• The data items in a structure are called the members
//ID number of the structure.
of widget part
//cost of part
////////////////////////////////////////////////////////////////
int main()
{
part part1; //define a structure variable
part1.modelnumber = 6244; //give values to structure members
part1.partnumber = 373;
part1.cost = 217.55F;
//display structure members
cout << "Model " << part1.modelnumber;
cout << ", part " << part1.partnumber;
cout << ", costs $" << part1.cost << endl;
return 0;
}
Array Fundamentals
#include <iostream>
using namespace std;
int main() {
int age[4],j;
for(j=0; j<4; j++)
{
cout << "Enter an age: ";
cin >> age[j];
}
for(j=0; j<4; j++)
cout << "You entered " << age[j] << endl;
return 0; }
#include <iostream> Enter sales for district 2, month 1: 645
#include <iomanip> for(d=0; d<DISTRICTS; d++)
using namespace std; Enter sales for district 2, month 2: 233 {
const int DISTRICTS = 4; cout <<"\nDistrict " << d+1;
const int MONTHS = 3; Enter sales for district 2, month 3: 876 for(m=0; m<MONTHS; m++)
int main() { cout << setiosflags(ios::fixed) //not exponential
//for setprecision, etc. Enter sales for district 3, month 1: 987<< setiosflags(ios::showpoint) //always use point
//array dimensions << setprecision(2)
int d, m; Enter sales for district 3, month 2: 999 << setw(10)
double sales[DISTRICTS][MONTHS]; //two-dimensional << sales[d][m];
array Enter sales for district 3, month 3: 888} //end for(d)
//definition cout << endl;
cout << endl; Enter sales for district 4, month 1: 000 return 0;
for(d=0; d<DISTRICTS; d++) //get array values }
for(m=0; m<MONTHS; m++) Enter sales for district 4, month 2: 6554
{
Enter sales
cout << "Enter sales for district " << for
d+1;district 4, month 3: 5425
cout << ", month " << m+1 << ": ";
cin >> sales[d][m]; Months
}
cout << "\n\n"; 123
cout << "Months\n";
cout << "1 2 3"; District 1 345.00 567.00 456.00