You are on page 1of 16

Object Oriented Programming CSC241

Lab NO:01

Name KHIZER HAYAT SATTI

FA20-EEE-012
Registration Number

Class EEE-3

Instructor’s Name NAYAB GOGOSH


5. Lab Tasks

5.1. Write a program that declares a structure to store book Id, price and pages of a book. The
structure should include functions to assign user defined values to each book and display the record
of most costly book.

#include <iostream>

using namespace std;

int n;

struct Book

int Bookid; float

Bookprice; int

BookNop; }pk[20]; void

assign_value(int n)

int i;

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

cout<<"Enter the Book ID:";

cin>>pk[i].Bookid; cout<<"\nEnter the Book

Price:"; cin>>pk[i].Bookprice;

cout<<"\nEnter the No of Pages in a book:";

cin>>pk[i].BookNop;

void display(int n)

{ int

j,k,i;
float lar=pk[0].Bookprice; for(k=0;k<j;i++)

if(pk[i].Bookprice>lar)

lar=pk[i].Bookprice;

j=k;

cout<<"Most costly Book details "<<endl; cout<<"Book

ID: "<<pk[j].Bookid<<endl; cout<<"Book Price:

"<<pk[j].Bookprice<<endl; cout<<"No of pages in a

Book: "<<pk[j].BookNop<<endl;

int main()

cout<<"Enter the number of Books:";

cin>>n; assign_value(n); display(n);

return 0;

5.2. Write a program to take the values of two integers and use pointers to add 10 to the value of
each integer.

#include <iostream>
using namespace std;

int main()

int n1,n2; int


*ptr;

cout<<"Enter the value of number1:"; cin>>n1;

cout<<"\nEnter the value of number2:"; cin>>n2;

ptr=&n1; *ptr=*ptr+10; cout<<"after adding 10 to

number 1 using pointer"; cout<<"\nthe value of

number 1:"<<n1; ptr=&n2;

*ptr=*ptr+10;
cout<<"after adding 10 to number 2 using pointer";
cout<<"\nthe value of Number 2:"<<n2;

return 0;

5.3. Write a function that swaps the values of two integer variables

a. using pass by value

#include <iostream>

using namespace std; void

swap_int(int a,int b)
{

int k;

k=a; a=b;

b=k;

int main()

int n1,n2; cout<<"Enter the value of number 1:"; cin>>n1;

cout<<"\nEnter the value of number 2:"; cin>>n2;

cout<<"before swapping"; cout<<"\nthe value of Number

1:"<<n1 <<"\t number 2:"<<n2;

swap_int(n1,n2); cout<<"after swapping"; cout<<"\nthe value

of Number 1:"<<n1 <<"\t number 2:"<<n2;

return 0;
}

5.3 Write a function that swaps the values of two integer variables

b. and pass by reference and see their differences

#include <iostream>

using namespace std; void

swap_int(int *a,int *b)

int k;

k=*a;

*a=*b;

*b=k;

}
int main()

int n1,n2; cout<<"Enter the value of number 1:"; cin>>n1;

cout<<"\nEnter the value of number 2:"; cin>>n2;

cout<<"before swapping"; cout<<"\nthe value of Number

1:"<<n1 <<"\t number 2:"<<n2; swap_int(&n1,&n2);

cout<<"after swapping"; cout<<"\nthe value of Number

1:"<<n1 <<"\t number 2:"<<n2;

return 0;

}
6. Home Tasks
6.1. There is a structure called employee that holds information like employee code, name, date of
joining. Write a program to create an array of the structure and enter some data into it. Then ask the
user to enter current date. Display the names of those employees whose tenure is 3 or more than 3
years according to the given current date.

#include <iostream>

#include <cstring>

#include <cmath>

using namespace std;

struct employee

int code;

string name;

int DD,MM,YYYY;

};

int main(){

struct employee emp[3];


int k;

for(k=0; k<3; k++){

cout << "Employee " << k + 1 << endl;

cout << "Enter code" << endl;

cin >> emp[k].code;

cout << "Enter name" << endl;

cin >> emp[k].name;

cout << "Enter Date of Joining (Format : DD MM YYYY)" << endl;

cin >> emp[k].DD>>emp[k].MM>> emp[k].YYYY;

int DD,MM,YYYY;

cout << "Enter Current Date (Format : DD MM YYYY)" << endl;

cin >> DD >> MM >> YYYY;

for(int K=0;K<3;k++){
if(abs(emp[k].YYYY-YYYY)>=3)

cout<<emp[k].name<<endl;

return 0;

}
6.2. Write a function to sort data (in increasing order) in an array using

a. pass by value b. and pass by reference.

#include <iostream> using std::cout;


using std::endl;

#include <iomanip> using


std::setw;

void selectionSort( int * const, const int ); void


swap( int * const, int * const );

int main()
{
const int arraySize = 10; int a[ arraySize ] =
{3,9,2,7,48,21,15,98,75,67};

cout << "Data items in original order\n";

for ( int i = 0; i < arraySize; i++ ) cout


<< setw( 4 ) << a[ i ];

selectionSort( a, arraySize );

cout << "\nData items in ascending order\n";

for ( int j = 0; j < arraySize; j++ )


cout << setw( 4 ) << a[ j ];
cout << endl; return
0;
}

void selectionSort( int * const array, const int size )


{
int smallest;

for ( int i = 0; i < size - 1; i++ )


{
smallest = i;

for ( int index = i + 1; index < size; index++ )

if ( array[ index ] < array[ smallest ] ) smallest


= index;

swap( &array[ i ], &array[ smallest ] );


}
}

void swap( int * const element1Ptr, int * const element2Ptr )


{
int hold = *element1Ptr;
*element1Ptr = *element2Ptr;
*element2Ptr = hold;
}

6.3. Write a program that inputs a string value from the user and displays it in reverse using pointer.

#include <stdio.h>

#include <string.h>

void reverseString(char* str)

{ int l,

i;

char *begin_ptr, *end_ptr, ch;

l = strlen(str);
begin_ptr = str; end_ptr

= str;

for (i = 0; i < l - 1; i++)

end_ptr++;

for (i = 0; i < l / 2; i++) {

ch = *end_ptr;

*end_ptr = *begin_ptr;

*begin_ptr = ch;

begin_ptr++; end_ptr--;

int main()

char str[100] = "liamuk"; printf("Enter

a string: %s\n", str);

reverseString(str); printf("Reverse of the string: %s\n", str);

return 0;

}
Conclusion:-
In this lab I learned the basic concept such as structures,
pointers, passing function arguments by value and by reference.
However, I also learn the operator asterisk (*) to use as pointer. It
is also used to input the value in the variable and process the data
stored in the variable.

You might also like