Lec1 Oop
Lec1 Oop
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>
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;
• The definition consists of a line called the declarator, followed by the function body
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