You are on page 1of 8

Air University, Islamabad

Department of Computer Science

Name: Zain Zahid


ID: 150900
Section : BSCS-II-A

OOP LAB 1
Exercise 1
Write a program in C++ which carries out following tasks
- Create two integer variables (A and B) and assign values to them
- Print values of both variables on screen
- Print address of both variables
- Create an integer pointer and point it to first integer variable (A)
- Print first variable (A) value using pointer
- Point/Direct pointer variable to second variable
- Print the value where pointer points to
- Print address and value of pointer
- Program should not exit unless user decides to
- Create second pointer and point pointer1 and pointer 2 to first variable
- Create Change value of value of first pointer and print value, change value using
second pointer and print value of variable
#include <iostream>
using namespace std;
int main() {
int a,b;
a=10;
b=11;
cout<<"Values:\n"<<a<<"\t"<<b;
cout<<"\nAddress:\n"<<&a<<"\t"<<&b;

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science
int *ptr;
ptr=&a;
cout<<"\nValue of First Variable a:"<<*ptr;
cout<<"\nAddress of First Variable a:"<<ptr;
ptr=&b;
cout<<"\nValue of Second Variable b:"<<*ptr;
cout<<"\nAddress of Second Variable b:"<<ptr;
int *ptr2;
ptr=ptr2=&a;
*ptr=12;
cout<<"\n First Value "<<a;
*ptr2=13;
cout<<"\n Second Value "<<a;

return 0;

Exercise 2
Write a program which reads two integer type values from user and displays the
values. Create a function to swap the values of both variables using pointers and
display to user. Program should repeat itself until wants to exit.
#include <iostream>
using namespace std;
void swap(int *ptr1,int *ptr2)
{
int temp;
temp=*ptr1;
*ptr1=*ptr2;
*ptr2=temp;
}
int main() {
int a,b;
cout<<"Enter the Value 1";
cin>>a;
cout<<"Enter the Value 2";
cin>>b;
swap(&a,&b);
cout<<"\nValue of a:"<<a;
cout<<"\nValue of b:"<<b;
return 0;
}

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science

Exercise 3
Write C++ program which reads a number from user and makes use of user defined
function which accepts an integer pointer. This function should increment or decrement
sent value by one depending on user choice. User should pass value by reference to the
function and the option as an argument (1 to increment and 2 to decrement value).
Program should repeat itself without user choice.
Custom function signature is:
Void IncrementDecrement ( int * number, int operation );
#include <iostream>
using namespace std;
void IncrementDecrement(int *number,int c)
{
if(c==1)
{
*number=*number+1;
}
if(c==2)
{
*number=*number-1;
}
}
int main() {
int n,c,choice;
cout << "Enter the number" << endl;
cin>>n;
cout<<"\nEnter 1 for increment or 2 for decrement";
cin>>c;
if(c==1)
{
choice=1;
}
if(c==2)
{
choice=2;
}
IncrementDecrement(&n,choice);
cout<<"\nNumber After increment or decrement is"<<n;
return 0;
}

Exercise 4

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science
Write C++ program which reads a number from user and makes use of user defined
function to calculate factorial. Please pass argument by reference (using pointer) and
display result (factorial) in main function.
#include <iostream>
using namespace std;
int factorial=1;
void fact(int *number)
{
for(int i=1;i<=*number;i++)
{
factorial=factorial*i;
}
}
int main() {
int n;
cout << "Enter the number whose factorial is required" << endl;
cin>>n;
fact(&n);
cout<<"\nFactorial is "<<factorial;
return 0;
}

Exercise 5
Write a program in C++ which makes use of double type array to read subject marks of
student and display entered marks, their total and average on screen.
-

Create an array of type double with 5 elements;


initialize all elements with value 0.

Make use of for loop to read values from user and fill
array elements - Display all elements data on screen
using loop

#include <iostream>
using namespace std;
int main() {
double marks[5]{0,0,0,0,0};
//For Taking Marks
for(int i=0;i<5;i++)
{
cout<<"Enter the marks of Subject "<<i+1<<" :"<<endl;
cin>>marks[i];

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science
}
//For Showing Marks of Each Subject
for(int i=0;i<5;i++)
{
cout<<"Marks of Subject "<<i+1<<" "<<marks[i]<<endl;
}
//For Taking Total of Marks
double total=0;
for(int i=0;i<5;i++)
{
total=total+marks[i];
}
cout<<"\n Total Marks="<<total;
//Average of Marks
double avg;
avg=total/5;
cout<<"\n Average marks="<<avg;
return 0;

Exercise 6
Write a program which creates an array of type int (length 10). Initialize array with
random values and swap first two elements of array with last two elements. You should
at least:
-

Create integer type array having 10 elements, initialized using initializers


list, and then print array using for loop

Swap first two elements with last two elements, after swapping, print
array using for loop - Display size of array using sizeof () function

#include <iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
int main() {
int arr[10],temp,temp2;
srand(time(0));
for(int i=0;i<10;i++)
{
arr[i]=rand()%20;

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science
cout<<arr[i]<<"\t";
}
//swapping
temp=arr[0];
arr[0]=arr[9];
arr[9]=temp;
temp2=arr[1];
arr[8]=arr[1];
arr[1]=temp2;
//display after swapping
cout<<endl<<"Values After Swapping:\n";
for(int i=0;i<10;i++)
{
cout<<arr[i]<<"\t";
}

return 0;

Exercise 7
Create an integer array with 10 elements and read values from user at run time. Ask
user to enter a value to search in array. Find all the occurrences of search value and
return index numbers of occurrences (if found).

#include<iostream>
using namespace std;
int main()
{
int arr[10],n,o=0;
for(int i=0;i<10;i++)
{
cout<<Enter the Number"<<i+1;
cin>>arr[i];
}
cout<<"Enter the number to search and find it Occuren:";
cin>>n;
for(int i=0;i<10;i++)
{
if(n==arr[i])
{
occ++;
cout<<"index is:"<<i<<endl;

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science
}
}
if(o!=0)
{
cout<<"number found :)"<<endl;
}
else
{
cout<<"number not found :("<<endl;
}
cout<<" The number "<<n <<" occurences:"<<o<<" times";
return 0;
}

Exercise 8
Write a program in C++ which does following:
- Make use of strlen function to display size of a string and use strcat function to
concatenate two strings
- Use strcpy function to copy one string to another
#include
#include
#include
#include

<iostream>
<time.h>
<cstdlib>
<string.h>

using namespace std;


int main() {
char a[10],n[33];
cout<<"Enter character 1";
cin>>a;
cout<<"Enter the Character 2";
cin>>n;
int l;
l=strlen(a);
cout<<l<<endl;
cout<<strcat(a,n)<<endl;
strcpy(a,n);
cout<<a<<endl;
cout<<n<<endl;
return 0;

Lab Instructor: Adil Shahzad

Air University, Islamabad


Department of Computer Science

You might also like