Professional Documents
Culture Documents
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;
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;
}
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
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.
-
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];
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:
-
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;
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;
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>