Write and use the following functions in your program: loadArray provide the arr
ID: 3834867 • Letter: W
Question
Write and use the following functions in your program: loadArray provide the array; fill the array with elements from a fie and return the number of strings extracted from the file; return the list length (the portion of the amay used to store data from the file) sortArray provide the array of integers its list length; sort the amay elements in ascending and order indexof provide the array, the list length, and the search string returmthe inden afthat string if it is in the array-return-1 otherwise get Unique List provide the array of integers and the list length create a list of distinct words (no duplicates; make the new lst available to the calling interface-the original should remain unchanged count Instances provide the array of integers from the fae, tsist length, the anayar 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 add this numberto the court ist make the new list available to the calling interface getHighest Index provide a list of numbers and its length, return the inden of the highest number in the list getMean provide the array of integers and its 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 anthmetic when applicable) getMode provide the array of number counts, its list length and an empty lat sne index of the highest number in the list and add it to the new list (if multiple items inthe li have the highest value, then they should all appear in the new list), return the length ofthe new list open File prompt the user to enter the path to a file; repeat this prompt until the fie is opened successfully or the user enters EXIT as a string if ExiT isentered return taase. otherwise return true (when the file is successfully opened). Use functions to write a program that: these prompts the user to enter the path to a file continuously until the file is apened or the user aborts the program reads integers from a file into an array o a list of unique integers in the file and the number times each ane appears of outputs o the total number of integers in the file appears o the mean, median, and mode of this list land how many times the modeExplanation / Answer
code:
#include<cstdio>
#include<string.h>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include <fstream>
#include<map>
using namespace std;
void read(int arr[])
{
string line;
ifstream myfile("input.txt");
int i=0;
if (myfile.is_open())
{
while (getline (myfile,line) )
{
arr[i]=atoi(line.c_str());
i++;
}
myfile.close();
}
}
void output(int arr[])
{
ofstream myfile ("output.txt");
if (myfile.is_open())
{
int i=0;
int k=0;
while(i<10)
{
myfile<<arr[k]<<" ";
k++;
i++;
}
myfile.close();
}
}
int main ( int argc, char *argv[] )
{
string line;
map<int,int> hash;
ifstream myfile("input.txt");
int i=0;
if (myfile.is_open())
{
while (getline (myfile,line) )
{
hash[atoi(line.c_str())]++;
i++;
}
myfile.close();
}
double mode=0;
double mean=0;
double median=0;
int max=-100000;
vector<int> arr;
for (std::map<int,int>::iterator it=hash.begin(); it!=hash.end(); ++it)
{
arr.push_back(it->first);
cout<<it->first<<" : "<<it->second<<endl;
if(it->second>max)
mode=it->first;
}
i=0;
double sum=0;
sort(arr.begin(),arr.end());
for(i=0;i<arr.size();i++)
{
sum+=arr[i];
}
mean = double(sum)/double(arr.size());
median = arr[arr.size()/2];
cout<<"mean: "<<mean<<endl;
cout<<"median: "<<median<<endl;
cout<<"mode: "<<mode<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.