c++ programming You may design your program to expect any data set will be ho lr
ID: 3713750 • Letter: C
Question
c++ programming
You may design your program to expect any data set will be ho lrger tnin 100 Potentially your program could process several different data sets, though you will only use the 3 sets provided for your would be no greater than 300 elements. submission Design your program for generality: The code should not need to be changed if you were asked to run this program with 4 different data sets, or with 10 different data sets, or even 100 different data sets (except maybe change the dimensions of the arrays used). Your output should contain a print of both the unsorted data values and the sorted data values as well as the mean, For compactness, print the output in an arrangement of columns, rather than one element per line. Make sure everything Your program should have the following functions: median, and mode values of each of the input data sets, as well as for the combined data set. is well labeled and clear. A function to print an array You will call this function to print an unsorted array, and then a second time to print a sorted array. A function to sort an array A function to calculate the mear A function to calculate the median A function to calculate the mode You may use other functions as desired. You do not need to write your own functions, particularly the ones to sort the array, or to calculate the mode. You may use routines given in the course documents, from the textbook, or from another source. Document the source of any routine that you did not write, ineluding any comments on use and/or modifications. For submission, run the program with the data sets PlOstatdatal Ixt,PlOstatdata2 txt and P10statdata3 ixt in Blackboard. These data sets are set up with a sentinel of -999 at the end, and each contain a different number of numbers. Do not change or combine these files. The data filenames should be read into your program, and the program is only run once. You should strive to demonstrate your programming prowess. Be sure to include sufficient comments Updated 5/27/2017Explanation / Answer
CODE:
#include<bits/stdc++.h>
using namespace std;
const int MAX=10000; //max size of array
void printArray(float arr[],int n)
{
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
void sortArray(float arr[],int n)
{
//you can use builtin function in algorithm.h to sort arrays
sort(arr,arr+n);
}
float mean(float arr[],int n)
{
float sum=0; //find sum of array
for(int i=0;i<n;i++)
sum+=arr[i];
return sum/n;
}
float median(float arr[],int n)
{
sortArray(arr,n); //first sort the array
if(n%2==0) //n is even
{
return (arr[n/2]+arr[n/2-1])/2; //average of middle two elements
}
else //n is odd
return arr[n/2]; //return middle element
}
float mode(float arr[],int n)
{
//first sort array to find occurences of each element easily
sortArray(arr,n);
//for first element count is 1
int max_count=1;
int cnt=1;
float m=arr[0];
for(int i=1;i<n;i++)
{
if(arr[i]==arr[i-1])
{
cnt++;
if(cnt>max_count)
{
max_count=cnt;
m=arr[i];
}
}
else
cnt=1;
}
return m;
}
int loadDataFromFile(float arr[],string file)
{
ifstream in;
in.open(file.c_str());
int n=0;
if(in.fail())
{
cout<<"File failed to open ";
exit(1);
}
int x;
in>>x;
while (x!=-999) { // Continue if the -999 not read
arr[n++]=x;
in>>x;
}
in.close();
return n;
}
int main()
{
//load data in array
float arr[MAX];
string file;
for(int i=1;i<=3;i++)
{
//run three times loop for this assignment
cin>>file;
file="P10statdata";
file+=(char)(i+'0');
file+=".txt";
int n = loadDataFromFile(arr, file);
//perform your operations here
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.