Lab Assighnment 05
Lab Assighnment 05
SOLUTION
#include<iostream>
using namespace std;
void merge(int a[],int a1[],int m,int n){
int c[200],p1=0;
for(int i=0;i<m;i++){
c[i]=a[i];
}
for(int i=0;i<n;i++){
c[i+m]=a1[i];
}
for(int i=0;i<m+n-1-p1;i++){
for(int j=i+1;j<m+n-p1;j++){
if(c[j]==c[i]){
for(int k=j;k<m+n-1-p1;k++){
c[k]=c[k+1];
}
p1++;
}
if(c[j]<c[i]){
swap(c[j],c[i]);
}
}
}
for(int i=0;i<m+n-p1;i++){
cout<<c[i]<<" ";
}
}
int main(){
int a[100],a1[100],m,n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter size of first array : ";
cin>>m;
cout<<"Enter elements in ascending order : ";
for(int i=0;i<m;i++){
cin>>a[i];
}
cout<<"Enter size of second array : ";
cin>>n;
cout<<"Enter elements in descending order : ";
for(int i=0;i<n;i++){
cin>>a1[i];
}
merge(a,a1,m,n);
return 0;
}
2. Write a user defined function named upper_half() which takes a two dimensional
array A, with size N rows and N columns as argument and prints the upper half of
the array.
For e.g.,
2315023150
715311531
2 5 7 8 1 Output will be: 1 7 8
0150101
349155
SOLUTION
#include<iostream>
using namespace std;
void upper_half(int arr[][50],int row1,int
column1){
for(int i=0;i<row1;i++){
for (int j=0;j<column1;j++){
if(i>j){
cout<<" ";
}
else{
cout<<arr[i][j];
}
}
cout<<endl;
}
}
int main(){
int arr[50][50],row1,col1;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter number of rows : ";
cin>>row1;
cout<<"Enter number of column : ";
cin>>col1;
cout<<"Enter elements of 2D array : ";
for(int i=0;i<row1;i++){
for (int j=0;j<col1;j++){
cin>>arr[i][j];
}
}
cout<<endl;
upper_half(arr,row1,col1);
return 0;
}
3. Write a function in C++ which accepts a 2D array of integers and its size as
arguments and displays the elements of middle row and the elements of middle
column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e. 3x3, 5x5, 7x7
etc...]
Example, if the array contents are
354
769
218
Output through the function should be:
Middle Row: 7 6 9
Middle Column: 5 6 1
SOLUTION
#include<iostream>
using namespace std;
void display1(int arr[][50],int n){
int mid = n/2;
cout<<"Middle row : ";
for(int j=0;j<n;j++){
cout<<arr[mid][j]<<" ";
}
cout<<endl;
cout<<"Middle column : ";
for(int i=0;i<n;i++){
cout<<arr[i][mid]<<" ";
}
}
int main(){
int arr[50][50],n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter dimension of square matrix (odd) : ";
cin>>n;
cout<<"Enter elements of 2D array : ";
for(int i=0;i<n;i++){
for (int j=0;j<n;j++){
cin>>arr[i][j];
}
}
display1(arr,n);
return 0;
}
4. Write a C++ to find maximum array element using recursion.
SOLUTION
#include<iostream>
using namespace std;
int maximum1(int arr[],int n){
static int i=0,max=0;
if(i<n){
if(arr[i] > max){
max=arr[i];
i++;
maximum1(arr,n);
}
else{
i++;
maximum1(arr,n);
}
}
else{
return max;
}
}
int main (){
int arr[100],n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"enter size of array : ";
cin>>n;
cout<<"Enter elements : ";
for(int i=0;i<n;i++){
cin>>arr[i];
}
int max1 = maximum1(arr,n);
cout<<"Maximum array element : "<<max1;
return 0;
}
5. Write a C++ program for binary search using recursion
SOLUTION
#include<iostream>
using namespace std;
void binary(int a[],int key,int n,int start,int end){
int m;
if(start<=end){
m=(start+end)/2;
if(a[m]==key){
cout<<"Element is found at "<<m;
return ;
}
else{
if(a[m]>key){
end=m-1;
}
else {
start= m +1;
}
}
binary(a,key,n,start,end);
}
else{
cout<<"Element not found";
}
}
int main(){
int a[100],key,n,start=0;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter array size : ";
cin>>n;
int end=n-1;
cout<<"Enter elements in ascending order : ";
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Enter element to be found : ";
cin>>key;
binary(a,key,n,start,end);
return 0;
}
6. Write a user-defined function that removes vowels from the sentence, makes the
first character of every output sentence as capital and makes all other characters in
the output sentences in small cases.
Example: Input string: “hello world. i Am writing a c pRogrAm.”
Output string: “Hll wrld. M wrtng c prgrm.”
SOLUTION
#include<iostream>
#include<string.h>
using namespace std;
void newstring(char str[],int l1){
int c=0;
for(int i=0;str[i]!='\0';i++){
if(isupper(str[i])){
str[i]=str[i]+32;
}
if(str[i]=='.'){
if(islower(str[i+1])){
str[i+1] = str[i+1] -32;
}
}
}
for(int i=0;i<l1-c;i++){
if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' ||
str[i]=='u'){
for(int j=i;str[j]!='\0';j++){
str[j]=str[j+1];
}
}
}
if(islower(str[0])){
str[0]=str[0]-32;
}
}
int main(){
char str[100];
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter string : ";
cin.getline(str,100);
int l1=strlen(str);
newstring(str,l1);
cout<<str;
return 0;
}
7. Write a program which calls a recursive function to determine the given string is
palindrome or not and also write a recursive function to find the length the given
string.
SOLUTION
#include<iostream>
using namespace std;
void palindrome(char str[],int l1){
static int start =0,end=l1-1;
if(start<=end){
if(str[start]==str[end]){
start++;
end--;
palindrome(str,l1);
}
else{
cout<<"String is not a palindrome";
return ;
}
}
else{
cout<<"String is palindrome";
}
}
int length(char str[]){
static int l1=0,i=0;
if(str[i]!='\0'){
l1++;
i++;
length(str);
}
else{
return l1;
}
}
int main(){
char str[100];
int l1;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter a string : ";
cin>>str;
l1=length(str);
palindrome(str,l1);
cout<<endl;
cout<<"Length of a given string : "<<l1;
return 0;
8. Write a program to store n elements in an array of float values and print the
elements using pointer
SOLUTION
#include<iostream>
using namespace std;
int main(){
float arr[100];
int n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout << "Enter n: ";
cin >> n;
cout << "Enter elements of array : ";
for(int i=0;i<n;i++){
cin >> *(arr+i);
}
for(int i=0;i<n;i++){
cout << *(arr+i) << " ";
}
return 0;
9. Write a function countEven(int*, int) which receives an integer array and its size,
and returns the number of even numbers in the array.
SOLUTION
#include<iostream>
using namespace std;
int countEven(int* arr,int n){
int c= 0;
for(int i=0;i<n;i++){
if(arr[i]%2 == 0){
c++;
}
}
return c;
}
int main(){
int arr[100],n;
cout<<"Name : TarunSingh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter size of array : ";
cin>>n;
cout<<"Enter elements of array : ";
for (int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"Number of even numbers in an array :
"<<countEven(arr,n);
return 0;
}
10. Write a function that returns a pointer to the maximum value of an array of
doubles. If the array is empty, return NULL.
SOLUTION
#include<iostream>
using namespace std;
double max(double* arr,int n){
int max=0;
for(int i=0;i<n;i++){
if(arr[i]>max){
max = *(arr+i);
}
}
return max;
}
int main(){
double arr[100];
int n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
11. Write a user defined function to find the sum of two matrices using dynamically
allocating memory and later free the memory.
SOLUTION
#include<iostream>
using namespace std;
void sum(int** a1,int** a2,int r1,int c1,int r2,int c2){
int a3[100][100];
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
a3[i][j]=a1[i][j]+a2[i][j];
}
}
cout<<endl;
for(int i=0;i<r1;i++){
for(int j=0;j<c1;j++){
cout<<a3[i][j]<<" ";
}
cout<<endl;
}
}
int main(){
int r,c,r2,c2;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter row of first matrix : ";
cin>>r;
cout<<"Enter column of first matrix : ";
cin>>c;
int **a1 = new int* [r];
for(int i=0;i<r;i++){
a1[i]=new int[c];
}
cout<<"Enter elements of first matrix : ";
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>a1[i][j] ;
}
}
cout<<"Enter row of second matrix : ";
cin>>r2;
cout<<"Enter column of second matrix : ";
cin>>c2;
int **a2 = new int* [r2];
for(int i=0;i<r2;i++){
a2[i]=new int[c2];
}
cout<<"Enter elements of second matrix : ";
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>a2[i][j] ;
}
}
sum(a1,a2,r,c,r2,c2);
for(int i=0;i<r;i++){
delete []a1[i];
}
delete []a1;
for(int i=0;i<r;i++){
delete []a2[i];
}
delete []a2;
return 0;
}
12. Write a C++ program to read and print an Employee’s Details using Structure - In
this program, user should read employee’s details like name, employee id and
salary using structure and then print the entered values
SOLUTION
#include<iostream>
using namespace std;
struct employee{
char name[20];
int id;
float salary;
};
int main(){
employee em[100];
int n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter number of employee : ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter employee "<<i+1<<" details :
"<<endl;
cout<<"Name : ";
cin>>em[i].name;
cout<<"Employee id : ";
cin>>em[i].id;
cout<<"Employee salary : ";
cin>>em[i].salary;
cout<<endl;
}
cout<<endl;
for(int i=0;i<n;i++){
cout<<"Enter employee "<<i+1<<" details :
"<<endl;
cout<<"Name : "<<em[i].name<<endl;
cout<<"Employee id : "<<em[i].id<<endl;
cout<<"Employee salary : "<<em[i].salary<<endl;
cout<<endl;
}
return 0;
}
13. Write a structure to store the name, account number and balance of customers
(more than 10) and store their information.
a. Write a function to print the names of all the customers having balance less than
$200.
b. Write a function to add $100 in the balance of all the customers having more
than $1000 in their balance and then print the incremented value of their
balance
SOLUTION
#include<iostream>
using namespace std;
struct customer{
char name[20];
double account;
float balance;
};
void names(customer ct[],int n){
cout<<"Names of customer having blance less tha
200$ : "<<endl;
for(int i=0;i<n;i++){
if(ct[i].balance<200){
cout<<ct[i].name<<endl;
}
}
}
void increment(customer ct[],int n){
cout<<"Incremented balance of customer having
balance more than 1000 : ";
for(int i=0;i<n;i++){
if(ct[i].balance > 1000){
cout<<ct[i].name<<endl;
ct[i].balance+=100;
cout<<"Incremented balance :
"<<ct[i].balance<<endl;
}
}
}
int main(){
customer ct[50];
int n;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
cout<<"Enter number of customer : ";
cin>>n;
for(int i=0;i<n;i++){
cout<<"Enter details of customer "<<i+1<<"
:"<<endl;
cout<<"Enter name : ";
cin>>ct[i].name;
cout<<"Enter account number : ";
cin>>ct[i].account;
cout<<"Enter balance : ";
cin>>ct[i].balance;
cout<<endl;
}
names(ct,n);
cout<<endl;
increment(ct,n);
return 0;
}
14.Write a program to print the area of a rectangle by creating a class named ‘Area’
having two functions. First function named as ‘setDim’ takes the length and breadth
of the rectangle as parameters and the second function named as ‘getArea’ returns
the area of the rectangle. Length and breadth of the rectangle are entered through
keyboard
SOLUTION
#include<iostream>
using namespace std;
class area{
int length;
int breadth;
public:
void setDim(){
cout<<"Enter length : ";
cin>>length;
cout<<"Enter breadth : ";
cin>>breadth;
}
int getArea(){
return length*breadth;
}
};
int main(){
area r1;
cout<<"Name : Tarun Singh"<<endl;
cout<<"roll no. : 22CHB0A51"<<endl;
r1.setDim();
cout<<"Area : "<<r1.getArea();
return 0;
}