Write and use the following functions in your program: loadArray provide the arr
ID: 3837774 • Letter: W
Question
Write and use the following functions in your program: loadArray provide the array; fill the array with elements from a file and return the number of strings extracted from the file; return the list length (the portion of the array used to store data from the file) sortArray provide the array of integers and its list length; sort the array elements in ascending order indexof provide the array, the list length, and the search string; return the index of that string if it is in the array-return -1 otherwise get UniqueList provide the array of integers and the list length; create a list of distinct words (no duplicates); make the new list available to the calling interface-the original list should remain unchanged countInstances provide the array of integers from the file, its list length, the array of unique integers, its list length, and an empty list of integers, count the number of times each word in the unique list appears in the file list and this number to the count list; make the ad new list available to the calling interface getHighestIndex provide a list of numbers and its length; return the index the highest number in the list of getMean provide the array of inte and list length; calculate the arithmetic mean of the values in the array (as a decimal) and return it get Median provide a sorted copy of the array of numbers and its list length; return the median value of that collection (using floating-point arithmetic when applicable) getMode provide the array of number counts, its list length, and an empty list; find the index of the highest number in the list and add it to the new list (if multiple items in the list have the highest value, then they should all appear in the new list); return the length of the new list open File prompt the user to enter the path to a file; repeat this prompt until the file is opened successfully or the user enters "ExIT" as a string; if "ExIT" is entered, return f otherwise return true (when the file is successfully opened) Use these functions to write a program that prompts the user to enter the path to a file continuously until the file is opened or the use aborts the program reads integers from a file into an array e outputs a list of unique integers in the file and the number of times each one appears the total number of integers in the file the mean, median, and mode of this list (and how many times the mode appears)Explanation / Answer
As per analyzing your problem I have written the code in my own way to enable the functionality.
Please find the below-working code and the attached output:
*************************
#include <iostream>
#include <fstream>
using namespace std;
//Function Prototypes:
void printArray(int score[], int size);
bool myLess(int score,int value);
bool myGreater(int score, int value);
int compare(int score[], int size, bool (*comparison)(int,int));
bool prompt(int &choice);
//Main program:
int main()
{
bool run = true;
int score[100]={0},//Set first element to 0 and default the remaining to 0.
size=0, choice=0;
fstream infile("data.txt", ios::in);
while(!infile.eof()&&size<100)
{
infile>>score[size++];
infile.clear();
infile.ignore();
}
infile.close();
while(run)
{
while(prompt(choice)&&(choice<1||choice>7))//0 is also invalid
cout<<"Error, unavailable choice, try again ";
switch(choice)
{
case 1:
cout<<"Highest score: "<<compare(score,size,myGreater)<<' ';
break;
case 2:
cout<<"Lowest score: "<<compare(score,size,myLess)<<' ';
break;
case 3:
case 4:
case 5:
cout<<"Not implemented. ";
break;
case 6:
printArray(score,size);
break;
case 7:
run = false;
break;
}
}
return 0;
}
//Function Definitions:
bool prompt(int &choice)
{
cout << " ************************************** ";
cout << " What do you wish to do? ";
cout << "1. Retrieve highest score ";
cout << "2. Retrieve lowest score ";
cout << "3. Calculate average ";
cout << "4. Calculate median ";
cout << "5. Calculate mode ";
cout << "6. Print array ";
cout << "7. Exit ";
cout << "Choice: ";
cin>>choice;
cin.clear();
cin.ignore(1000,' ');
return true;
}
int compare(int score[], int size, bool (*comparison)(int,int))
{
int value=score[0];
for(int i=1;i<size;++i)
if(comparison(score[i],value))
value=score[i];
return value;
}
bool myGreater(int score, int value)
{
return (score>value);
}
bool myLess(int score,int value)
{
return (score<value);
}
void printArray(int score[], int size)
{
for(int i=0;i<size;++i)
cout<<score[i]<<' ';
cout<<' ';
}
***************************
Output:
******************
*********************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.