really need help with this program i have been able to do all the other programs
ID: 3555222 • Letter: R
Question
really need help with this program i have been able to do all the other programs up til now on my own
For this project you will write a program to compute the arithmetic mean (average), median, and mode for the values read in from TopicFin.txt. The program results will be written to TopicFout.txt.
The program will have a vector and an array. The vector will contain the values read in from the file. The array will contain those values in sorted order. You can assume that there will be no more than 1300 values. However, your program must handle files values less than 1300. You can assume that a file will have values and that these will be valid values.
At a minimum your program must have the following functions:
Explanation / Answer
#include<iostream>
#include<fstream>
#include<vector>
using namespace std ;
int readInput(vector<int>&);
void sort(int* , int) ;
void copy(vector<int> , int*) ;
void calculateMode();
void writeToFile(vector<int>,int) ;
void writeToFile(int [],int);
void calculatemode(int[],int) ;
int main()
{
vector<int> data ;
int arr[1500];
int sum = readInput(data) ;
copy(data, arr) ;
sort(arr,data.size()) ;
writeToFile(data,data.size()) ;
writeToFile(arr,data.size()) ;
calculatemode(arr,data.size()) ;
return 0 ;
}
int readInput(vector<int> &data)
{
ifstream inp("TopicFin.txt") ;
int res = 0 ;
if( inp.is_open() )
{
int x ;
while(inp >> x )
data.push_back(x) , res += x ;
}
return res ;
}
void copy(vector<int> data , int *arr)
{
for(int i = 0 ; i < data.size() ; i++ )
arr[i] = data[i] ;
}
void sort(int *arr , int N)
{
for(int i = 0 ; i < N ; i++ )
for(int j = 0 ; j+1 < N ; j++ )
if( arr[j] > arr[j+1] )
{
int tmp = arr[j] ;
arr[j] = arr[j+1] ;
arr[j+1] = tmp ;
}
}
void calculatemode(int arr[1500],int N)
{
int val = 1 ;
int mode = arr[0] ;
int occ_mode = 1 ;
ofstream out;
out.open( "TopicFout.txt" , ios::out | ios::app ) ;
for(int i = 1 ; i < N ; i++ )
{
if( arr[i] != arr[i-1] )
val = 1 ;
else
{
val++ ;
if( val > occ_mode )
occ_mode = val , mode = arr[i] ;
}
}
out << "The median of the values is: " << arr[N/2] << " " ;
out << "The mode of the values is " << mode << " which occurs " << occ_mode << " times. " ;
out.close() ;
}
void writeToFile( vector<int> v , int N )
{
ofstream out("TopicFout.txt");
if(out.is_open())
{
out << "The values read are: " ;
int sum = 0 ;
for(int i = 0 ; i < N ; i++ )
out << v[i] << " " , sum += v[i];
out << " " ;
out << "Average of values is " << sum/v.size() << endl ;
out.close();
}
}
void writeToFile( int v[],int N)
{
ofstream out;
out.open( "TopicFout.txt" , ios::out | ios::app ) ;
out << "The sorted result is: " ;
for(int i = 0 ; i < N ; i++ )
out << v[i] << " " ;
out << " " ;
out.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.