O b j e c t O r i e n t e d P r o g r a m m i n g L a b
Lab 02 Marks 10
Instructions
➢ Work in this lab individually.
➢ You can use your books, notes, handouts etc. but you are not allowed to borrow anything from your peer student.
➢ Make sure to follow the best coding practices.
➢ Include comments to explain the logic where necessary.
➢ Test your program thoroughly with various inputs to ensure proper functionality and error handling.
➢ Show your work to the instructor before leaving the lab to get some or full credit.
Data Operations Program
You are required to create a C++ program that reads data from the "[Link]" (provided with this lab) file and performs various
operations on the data. The program should display a menu with different options, allowing the user to choose a specific operation
such as displaying the original data, getting the minimum number, maximum number, sum of the data, average of the data and
counting odd/even numbers. Your task is to implement the necessary functions to achieve this functionality.
Requirements:
1. Read Data from file:
• In the main function, read data from the "[Link]" file and store it in an array of integers. The file contains a series of 100
numeric values separated by newlines.
2. Create a function called printMenu that accepts an array of integers (data read from "[Link]") and array’s size as a parameter
and displays the following menu options:
----- Data Operations Menu -----
0. Display Original Data
1. Get Minimum Number
2. Get Maximum Number
3. Get Sum of Data
4. Get Average of Data
5. Count Odd/Even Numbers
6. Exit
------------------------------------------
Call this function in the main function, passing the array of integers with its size as an argument, to show the menu.
3. Service Functions: Implement the following service functions, each accepting an array of integers (read from the "[Link]")
and array’s size as an argument:
• printOriginalData:
Display the original data read from the file in a grid format, where each line contains 10 numbers separated by a tab.
• getMinimumNumber:
Find and display the minimum number from the read data.
• getMaximumNumber:
Find and display the maximum number from the read data.
• getSumOfData:
Calculate and display the sum of all data.
• getAverageOfData:
Calculate and display the average of all data.
• countOddEvenNumbers:
Count and display the number of odd and even numbers in the data.
4. Program Execution:
• Read data from the "[Link]" file in the main function and make a call to printMenu function.
• Implement a loop inside printMenu that continues until the user chooses to exit (selects option 6).
• Inside the loop, display the menu, get the user's choice, and call the corresponding service function accordingly.
Umair Babar, FCIT – PU. Lahore. Page 1 of 2
5. User Input and Error Handling:
• Implement error handling to ensure that the user enters valid value (0–6). If the input is not a valid number, display an
error message and prompt the user to enter the value again.
6. Exiting the Program:
• When the user chooses to exit, display a farewell message, and terminate the program.
Sample Run:
==> Data Reading from [Link] Completed <== ----- Data Operations Menu -----
0. Display Original Data
----- Data Operations Menu ----- 1. Get Minimum Number
0. Display Original Data 2. Get Maximum Number
1. Get Minimum Number 3. Get Sum of Data
2. Get Maximum Number 4. Get Average of Data
3. Get Sum of Data 5. Count Odd/Even Numbers
4. Get Average of Data 6. Exit
5. Count Odd/Even Numbers ------------------------------------------
6. Exit Enter your choice (0-6): 9
------------------------------------------
Invalid choice. Please enter a number between 0 and 6.
Enter your choice (0-6): 0
The original data is: ----- Data Operations Menu -----
0. Display Original Data
93 46 95 39 52 77 60 64 46 21
1. Get Minimum Number
27 10 90 58 19 18 42 77 26 34
2. Get Maximum Number
80 92 47 89 89 0 3 95 47 68
3. Get Sum of Data
73 18 77 18 67 89 93 19 1 42
4. Get Average of Data
7 65 21 87 27 20 2 94 49 36
5. Count Odd/Even Numbers
38 8 71 54 51 80 55 59 79 83
6. Exit
26 68 91 26 14 1 27 46 47 58
------------------------------------------
88 11 34 50 70 24 10 17 51 35
Enter your choice (0-6): 1
44 50 44 11 13 48 89 75 6 84
70 54 51 79 18 42 45 5 85 94 Minimum number: 0
----- Data Operations Menu ----- ----- Data Operations Menu -----
0. Display Original Data 0. Display Original Data
1. Get Minimum Number 1. Get Minimum Number
2. Get Maximum Number 2. Get Maximum Number
3. Get Sum of Data 3. Get Sum of Data
4. Get Average of Data 4. Get Average of Data
5. Count Odd/Even Numbers 5. Count Odd/Even Numbers
6. Exit 6. Exit
------------------------------------------ ------------------------------------------
Enter your choice (0-6): 0 Enter your choice (0-6): 3
Count of Odd Numbers: 53 The sum of data: 4980
Count of Even Numbers: 47
----- Data Operations Menu -----
----- Data Operations Menu ----- 0. Display Original Data
0. Display Original Data 1. Get Minimum Number
1. Get Minimum Number 2. Get Maximum Number
2. Get Maximum Number 3. Get Sum of Data
3. Get Sum of Data 4. Get Average of Data
4. Get Average of Data 5. Count Odd/Even Numbers
5. Count Odd/Even Numbers 6. Exit
6. Exit ------------------------------------------
------------------------------------------ Enter your choice (0-6): 6
Enter your choice (0-6): 4
Exiting the program. Goodbye!
The average of numbers: 49.8
Umair Babar, FCIT – PU. Lahore. Page 2 of 2