In: Computer Science
This assignment covers practical aspects of this course. This assessment covers the following course learning outcomes: CLO 2 - Apply programming concepts to computing problems CLO 3 - Examine Code for its syntax and semantic validity Case Study The assignment is based on the monthly income of Anna, a coffee shop owner in Suva. Anna’s monthly income for 2019 is given below, and you are required to write a program that will perform some manipulation actions on her monthly income records.
jan | feb | march | april | may | june |
$3055.00 | $4512.00 | $3221.50 | $4899.20 | $4556.00 | $4787.00 |
jul | aug | sep | oct | nov | dec |
$5002.10 | $5321.00 | $6025.00 | $7652.00 | $8623.50 | $7695.00 |
You are required to work in groups of 3 to complete this assignment. The programming concepts you are expected to use include if…else structures, loops, functions and arrays. Your program should 1. Store Anna’s monthly income values into one array. The array declaration and assignment is given below, you can copy and paste this statement into your sourcefile directly. Notice that each element of the array is Anna’s income for each respective month.
double income_arr[] = {3055.00, 4512.00, 3221.50, 4899.20, 4556.00, 4787.00, 5002.10, 5321.00, 6025.00, 7652.00, 8623.50, 7695.00};
3. Implement a function for each of the following tasks:
a. A function that will display the menu and get the user’s selection from the menu.
b. A function that finds the highest value given an array of numbers.
c. A function that finds the lowest value given an array of numbers.
d. A function that sums all the values in a given array of numbers.
e. A function that prints the elements of a given array.
f. A function that sorts the values of an array in ascending order.
g. A function that sorts the values of an array in descending order.
h. Optional: A function that copies the values of one array into another array of the same size. Hint: This can be used when you want to sort the array in ascending and descending order, since you should not change the order of the elements in the original array.
4. Use a while loop in your main function to allow the user to try different menu options until they decide to quit/exit.
5. Other tips: a. Don’t forget to add comments where necessary
b. Format your output well so that money values have the currency symbol ($)
c. Libraries you can consider:
i. iostream for input/output
ii. iomanip for formatting output where currency is displayed
iii. algorithm for sort function
// C++ program to perform operations on array
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std;
// function declaration
int menu();
void highest(double arr[], int size);
void lowest(double arr[], int size);
void sum(double arr[], int size);
void display(double arr[], int size);
void sortAscending(double arr[], int size);
void sortDescending(double arr[], int size);
void copy(double arr[], int size, double copyArr[]);
int main()
{
// define the array
double income_arr[] = {3055.00, 4512.00, 3221.50, 4899.20, 4556.00,
4787.00, 5002.10, 5321.00, 6025.00, 7652.00, 8623.50,
7695.00};
int size = 12, choice;
double temp_arr[size]; // define temporary array used for
sorting
// loop that continues until the user exits
do
{
choice = menu(); // display the menu and get user choice
// based on user choice, call the function to perform the
operation
switch(choice)
{
case 1:
highest(income_arr, size);
break;
case 2:
lowest(income_arr, size);
break;
case 3:
sum(income_arr, size);
break;
case 4:
cout<<"Values:"<<endl;
display(income_arr, size);
break;
case 5:
copy(income_arr, size, temp_arr); // copy the array from income_arr
to temp_arr
sortAscending(temp_arr, size); // sort the temp_arr
break;
case 6:
copy(income_arr, size, temp_arr); // copy the array from income_arr
to temp_arr
sortDescending(temp_arr, size); // sort the temp_arr
break;
}
cout<<endl;
}while(choice != 7);
return 0;
}
// function to display menu choice, input user choice and return
the choice
int menu()
{
int choice;
cout<<"1. Finds the highest value given an array of
numbers"<<endl;
cout<<"2. Finds the lowest value given an array of
numbers."<<endl;
cout<<"3. Sums all the values in a given array of
numbers."<<endl;
cout<<"4. Prints the elements of a given
array."<<endl;
cout<<"5. Sorts the values of an array in ascending
order."<<endl;
cout<<"6. Sorts the values of an array in descending
order."<<endl;
cout<<"7. Exit the program"<<endl;
cout<<"Enter your choice(1-7): ";
cin>>choice; // input user choice
// validate user choice and re-prompt until valid
while(choice < 1 || choice > 7)
{
cout<<"Invalid choice. Re-enter: ";
cin>>choice;
}
return choice;
}
// function to determine and display the highest element of
input array
void highest(double arr[], int size)
{
double maxEl = arr[0]; // initialize the first element to be the
highest
// loop over the array to find the highest element
for(int i=1;i<size;i++)
{
if(maxEl < arr[i])
maxEl = arr[i];
}
// display the highest element
cout<<"Highest value:
$"<<fixed<<setprecision(2)<<maxEl<<endl;
}
// function to determine and display the lowest element of input
array
void lowest(double arr[], int size)
{
double minEl = arr[0]; // initialize the first element to be the
lowest
// loop over the array to determine the lowest element
for(int i=1;i<size;i++)
{
if(minEl > arr[i])
minEl = arr[i];
}
// display the lowest element
cout<<"Lowest value:
$"<<fixed<<setprecision(2)<<minEl<<endl;
}
// function to determine and total of all elements of input
array
void sum(double arr[], int size)
{
double total = 0; // initialize total to 0
// loop over the array to add each element of array to total
for(int i=0;i<size;i++)
total += arr[i];
// display the total of all values
cout<<"Total of all values:
$"<<fixed<<setprecision(2)<<total<<endl;
}
// function to display all the elements of input array
void display(double arr[], int size)
{
// loop over the array displaying the elements of the array
for(int i=0;i<size;i++)
{
cout<<fixed<<setprecision(2)<<"$"<<arr[i]<<"
";
}
cout<<endl;
}
// function to sort the array in ascending order
void sortAscending(double arr[], int size)
{
sort(arr, arr+size); // sort the values in ascending order
// display the sorted array
cout<<"Ascending sorted values: "<<endl;
display(arr, size);
}
// function to sort the array in descending order
void sortDescending(double arr[], int size)
{
sort(arr, arr+size, greater<double>()); // sort the values in
descending order
// display the sorted array
cout<<"Descending sorted values: "<<endl;
display(arr, size);
}
// function to copy the values of arr into copyArr
void copy(double arr[], int size, double copyArr[])
{
// loop over the array copying the elements of arr into
copyArr
for(int i=0;i<size;i++)
copyArr[i] = arr[i];
}
//end of program
Output: