Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write testmain. CPP to verify that functions in arrayFunctions are valid. There is no need to open the folder in testmain. CPP, you should use

Write testmain. CPP to verify that functions in arrayFunctions are valid. There is no need to open the folder in testmain. CPP, you should use string as sample data. I've already written arrayFunctions. The CPP and main. CPP, but I don't know how to verify that arrayFunction works without opening the folder.Requirements are:

Write a C++ program that reads in an unknown number of integer arrays from a file. Your program will need to store each array temporaily in a dynamically allocated array for processing, and display, to the screen, the largest, smallest, average, and median values of each array. After each array is processed, your program should free the memory allocated for that array. In addition to the processing done on each array, your program needs to display the overall largest and smallest values of all arrays (this can be done with out a function within the main function).

VLA's (Variable Length Arrays) are restricted from use. If your program uses a VLA, your solution will loose 25% of the points.

Memory leaks will be monitored closely. If your solution leaks memory, your solution will loose 25% of the points.

Your program will need to open a file named "arrays.txt" to obtain the arrays.

format of arrays.txt:

numberOfValues value1value2...valueN numberOfValues value1value2...valueN arrays.txt (sample provided below)
5 1 2 3 4 5 4 10 2 32 14 3 23 41 2

Here is my code:

proj2-arrayFunctions.h

#ifndef PROJ2_ARRAYFUNCTIONS_H #define PROJ2_ARRAYFUNCTIONS_H

void bubbleSort(int *array, int size); int largestValue(int *array, int size); // used for largest in single array int smallestValue(int *array, int size); // used for smallest in single array double averageValue(int *array, int size); int medianValue(int *array, int size); //choose the smaller value if an evenly sized array

#endif

proj2-arrayFunctions.cpp

using namespace std;

/** * bubbleSort the data * * the function sort data using bubble sort * * Parameter * * array is the pointer array that store data * size is the size of the array * * output * return none * reference parameter: array * stream none */ void bubbleSort(int *array, int size){

// use first for loop to set the i for(int i = 0; i < size - 1; i++) { // use this loop to set the j for(int j = 0; j < size - 1 - i; j++) { // to check if we find the smaller number if(array[j] > array[j+1]) { // found a smaller value, switch them swap (array[j], array[j+1]); } } } } /** * find the largestValue in array * * the function find the largestValue in array * * Parameter * * array is the pointer array that store data * size is the size of the array * * output * return int * reference parameter: array * stream none */ int largestValue(int *array, int size){ int large = array[0]; for ( int i = 1; i < size; i++ ){ if ( large < array[i]){ large = array[i]; } } return large; } /** * find the smallestValue in array * * the function find the smallestValue in array * * Parameter * * array is the pointer array that store data * size is the size of the array * * output * return int * reference parameter: array * stream none */ int smallestValue(int *array, int size){ int small = array[0]; for ( int i = 1; i < size; i++ ){ if ( small > array[i]){ small = array[i]; } } return small; } /** * find the averageValue in array * * the function find the averageValue in array * * Parameter * * array is the pointer array that store data * size is the size of the array * * output * return double * reference parameter: array * stream none */ double averageValue(int *array, int size){ double avg = 0; for ( int i = 0; i < size; i++ ){ avg += array[i]; } return avg/size; } /** * find the medianValue in array * * the function find the medianValue in array * * Parameter * * array is the pointer array that store data * size is the size of the array * * output * return int * reference parameter: array * stream none */ int medianValue(int *array, int size){ int medianNum; bubbleSort(array,size); if ( size % 2 == 1){ medianNum = array[ size / 2 ]; } else { medianNum = array[( size / 2 ) - 1 ]; } return medianNum; }

main.cpp:

#include #include #include "proj2-arrayFunctions.h"

using namespace std;

int main () { fstream inputFile; int arraySize; int *numArray; int count = 0; // used to initial the overall Max and Min int largestNum, smallestNum; // open the file inputFile.open("arrays.txt"); // check if the file opened if (inputFile){ // read first arraySize inputFile >> arraySize; // while loop until reach the end of the file while (!inputFile.eof()){ // use new to create space for the array numArray = new int[arraySize]; //read date into the array for(int i=0;i>numArray[i]; } // call the function, sort the data bubbleSort(numArray, arraySize); // call the function, find and return the largest value cout << "largest: " << largestValue(numArray, arraySize) << endl; // call the function, find and return the smallest value cout << "smallest: " << smallestValue (numArray, arraySize) << endl; // call the function, find and return the average value cout << "avg: " << averageValue ( numArray, arraySize) << endl; // call the function, find and return the median value cout << "mid: " << medianValue ( numArray, arraySize) << endl; // find the overall max value if (largestValue(numArray, arraySize) > largestNum || count == 0){ largestNum = largestValue(numArray, arraySize); } // find the overall min value if (smallestValue (numArray, arraySize) < smallestNum || count == 0){ smallestNum = smallestValue (numArray, arraySize); } // read next arraysize inputFile >> arraySize; // after initialize the largest and smallest number, increment count count ++; // free the memory delete[] numArray; } // display two numbers for max and min cout << "Overall largest: " << largestNum << endl; cout << "Overall smallest: " << smallestNum << endl; } else { // if file not open, print error message cout << "Error: cannot find the file" << endl; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spatial Databases With Application To GIS

Authors: Philippe Rigaux, Michel Scholl, Agnès Voisard

1st Edition

1558605886, 978-1558605886

More Books

Students also viewed these Databases questions