0% found this document useful (0 votes)
61 views13 pages

Assignment 4: 1.write A C++ Program To Get The Area of Square and Rectangle Using Friend Class and Constructors

The document describes 6 programming assignments involving the use of classes and objects in C++. 1. Write a program to calculate the area of a square and rectangle using friend classes and constructors. 2. Write a program to swap two numbers using a friend function. 3. Write a program to perform linear search on an array of objects. 4. Write a program to perform binary search on an array of objects. 5. Write a program to implement bubble sort on an array of objects. 6. Write a program to implement quick sort on an array of objects.

Uploaded by

Sujata Naskar
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)
61 views13 pages

Assignment 4: 1.write A C++ Program To Get The Area of Square and Rectangle Using Friend Class and Constructors

The document describes 6 programming assignments involving the use of classes and objects in C++. 1. Write a program to calculate the area of a square and rectangle using friend classes and constructors. 2. Write a program to swap two numbers using a friend function. 3. Write a program to perform linear search on an array of objects. 4. Write a program to perform binary search on an array of objects. 5. Write a program to implement bubble sort on an array of objects. 6. Write a program to implement quick sort on an array of objects.

Uploaded by

Sujata Naskar
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/ 13

ASSIGNMENT 4

1.Write a C++ program to get the area of square and rectangle using friend class
and constructors.
Algorithm:-
Step 1: Start.
Step 2: Create separate classes for square and rectangle.
Step 3: Make the square class a friend of the rectangle class.
Step 4: Calculate the area of the shapes using a function from rectangle class.
Step 5: Show the output.
Step 6: Stop.
Source Code:-
#include <iostream>
using namespace std;
class Rectangle
{
private:
float length, breadth;
public:
Rectangle ()
{
cout << "ENTER THE RECTANGLE'S LENGTH: ";
cin >> length;
cout << "ENTER THE RECTANGLE'S BREADTH: ";
cin >> breadth;
area ();
}
friend class Square;
public:
void area ()
{
cout << "THE AREA IS " << (length * breadth) << " SQ. UNITS" << endl;
}
};
class Square
{
private:
float side;
public:
Square (Rectangle R)
{
cout << "ENTER THE SQUARE'S SIDE: ";
cin >> R.length; R.breadth = R.length;
R.area ();
}
};
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
Rectangle R;
Square S (R); return 0;
}
Screenshot:-
2.Write a C++ program to implement swapping of two numbers using friend
function.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to swap the numbers.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <iostream>
using namespace std;
class Numbers
{
private:
int a, b;
public:
Numbers ()
{
cout << "ENTER INTEGER A: ";
cin >> a;
cout << "ENTER INTEGER B: ";
cin >> b;
cout << "BEFORE SWAPPING" << endl;
showNums ();
}
void showNums ()
{
cout << "A = " << a << endl;
cout << "B = " << b << endl;
}
friend void swap (Numbers N);
};
void swap (Numbers N)
{
int r;
r = N.a;
N.a = N.b;
N.b = r;
cout << "AFTER SWAPPING" << endl;
N.showNums ();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
Numbers N;
swap (N);
return 0;
}
Screenshot:-

3.Write a C++ program to implement Linear Search using object as parameter.


Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
};
void linSearch (NumberList N, int item)
{
int i; bool flag = false;
for (i = 0; i < N.length; i++)
{
if (item == N.list[i])
{
cout << endl << "ELEMENT " << (i+1) << " IS " << item << endl;
flag = true;
}
}
if (flag == false)
cout << item << " NOT FOUND IN THE ARRAY!" << endl;
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N; int item;
cout << "ENTER THE ITEM TO SEARCH: ";
cin >> item;
linSearch (N, item);
return 0;
}
Screenshot:-

4.Write a C++ program to


implement Binary Search
using object as
parameter.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers in ascending order.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS IN ASCENDING ORDER" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
};
int binSearch (NumberList N, int l, int r, int x)
{
int mid;
if (r >= l)
{
int mid = l + (r - l) / 2;
if (N.list[mid] == x)
return mid;
if (N.list[mid] > x)
return binSearch(N, l, mid - 1, x);
return binSearch (N, mid + 1, r, x);
}
return -1;
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N; int item, index;
cout << "ENTER THE ITEM TO SEARCH: ";
cin >> item;
index = binSearch (N, 0, N.length-1, item);
(index == -1)? cout << "ITEM NOT FOUND!" : cout << "ITEM " << (index+1) << " IS " << item;
return 0;
}

Screenshot:-

5.Write a C++ program to implement Bubble Sort using object as parameter.


Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS IN ASCENDING ORDER" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0; i < length; i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void bubbleSort (NumberList N)
{
int i, j, temp;
for (i = 0; i < N.length-1; i++)
for (j = 0; j < N.length-i-1; j++)
if (N.list[j] > N.list[j+1])
{
temp = N.list[j];
N.list[j] = N.list[j+1];
N.list[j+1] = temp;
}
cout << "THE SORTED LIST IS" << endl;
N.showList();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
bubbleSort (N);
return 0;
}
Screenshot:-

6.Write a C++ program to implement Quick Sort using object as parameter.


Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0; i < length; i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void swap (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (NumberList N, int low, int high)
{
int pivot = N.list[high];
int i = (low - 1); int temp;
for (int j = low; j <= high-1; j++)
{
if (N.list[j] <= pivot)
{
i++;
swap (&N.list[i], &N.list[j]);
}
}
swap (&N.list[i+1], &N.list[high]);
return (i+1);
}
void quickSort (NumberList N, int low, int high)
{
if (low < high)
{
int pi = partition (N, low, high);
quickSort (N, low, pi-1);
quickSort (N, pi+1, high);
}
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
quickSort (N, 0, N.length-1);
cout << endl << "THE LIST IS SORTED";
N.showList ();
return 0;
}
Screenshot:-

7.Write a C++ program


to implement Merge
Sort using object as parameter.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0; i < length; i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void merge (NumberList N, int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0;i < n1;i++)
L[i] = N.list[l + i];
for (j = 0;j < n2;j++)
R[j] = N.list[m + 1+ j];
i = 0; j = 0; k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
N.list[k] = L[i];
i++;
}
else
{
N.list[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
N.list[k] = L[i];
i++;
k++;
}
while (j < n2)
{
N.list[k] = R[j];
j++;
k++;
}
}
void mergeSort (NumberList N, int l, int r)
{
if (l < r)
{
int m = l + (r-l) / 2;
mergeSort (N, l, m);
mergeSort (N, m+1, r);
merge (N, l, m, r);
}
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
mergeSort (N, 0, N.length-1);
cout << endl << "THE LIST IS SORTED";
N.showList ();
return 0;
}
Screenshot:-
8.Write a C++ program to implement Insertion Sort using object as parameter.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0;i < length;i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0;i < length;i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void insertionSort (NumberList N)
{
int i, key, j;
for (i = 1; i < N.length; i++)
{
key = N.list[i];
j = i - 1;
while (j >= 0 && N.list[j] > key)
{
N.list[j+1] = N.list[j];
j = j - 1;
}
N.list[j+1] = key;
}
cout << endl << "THE LIST IS SORTED";
N.showList ();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
insertionSort (N);
return 0;
}
Screenshot:-

9.Write a C++
program to
implement Heap Sort
using object as
parameter.
Algorithm:-
Step 1: Start.
Step 2: Create a class to store the numbers.
Step 3: Create a function to search through the object of the numbers class.
Step 4: Show the output.
Step 5: Stop.
Source Code:-
#include <cstdlib>
#include <iostream>
using namespace std;
class NumberList
{
public:
int *list, length;
NumberList ()
{
cout << "ENTER THE LIST SIZE: ";
cin >> length;
int i;
list = (int *) malloc (length * sizeof (int));
cout << "ENTER THE LIST ITEMS" << endl;
for (i = 0; i < length; i++)
{
cout << "ITEM " << (i+1) << ": ";
cin >> list[i];
}
}
void showList ()
{
cout << endl << "THE LIST IS" << endl;
int i;
for (i = 0; i < length; i++)
cout << "ITEM " << (i+1) << ": " << list[i] << endl;
}
};
void heapify (NumberList N, int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && N.list[l] > N.list[largest])
largest = l;
if (r < n && N.list[r] > N.list[largest])
largest = r;
if (largest != i)
{
swap(N.list[i], N.list[largest]);
heapify(N, n, largest);
}
}
void heapSort (NumberList N)
{
int i, temp, n = N.length;
for (i = n/2 - 1; i >= 0; i--)
heapify (N, n, i);
for (i = n-1; i >= 0; i--)
{
temp = N.list[0];
N.list[0] = N.list[i];
N.list[i] = temp;
heapify (N, i, 0);
}
cout << endl << "THE LIST IS SORTED";
N.showList ();
}
int main ()
{
cout << "NAME : Abhinandan Goswami" << endl;
cout << "SECTION : CSE 3E" << endl;
cout << "ROLL : 93" << endl << endl;
NumberList N;
heapSort (N);
return 0;
}
Screenshot:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy