[go: up one dir, main page]

0% found this document useful (0 votes)
41 views11 pages

Oop Lab Report 1, Fa20-Bee-3c-146

Uploaded by

Souban Javed
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)
41 views11 pages

Oop Lab Report 1, Fa20-Bee-3c-146

Uploaded by

Souban Javed
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/ 11

CSC 241 Object Oriented Programming

Lab 01 – Review of Basic Programming Concepts

Name: Muhammad Souban Javaid


Roll no: FA20-BEE-146
Section: 3C
Submitted to: Madam Mehwish Mehmood
Dated: 23-09-2021

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>
#include<conio.h>
using namespace std;
struct Book
{
int b_id;
char b_name[30];
int price;
};
struct Order
{int j;
int order_id;
Book b[100];
};
int main()
{
Order c;
cout<<"Insert the number of books :";
int n;
cin>>n;
cout<<"Insert the order Id: ";
cin>>c.order_id;
cout<<"Insert the details of books: \n";
for(int j=0;j<n;j++)
{
cout<<"\nInsert the book Id: ";
cin>>c.b[j].b_id;
cout<<"Insert the book name: ";
cin>>c.b[j].b_name;
cout<< " Insert the price of the book: ";
cin>>c.b[j].price;
}
cout<<"\n The detail of order is: "<<endl;
cout<<" Order Id: "<<c.order_id<<endl;
cout<<" Book Id\t Book Name \t Price "<<endl;
for(int j=0;j<n;j++)
{
cout<<c.b[j].b_id<<"\t\t"<<c.b[j].b_name<<"\t\t"<<c.b[j].price<<endl;
}
int j ;
int x=0;
for(int j=0;j<n;j++)
if(c.b[0].price<c.b[j].price)
x=j;
cout<<"Most expensive book is :\n";
cout<<c.b[x].b_id<<"\t\t"<<c.b[x].b_name<<"\t\t"<<c.b[x].price<<endl;
}

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 *ptr;
int a,b;
cout<<"Insert the first integer:";
cin>>a;
cout<<endl;
cout<<"Insert the second integer:";
cin>>b;
cout<<endl;
ptr=&a;
*ptr=*ptr+10;
cout<<"The value of a is:"<<a<<endl;
ptr=&b;
*ptr=*ptr+10;
cout<<"The value of b is:"<<b<<endl;
}

5.3. Write a function that swaps the values of two integer variables
a. using pass by value
b. and pass by reference and see their differences
#include <iostream>
using namespace std;
void swap(int x, int y);
int main()
{
int a,b;
cout<<"Insert the numbers"<<endl;
cin>>a>>b;
cout<<"Values before swapping:"<<endl<<a<<endl<<b<<endl;
swap(a,b);
return 0;
}
void swap(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"Values after swapping:"<<endl<<x<<endl<<y;
}

#include <iostream>
using namespace std;
void swap(int *i, int *j);
int main()
{
int a,b;
cout<<"Insert the numbers"<<endl;
cin>>a>>b;
cout<<"Values before swapping:"<<endl<<a<<endl<<b<<endl;
swap(&a,&b);
cout<<"Values after swapping:"<<endl<<a<<endl<<b;
return 0;
}
void swap(int *i, int *j)
{
int temp;
temp=*i;
*i=*j;
*j=temp;
}
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>
using namespace std;
struct employee
{
int dd,mm,yy;
char code[10],name[10];
} emp[10];
int main()
{
int n,i,dd,mm,yy;
cout<<"Insert the number of employees:";
cin>>n;
///Reding Data of n Employees/
for(i=0; i<n; ++i)
{
cout<<"\nInsert the Emp code:";
cin>>emp[i].code;
cout<<"Insert the Emp name:";
cin>>emp[i].name;
cout<<"Insert the joining date dd mm yyyy:";
cin>>emp[i].dd>>emp[i].mm>>emp[i].yy;
}
cout<<"\nInsert the current date:(dd/mm/yyyy)";
cin>>dd>>mm>>yy;
///Check Employees working for >= 3 years/
for(i=0; i<n; ++i)
if((yy - emp[i].yy) >= 3)
cout<<"Names of employees:"<<emp[i].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 namespace std;

// Simulation of input by value


void sortArray1(double arr[10] , int size)
{
for (int i = 0; i < size - 1; ++i)
{
int small = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] < arr[small])
small = j;
}
double temp=arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}

// Simulation of input by reference


void sortArray2(double arr[], int size)
{
for (int i = 0; i < size - 1; ++i)
{
int small = i;
for (int j = i + 1; j < size; ++j)
{
if (arr[j] < arr[small])
small = j;
}
double temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
int main()
{
// Example
double arr1[10] ={ 1.2, 34, 3.7, 48.6, -5, 9, 14.87, 17,0.89,1 };
sortArray1(arr1, 10);
// Output in display
for (int i = 0; i < 10; ++i)
std::cout << arr1[i] << " ";
std::cout << "\n";

double arr2[10] = { 1.2, 34, 3.7, 48.6, -5, 9, 14.87, 17,0.89,1 };


sortArray2(arr2, 10);
// output in display
for (int i = 0; i < 10; ++i)
std::cout << arr2[i] << " ";
return 0;
}

6.3. Write a program that inputs a string value from the user and displays it in reverse
using pointer.
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char *str="Comsats University";
cout<<"Original string::"<<str;
cout<<endl<<"String after reversing::";
for(int i=(strlen(str)-1);i>=0;i--){
cout<<str[i];
}
return 0;
}

Conclusion: We went over concepts like structures, pointers, and passing function arguments by
value and by reference in this lab. We learned how to create structures and how to use them.
Declaration, initialization and use of pointers were also covered.
-------------------------------------------------------------------------------------------------------

You might also like