Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ASSIGNMENT NO 1

Name :Esha Anwar

Reg no : 4642-FOC/BSCS/F22

Course Title : Object Oriented Paragridm

Course Code : CS 211

Submitted to: Mam Hira


Question: Write a program in C++ to display a menu to the user through
which user can store some integer values, then provide an option to the
user
i. Display the values
ii. Find out the maximum value
iii. Find out the minimum value
iv. Display in ascending order
v. Display in descending order.
vi. Exit
Implement using array data structure and principles of OOP

Solution:
 CLASS FILE
pragma once
//Creating class in a separate file class
Menu
{
int arr[5];
public:
void displayMenu();
void get um();
void display();
void findmax();
void findmin();
void displayAscen();
void displayDescen();
void Exit();
};
 IMPLEMENTATION FILE
include "Menu.h"
include<iostream>
using namespace std;
//Member function to read the numbers from user void
Menu::get um()
{
cout << "Enter five integers on which operations are going
to be performed:" << endl;
for (int i = 0; i < 5; i++)
{
cin >> arr[i];
}
cout << endl;
}
//Member function to Display Menu
void Menu::displayMenu()
{
cout << "\nThe following are the operations that you want to
perform:" << endl;
cout << "Press ‘1’to display the numbers you
entered." << endl;
cout << "Press ‘2’ to find and display the maximum
number." << endl;
cout << "Press ‘3’ to find and display the minimum number."
<< endl;
cout << "Press ‘4’ to display the numbers in an ascending
order." << endl;
cout << "Press ‘5’ to dsplay the numbers in a descending
order." << endl;
cout << "Press ‘6’ to exit the menu." << endl;
}
//Member function to calculate display the numbers
void Menu::display()
{
cout << " umbers that you entered:" << endl; for
(int i = 0; i < 5; i++)
{
cout << arr[i] << ",";
}
}
//Member function to find and display the maximum number
void Menu::findmax()
{
int* ptr;
ptr = &arr[0];
for (int i = 0; i < 5; i++)
{
if (*ptr < arr[i + 1])
{
ptr = &arr[i + 1];
}
}
cout << *ptr << " is the maximum number." << endl;
}
//Member function to find and display the minimum number
void Menu::findmin()
{
int* ptr;
ptr = &arr[0];
for (int i = 1; i < 5; i++)
{
if (*ptr > arr[i])
{
ptr = &arr[i];
}
}
cout << *ptr << " is the minimum number." << endl;
}
//Member function to display the numbers in ascending order
void Menu::displayAscen()
{
for (int i = 0; i < 5-1; i++)
{
for (int j = 0; j< 5-i-1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << " umbers arranged in ascending order:" << endl; for (int
k = 0; k < 5; k++)
{
cout << arr[k] << ",";
}
}
//Member function to display the numbers in descending number
void Menu::displayDescen()
{
for (int i = 0; i < 5-1; i++)
{
for (int j =0; j< 5-i-1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << " umbers arranged in descending order:" << endl;
for (int k = 0; k < 5; k++)
{
cout << arr[k] << ",";
}
}
//Member function to exit the menu
void Menu::Exit()
{
cout << "Exiting..........." << endl;
}

 MAIN FILE
include"Menu.h"
include<iostream>
using namespace std;
int main()
{
int choice;
Menu m; //Creating object
m.get um();
// Loop to display the menu and get the user’s choice do
{
m.displayMenu();
cout << "Enter your choice: "; cin
>> choice;
switch (choice)
{
case 1:
m.display();
break;
case 2:
m.findmax();
break;
case 3:
m.findmin();
break;
case 4:
m.displayAscen(); break;
case 5:
m.displayDescen(); break;
case 6:
m.Exit();
break;
default:
cout<< "Invalid choice!Please try again." << endl;
}
}
while (choice != 6); return
0;
}

 OUTPUT

You might also like