Overview You will be creating a special type of calculator. Your calculator will
ID: 3750778 • Letter: O
Question
Overview You will be creating a special type of calculator. Your calculator will run from the command line. It will accept a function code and an arbitrarily long list of numbers. It will print the function, the input, and the result. No input validation needs to be performed. This assignment is designed to test your ability to manipulate both dynamic and static arrays, as well as create an application which can handle different inputs. You may freely use code from my lecture slides and from the code folder in OWL.
Submission Requirements * Submission MUST be done through OWL. * See the syllabus for late penalties. * You will be submitting one and ONLY one file, name exactly “assn1.cpp”. * This file will ONLY include . * Submission files will NEVER be zipped. * Your program must terminate after printing the output without the user needing to input anything! * If your code does not run as-is in Microsft Visual Studio 2017, you will lose the majority of your marks. * You will not receive any marks for following these instructions, but marks will be deducted for not following them.
Project Setup I recommend creating a “Empty Project” as you did in lab 2, and create your assn1.cpp file as you did in the same lab. You may require the user to input a character to keep the window open while you develop it, but you MUST remove that code before you submit it.
Program Arguments The program will accept any of the following function codes as the first argument: ‘A’ - Calculate the average = sum of all values divided by the number of values ‘M’ - Calculate the median value = if all values are sorted, the median is the middle value. If there are an even number of values, the median is the average of the middle 2 values. (ie. The median of {1,3,5} is 3, and the median of {1,4,5,30} is 4.5) ‘F’ - Calculate the sum of all values = normal addition of all values Following the function code will be a list of numbers. ‘A’ and ‘M’ function will accept any number of values, while the ‘F’ function will only accept exactly 3 numbers. (All numbers will be real numbers, ie. They can have a decimal) This list of numbers is NOT required to be in sorted order. Example of running your code if your executable program is named “assn1.exe”: “location of your program/assn1.exe” F 1.23 5.67 -8.88 “location of your program/assn1.exe” M 1.23 5.67 -8.88 1.23 5.67 -8.88 1.23 5.67 -8.88 1.23 5.67 -8.88 “location of your program/assn1.exe” A 1.23 5.67 -8.88 1 5.67
C++ Notes (You are not required to implement any of these notes) * Consider using atof function to conver your arguments into doubles * Consider using a switch statement to branch based on the function code (argv[1][0]) * Consider using the sort function from the code folder or lab 2 or create one yourself, but make sure to change it to use pointer notation!
Function Specifications The 3 functions will have the following prototypes, and return the final answer: A – double calcAverage(char * values[],int sz); M – double calcMedian(char * values[],int sz); F – double calcSum(char * values[]); All functions take a pointer to an array of character pointers. Since our argv in main includes the program name in index 0, and the function code in index 1, we can pass &argv[2] to these functions from main. The sz argument would then be argc-2. The first thing each function does is convert the input strings into the array elements (atof). Only after that is done will you process the elements individually! I know this is not efficient, but it is just for learning purposes. You will also notice that function F does not take a size parameter. That is because function F requires exactly 3 numbers. ‘F’ function must use a statically declared array of doubles of size 3. ‘A’ function must use a dynamically allocated array based on the input sz. Within this function, you must refer to the elements using ONLY ARRAY notation. The memory for the dynamic array must be freed. ‘M’ function must use a dynamically allocated array based on the input sz. Within this function, you must refer to the elements using ONLY POINTER notation. The memory for the dynamic array must be freed. You may create additional “helper” functions (like sort), but they must follow the notation rules of where they are being called from. (ie. A function called from calcMedian must also only use pointer notation for element access).
Output Specifications Each function will output exactly 3 lines (no more, no less, and only contain the following information): Line 1 will be either “Average”, “Median”, or “Sum”. Line 2 will be the list of input numbers separated by a space. Line 3 will be the output value. The printing of lines 2 and 3 must be done from your main function, printing of line 1 must be done within the A,M,or F function
Explanation / Answer
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
/*
sz - 2 is taken because of the first two command line argument does not contains numbers.
first command line argument is the file name
second command line argument is the function character
*/
// Function to calculate and return average
double calcAverage(char *values[], int sz)
{
// To store sum
double sum = 0;
// Loops from 2 to size of the command line arguments
for(int c = 2; c < sz; c++)
{
// Displays the value at c index position
cout<<values[c]<<" ";
// Calculate sum after converting it to double
sum += atof(values[c]);
}// End of for loop
cout<<endl;
// Calculates and returns average
return (sum / (double)(sz - 2));
}// End of function
// Function to calculate and return sum
double calcSum(char * values[])
{
// To store sum
double sum = 0;
// Loops from 2 to size of the command line arguments
for(int c = 2; c < 5; c++)
{
// Displays the value at c index position
cout<<values[c]<<" ";
// Calculate sum after converting it to double
sum += atof(values[c]);
}// End of for loop
cout<<endl;
// Returns the sum
return sum;
}// End of function
// Function to sort data
void sortData(char * values[], int sz)
{
// Temporary pointer variable to swap
char *temp;
// Loops from 2 to size minus one of the command line arguments
for(int r = 2; r < sz - 1; r++)
{
// Loops from 2 to size minus one of the command line arguments
for(int c = 2; c < sz - 1; c++)
{
// Checks if value at c index position is greater than the value at next position
// after converting it into double
if(atof(values[c]) > atof(values[c + 1]))
{
// Swapping process
temp = values[c];
values[c] = values[c + 1];
values[c + 1] = temp;
}// End of if condition
}// End of for inner loop
}// End of outer for loop
}// End of function
// Function to calculate and return median
double calcMedian(char * values[], int sz)
{
// To store median value
double median;
// To store position
int pos;
// Calls the function to sort data
sortData(values, sz);
// Loops from 2 to size of the command line arguments
for(int c = 2; c < sz; c++)
// Displays the value at c index position
cout<<values[c]<<" ";
// Calculates the middle position
pos = (sz - 2) / 2;
// Checks for even number of elements
if ((sz - 2) % 2 == 0)
// Calculates the average of middle position and its next position as median
// After converting it into double
median = (atof(values[pos+2-1]) + atof(values[pos+2])) / 2.0;
// Otherwise odd
else
// Stores the middle value as the median
// After converting it into double
median = atof(values[pos+2]);
return median;
}// End of function
// main function definition with command line argument
int main(int argc, char** argv)
{
// Checks if number of command line argument is one then display error message
if(argc == 1)
cout<<" ERROR: No command line arguments.";
// Otherwise checks second command line argument is an alphabet
else if(isalpha(argv[1][0]))
{
// Checks the second command line argument
switch(argv[1][0])
{
// Calculates sum
case 'F':
// Checks if number of argument minus two equals to three
if((argc - 2) == 3)
{
cout<<" Sum ";
// Calls the function to calculates sum and displays it
cout<<calcSum(argv);
}// End of if condition
// Otherwise display error message
else
cout<<" ERROR: Must the 3 numbers only.";
break;
// Calculates median
case 'M':
cout<<" Median ";
// Calls the function to calculates median and displays it
cout<<calcMedian(argv, argc);
break;
// Calculates average
case 'A':
cout<<" Average ";
// Calls the function to calculates average and displays it
cout<<calcAverage(argv, argc);
break;
// Otherwise invalid code
default:
cout<<" ERROR: Invalid function code. Must be an alphabet (F or M or A)";
}// End of switch case
}// End of else if
// Otherwise invalid second command line argument
else
cout<<" ERROR: Second command line argument is number. Number not allowed.";
return 0;
}// End of main function
Sample Output 1(No command line argument):
ERROR: No command line arguments.
Sample Output 2(s):
ERROR: Invalid function code.
Sample Output 3(F 10.3 20.2):
ERROR: Must the 3 numbers only.
Sample Output 4(F 10.3 20.2 30.3 40.5 55.21):
ERROR: Must the 3 numbers only.
Sample Output 5(F 10.3 20.2 30.3):
Sum
10.3 20.2 30.3
60.8
Sample Output 6(A 12.5 22.8 11.5 6.2 7.8):
Average
12.5 22.8 11.5 6.2 7.8
12.16
Sample Output 7(M 12.5 22.8 11.5 6.2 7.8):
Median
6.2 7.8 11.5 12.5 22.8
11.5
Sample Output 8(M 12.5 22.8 11.5 6.2 7.8 9.2):
Median
6.2 7.8 9.2 11.5 12.5 22.8
10.35
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.