c++ programming The objective of this Program is to process and determine statis
ID: 3713031 • Letter: C
Question
c++ programming
Explanation / Answer
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
print_array(vector <float> a){
vector <float> :: iterator i;
for (i = a.begin(); i != a.end(); i++)
cout << *i << ' ';
cout<<" ";
}
sort_array(vector <float> *a){
sort(a->begin(), a->end());
}
float mean(vector <float> a){
float ans = 0;
vector <float> :: iterator i;
for (i = a.begin(); i != a.end(); i++){
ans += *i;
}
ans = ans/a.size();
return ans;
}
float median(vector <float> a){
float ans;
if (a.size()%2 !=0)
ans = a[(a.size()-1)/2];
else{
ans = (a[(a.size()-1)/2] + a[((a.size()-1)+1)/2])/2;
}
return ans;
}
float mode(vector <float> a){
int counter = 1;
int max_iter = 0;
float ans = a[0];
for (int i = 0; i < a.size() - 1; i++)
{
if ( a[i] == a[i+1] )
{
counter++;
if ( counter > max_iter )
{
max_iter = counter;
ans = a[i];
}
} else
counter = 1;
}
return ans;
}
void Combine_DATA(vector <float> a, vector <float> *b){
for(int i=0; i<a.size(); i++){
b->push_back(a[i]);
}
}
int main(){
int k;
string filename;
vector <float> combined; // Combined array
ifstream ifile;
/* Infinite Loop to Enter as many datasets as you want every time
when someone adds a dataset at the end of the choice 1 combine_DATA
function is called which adds the new sorted datasets to a combined array
*/
while(1){
cout<<"Enter Your Choice: ";
cout<<"1. Add Data Set 2. Combine Data Set 3. Exit ";
cin>>k;
if(k == 1){
vector <float> basic; // Basic array for dataset corresponding to a new file
cout<<"Enter Name of File:(Please add .txt extension at the end of file name) ";//Please add .txt extension at the end of your input from console
cin>>filename;
float i;
ifile.open(filename);
do{
ifile >> i;
cout << i << endl;
basic.push_back(i);
}while(i != -999.0);
ifile.close();
basic.pop_back();
cout<<"The Unsorted Array is: ";
print_array(basic);
sort_array(&basic);
cout<<"The Sorted Array is: ";
print_array(basic);
float mean_basic = mean(basic);
cout<<"Mean = "<<mean_basic<<" ";
float meadian_basic = median(basic);
cout<<"Median = "<<meadian_basic<<" ";
float mode_basic = mode(basic);
cout<<"Mode = "<<mode_basic<<" ";
Combine_DATA(basic,&combined);
}
else if(k == 2){
cout<<"The Unsorted Combined Array is: ";
print_array(combined);
sort_array(&combined);
cout<<"The Sorted Array is: ";
print_array(combined);
float mean_combined = mean(combined);
cout<<"Mean = "<<mean_combined<<" ";
float meadian_combined = median(combined);
cout<<"Median = "<<meadian_combined<<" ";
float mode_combined = mode(combined);
cout<<"Mode = "<<mode_combined<<" ";
}
else if(k == 3){
break;
}
else
cout<<"Invalid Command !!!!! ";
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.