STATISTICS BASIC Given the following input file: 4 9 7 6 7 6 9 4 5 3 4 8 9 4 9 5
ID: 3575401 • Letter: S
Question
STATISTICS BASIC
Given the following input file:
4 9 7 6 7 6 9 4 5 3 4 8 9 4 9 5 8 4 3 2 3 6 8 8 2 6 1 4 2 3 2 3 1 7 6 7 1 4 8 6 6 3 2 5 4 1 7 2 2 1 1 4 6 8 1 6 4 1 7 3 4 7 5 4 4 2 8 4 3 7 9 8 7 1 1 8 8 8 7 1 8 7 2 2 6 2 7 7 3 3 1 4 9 3 5 1 2 1 4
Determine the mean, median and mode.
Implementation:
Use at least four (4) programmer defined functions; this excludes sort. At least three (3) must be void. Display unsorted array, sorted array, mean, median and mode (response frequency/histogram). All output should be displayed in the main program with the exception of mode (response/frequency histogram).
User should be prompted for input file name. Only 40 integers of sorted and unsorted arrays should be printed on one line. Please use a constant for size of array. In this file you can use a size of 99. ( const int SIZE=99)
SAMPLE OUTPUT:
STATISTICS:
*************
Mean
*************
The mean is the average value of all the data items divided by the number of data items.
Mean = 4.69697.
Unsorted array
4 9 7 6 7 6 9 4 5 3 4 8 9 4 9 5 8 4 3 2 3 6 8 8 2 6 1 4 2 3 2 3 1 7 6 7 1 4 8 6
6 3 2 5 4 1 7 2 2 1 1 4 6 8 1 6 4 1 7 3 4 7 5 4 4 2 8 4 3 7 9 8 7 1 1 8 8 8 7 1
8 7 2 2 6 2 7 7 3 3 1 4 9 3 5 1 2 1 4
Sorted array
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7
7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9
The median is element 49.
For this run, the median is 4.
*******
Mode
********
ResponseFrequencyHistogram
1 14 **************
2 12 ************
3 11 ***********
4 16 ****************
5 5 *****
6 10 **********
7 13 *************
8 12 ************
9 6 ******
The mode is the most frequent value.
For this run, the mode is 4 which occurred 16 time(s).
Please be sure to include the histogram. The file is named numbers. The language is C++
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//const size declaration
const int SIZE=99;
//function to read intgers from file
int readFromFile(int nos[SIZE],string fname)
{
ifstream myfile;
int size=0;
myfile.open(fname);
if(myfile.is_open())
{
while(myfile>>nos[size])
{
size++;
}
myfile.close();
}
else
cout<<"Unable top open file";
return size;
}
//funciton to cla mean
float calMean(int nos[],int size)
{
int total=0;
for(int i=0;i<size;i++)
{
total+=nos[i];
}
return (float)total/size;
}
//funciton to display array
void display(int nos[],int size)
{
for(int i=0;i<size;i++)
{
if(i%40==0)
cout<<endl;
cout<< nos[i]<<" ";
}
}
//funciton to sort array
void sort(int nos[],int size)
{
int tmp;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(nos[i]<nos[j])
{
tmp=nos[i];
nos[i]=nos[j];
nos[j]=tmp;
}
}
}
}
//function to cal median
int calMedian(int nos[],int size)
{
return nos[size/2];
}
//fucniton to cla mode
void calMode(int nos[],int size)
{
int uniquenos[size],uniquecount[size],tmpsize=0;
cout<<" ***********Response Frequency Histogram************** ";
for(int i=0;i<size;i++)
{
bool found=false;
for(int j=0;j<tmpsize;j++)
{
found=false;
if(uniquenos[j]==nos[i])
{
found=true;
uniquecount[j]++;
}
}
if(found==false)
{
uniquenos[tmpsize]=nos[i];
uniquecount[tmpsize]=1;
tmpsize++;
}
}
int maxindex=-1,max=0;
for(int i=0;i<tmpsize;i++)
{
cout<<endl;
cout<<uniquenos[i]<<" "<<uniquecount[i]<<" ";
for(int j=0;j<=uniquecount[i];j++)
{
cout<<"*";
}
if(max<uniquecount[i])
{
max=uniquecount[i];
maxindex=i;
}
}
cout<<" Mode is "<<uniquenos[maxindex]<<" which ocurred "<<max<<" times";
}
int main()
{
int nos[SIZE];
string fname;
cout<<"Enter file name: ";
cin>>fname;
int size=readFromFile(nos,fname);
cout << "Mean : " <<calMean(nos,size)<< endl;
cout<<" ***********Unsorted Array********************** ";
display(nos,size);
cout<<" ***********Sorted Array********************** ";
sort(nos,size);
display(nos,size);
cout<<" Median : "<<calMedian(nos,size) <<endl;
calMode(nos,size);
return 0;
}
Sample output:
Enter file name: a
Mean : 4.65657
***********Unsorted Array**********************
4 9 7 6 7 6 9 4 5 3 4 8 9 4 9 5 8 4 3 2 3 6 8 8 2 6 1 4 2 3 2 3 1 7 6 7 1 4 8 6
6 3 2 5 4 1 7 2 2 1 1 4 6 8 1 6 4 1 7 3 4 7 5 4 4 2 8 4 3 7 9 8 7 1 1 8 8 8 7 1
8 7 2 2 6 2 7 7 3 3 1 4 9 3 5 1 2 1 4
***********Sorted Array**********************
1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4
4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7
7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9
Median : 4
***********Response Frequency Histogram**************
1 14 ***************
2 12 *************
3 11 ************
4 16 *****************
5 5 ******
6 10 ***********
7 13 **************
8 12 *************
Mode is 6 which ocurred 16 times
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.