write a program that will read files of numbers into a vector, compute statistics relating to these numbers and distribute a vector of numbers into two vectors with approximately equal totals. The user should be allowed to repeat these tasks.
Main Input Loop
The program should allow the user to repeatedly perform one of the following tasks until he/she wishes to quit.
Read a file of numbers into int_vec
Print the contents of int_vec
Print the sum of int_vec
Print the mean of int_vec
Print the standard deviation of int_vec
Distribute the values stored in int_vec into two vectors where the sums of the values in two vectors are approximately equal, then print these sums
When the program starts the user should be prompted to read data from a file (as the user can't perform any of the other tasks until there is data to deal with). Thereafter the user should be prompted to perform one of the options noted above, or to quit.
Each of the tasks should be controlled by a separate function, and each of these functions, except for the read file function, should have a constant vector reference parameter. The read file function should return a vector of ints, which should be assigned to int_vec.
Write a function that prints the instructions, gets user input (a character) and then returns it. The instructions are shown in the screen-shot above starting with Please select one of the following options, and ending with q - Quit.
In addition to printing the instructions the function is also going to get user input. So the entire instructions function will look something like this, except it will, of course, be written in C++.
char instructions() declare char input variable print instructions using cout read user input using cin return inputStub Functions
A stub function is a placeholder function for work you haven't done yet. This allows a programmer to work on part of a project that needs to call a function even when that function hasn't been written yet (because someone else is writing it, or because it is very hard to write, etc.). The function is declared with the appropriate parameters and return value, but returns a default value. The default value will eventually be replaced with the correct calculation.
In the context of the assignment this will allow you to write the entire input loop and interface, and then plug in the functions as you complete them, and use the interface to test each function.
Here are a couple of sample stub functions for the first two tasks.
First, the read file activity stub function (which when finished will call another function to actually read the file, this one is mainly responsible for asking the user for the file name and dealing with errors).
// Performs the following tasks// - Requests a file name from the user// - Assigns the result of calling the readFile function to result// - Handles any errors from readFile// - Returns the result vectorvector <int> readFileTask(){ vector <int> result; // TO DO - read file into result return result;}And here is the print vector activity, this one doesn't need a calling function, as it is just going to print int_vec.
// Prints a vector of ints// PARAM: v is the vector to be printedvoid printVector(const vector<int> & v){ // TO DO - print v}Part 1 Summary
There is quite a lot to do here, but if you implement Part 1 like this you should be able to then handle each of the other tasks one at a time, and independent from each other. Pseudo-code for your main function will look something like this. This is a fairly high level description so I've let out variable declarations, which would include a vector for the data, and a character for the input.
int_vec = readFileTask() (a stub function at this point)input = instructions()while input is not 'q' if input is 1 int_vec = readFileTask() else if input is 2 printVector(int_vec) else if // ... options 3, 4, 5 and 6 end if statement input = instructions()end while loopprint goodbye
Part 2 - Reading Data From Files
Write a function that reads numeric data from a file of unknown length into a vector of ints. The vector should be returned by the function. The function should have one string parameter for the name of the file to be opened. The function should throw an invalid_argument error if the file could not be opened, and should ignore any lines in the file that cannot be inserted into the vector of ints (because they contain non numeric characters).
Now complete the stub function that calls your readFile function. This function should prompt the user for the name of the file to be opened and handle invalid_argument errors by requesting a new file name, or allowing the user to quit.
Part 3 - Printing a Vector
Write a void function with a constant vector reference parameter that prints its single constant vector<int> reference parameter, with one value per line. You can now replace your stub function with this function, using int_vec as its argument.
Part 4 - Calculating the Sum
Write a function returns an int which is the sum of the values in its constant vector<int> reference parameter.
Example - if the vector contains {3,4,4,6,10}Sum = 3+4+4+6+10 = 27Now complete the stub function for this task. The stub function should call your sum function and print the result. The stub function should be void and should have a constant vector reference parameter which it will pass to the sum function.
Part 5 - Calculating the Average
Write a function that returns a double which is the average of the values in an int vector. Your function should return (not print) the average, and should have a single constant vector<int> reference parameter.
Example - if the vector contains {3,4,4,6,10}Average = (3+4+4+6+10)/5 = 27/5 = 5.4Now complete the stub function for this task. The stub function should call your average function and print the result. The stub function should be void and should have a constant vector reference parameter which it will pass to the average function.
The standard deviation of a sample (or data set) is a measure of the amount by which the values in the sample differ from the mean. Standard deviation is used as a measure of statistical dispersion.
The standard deviation is the square root of the average sum of squared deviates (that is deviations, or differences from the mean).
standard deviation = root( sum( (v[i] - avg(v))2 ) / n ) - note the parentheses here!Write a function to calculate and return the standard deviation of its single constant vector<int> reference parameter. Use your average function (part 5) to calculate the average and assign this value to a variable that you can then use in a for loop that calculates the sum of squares. Once you've calculated the sum of squares you can calculate the standard deviation.
Here is an example of the calculation.
Example - arr = {3,4,4,6,10}, the average = 5.4Sum of Squares:((3 - 5.4)2 + (4 - 5.4)2 + (4 - 5.4)2 + (6 - 5.4)2 + (10 - 5.4)2)= (5.76 + 1.96 + 1.96 + 0.36 + 21.16)= 31.2standard deviation = root(31.2 / 5) = 2.498Now complete the stub function for this task. The stub function should call your deviation function and print the result. The stub function should be void and should have a constant vector reference parameter which it will pass to the deviation function.
Part 7 - Sharing
In this part you are to do the following tasks.
Create two new (empty) vectors (which I'll imaginatively refer to as vector1 and vector2)
Distribute the values in int_vec between these two vectors so that the sums of the two new vectors are as close as possible to each other
Print the sum of vector1 and vector2
Write the data in the two new vectors to files whose names are specified by the user
Part of the assignment is for you to come up with a good method of distributing the values. Here is a very simple method (but not a good one) to illustrate what we are trying to do.
Taking Turns
With this algorithm I'll distribute the values in int_vec by assigning taking turns to assign values to the new vectors (i.e. all even indexed elements to to vector1 and all odd indexed vectors go to vector2). Here is an example.
int_vec = {4,6,1,12,11,21,3,8} sum = 66give 4 to vector1, then 6 to vector2, then 1 to vector1, 12 to vector2, ...vector1 = {4,1,11,3} sum1 = 19vector1 = {6,12,21,8} sum2 = 47As you can see this method does not necessarily give very good results, since sum1 and sum2 are quite different. There are better ways to achieve this task than this.
Example Results
Here is a sample run showing all of the results for a file that contains the values 3, 4, 4, 6 and 10.
And here is another sample file. I ran all of the statistical functions on this file, and some algorithms for sharing the values. So that you have something to compare your results to here are my results from testing this file.
Statistics
sum = 1,723
average = 43.075
standard deviation = 28.8135
Sharing
Here are some results of three methods of sharing the values in the input vector between two other vectors.
Taking turns algorithms (as described above) - sum of vector1 = 734, sum of vector2 = 989
A different algorithm - sum of vector1 = 874, sum of vector2 = 849
Another different algorithm - sum of vector1 = 862, sum of vector2 = 861
If you want to give your sharing algorithm an even better work out add the value 500 as the last value in the file and see what happens! Remember that part of this assignment is for you to come up with a good algorithm, so I'm not going to tell you how to do it.
By the way don't forget to write your two new vectors to files!
Nobody here is going to do that for you. Anyway, you can't always create a good structure right at the start of a project. I myself am terrible when it comes to code design, so my original plans are always very very basic, and change as I work. Just take it on step by step, don't let it overwhelm you, and pick a small, simple place to start.
think about design, choose a small task, do it, iterate.
After all the effort you put into typing in all that verbiage you should have a pretty good idea where to start. Follow the directions given to you. It is pretty complete and explicit.
okay people i know what i wanted to ask wasn't clear because i had to leave the process half way through i know that is quite all i need help with is writing that catch and throw statement here as in think it is called that try function
if i were given a file to read it from how would i make sure that it eliminates anything that is not a number
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
//Forward Declarations
char instructions(); // Prints intructions, get user input and return input
vector<int> readFile(); //Read file into int_vec
void printContents(const vector<int> & int_vec); //Print contents of int_vec
void printSum(const vector<int> & int_vec); //Print sum of int_vec
void printMean(const vector<int> & int_vec); //Print mean of int_vec
void printStandardDev(const vector<int> & int_vec); //Print standard deviation of int_vec
void distributeVecotrs(const vector<int> & int_vec); //Distribute values of int_vec into two vectors with equal sum, then print the sums
double mean(const vector<int> & int_vec);
int sum(const vector<int> & int_vec);
//Print instructions
//Get user input
//Return user input
char instructions(){
char input = '';
cout << "Please select one of the following options" << endl;
cout << "1 - Open number file" << endl;
cout << "2 - Print data" << endl;
cout << "3 - Print the sum of the data" << endl;
cout << "4 - Print the average of the data" << endl;
cout << "5 - Print the standard deviation of the data" << endl;
cout << "6 - Distribute the values evenly into two files" << endl;
cout << "q - Quit" << endl;
cin >> input;
return input;
}
// Performs the following tasks
// - Requests a file name from the user
// - Assigns the result of calling the readFile function to result
// - Handles any errors from readFile
// - Returns the result vector
vector<int> readFile(string fname){
vector <int> result;
cout<< "Please enter file name: ";
cin>> fname;
ifstream ist(fname.c_str());
while (!ist){
throw invalid_argument("File could not be opened");
ist.clear();
cout<< "Please enter file name: ";
cin>> fname;
ist.open(fname.c_str());
}
int temp;
ist >> temp;
if (ist.fail()){
ist.clear();
ist.ignore(1000);
}
else{
result.push_back(temp);
}
return result;
}
// Prints a vector of ints
// PARAM: int_vec is the vector to be printed
void printContents(const vector<int> & int_vec){
for(int i = 0; i < int_vec.size(); ++i){
cout << int_vec[i] << endl;
}
}
//Print sum of values in int_vec
void printSum(const vector<int> & int_vec){
cout << "The sum of the data: " << sum(int_vec) << endl;
}
//Calculate sum of values in int_vec
int sum(const vector<int> & int_vec){
int result = 0;
for (int i = 0; i < int_vec.size(); ++i){
result += int_vec[i];
}
return result;
}
//Print mean of values in int_vec
void printMean(const vector<int> & int_vec){
cout << "The average of the data: " << mean(int_vec << endl;
}
//Calculate mean of values in in int_vec
double mean(const vector<int> & int_vec){
double result = sum(int_vec)/ int_vec.size();
return result;
}
it is not my homework i am trying to learn how to work with c++ it been hours since i have been reading book but i can't figure it out it kind of sucks now :|
okay people i know what i wanted to ask wasn't clear because i had to leave the process half way through i know that is quite all i need help with is writing that catch and throw statement here as in think it is called that try function
if i were given a file to read it from how would i make sure that it eliminates anything that is not a number
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
usingnamespace std;
//Forward Declarations
char instructions(); // Prints intructions, get user input and return input
vector<int> readFile(); //Read file into int_vec
void printContents(const vector<int> & int_vec); //Print contents of int_vec
void printSum(const vector<int> & int_vec); //Print sum of int_vec
void printMean(const vector<int> & int_vec); //Print mean of int_vec
void printStandardDev(const vector<int> & int_vec); //Print standard deviation of int_vec
void distributeVecotrs(const vector<int> & int_vec); //Distribute values of int_vec into two vectors with equal sum, then print the sums
double mean(const vector<int> & int_vec);
int sum(const vector<int> & int_vec);
int main(){
try{
vector<int> int_vec = readFile();
}catch(invalid_argument){
cerr << "File opening error" << endl;
}
char input = instructions();
while (input != 'q'){
if(input == 1){
int_vec = readFile();}
elseif(input == 2){
printContents(int_vec);}
elseif(input == 3){
printSum(int_vec);}
elseif(input == 4){
printMean(int_vec);}
elseif(input == 5){
printStandardDev(int_vec);}
else(input == 6){
distributeVectors(int_vec);}
input = instructions()
}
cout << "Bye!";
return 0;
}
//Print instructions
//Get user input
//Return user input
char instructions(){
char input = '';
cout << "Please select one of the following options" << endl;
cout << "1 - Open number file" << endl;
cout << "2 - Print data" << endl;
cout << "3 - Print the sum of the data" << endl;
cout << "4 - Print the average of the data" << endl;
cout << "5 - Print the standard deviation of the data" << endl;
cout << "6 - Distribute the values evenly into two files" << endl;
cout << "q - Quit" << endl;
cin >> input;
return input;
}
// Performs the following tasks
// - Requests a file name from the user
// - Assigns the result of calling the readFile function to result
// - Handles any errors from readFile
// - Returns the result vector
vector<int> readFile(string fname){
vector <int> result;
cout<< "Please enter file name: ";
cin>> fname;
ifstream ist(fname.c_str());
while (!ist){
throw invalid_argument("File could not be opened");
ist.clear();
cout<< "Please enter file name: ";
cin>> fname;
ist.open(fname.c_str());
}
int temp;
ist >> temp;
if (ist.fail()){
ist.clear();
ist.ignore(1000);
}
else{
result.push_back(temp);
}
return result;
}
// Prints a vector of ints
// PARAM: int_vec is the vector to be printed
void printContents(const vector<int> & int_vec){
for(int i = 0; i < int_vec.size(); ++i){
cout << int_vec[i] << endl;
}
}
//Print sum of values in int_vec
void printSum(const vector<int> & int_vec){
cout << "The sum of the data: " << sum(int_vec) << endl;
}
//Calculate sum of values in int_vec
int sum(const vector<int> & int_vec){
int result = 0;
for (int i = 0; i < int_vec.size(); ++i){
result += int_vec[i];
}
return result;
}
//Print mean of values in int_vec
void printMean(const vector<int> & int_vec){
cout << "The average of the data: " << mean(int_vec << endl;
}
//Calculate mean of values in in int_vec
double mean(const vector<int> & int_vec){
double result = sum(int_vec)/ int_vec.size();
return result;
}
sorry didn't know how to do that before, as in how to post code :) but here it is now. any advice welcome if i could improvise something and you could let me know that would be great as well
okay so i have got this far and now i was trying to run it and i am not sure what things are going wrong i am going to post the code and the errors i get couuld someone please help me fix them i am unable to fix them.
// jaskiran.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <cmath>
usingnamespace std;
//Forward Declarations
char instructions(); // Prints intructions, get user input and return input
vector<int> getFile(); //Get file name from user, call readFile() and return vector
vector<int> readFile(); //Read file into int_vec
void printContents(const vector<int> & int_vec); //Print contents of int_vec
void printSum(const vector<int> & int_vec); //Print sum of int_vec
void printMean(const vector<int> & int_vec); //Print mean of int_vec
void printStandardDev(const vector<int> & int_vec); //Print standard deviation of int_vec
//void distributeVecotrs(const vector<int> & int_vec); //not progamed yet //Distribute values of int_vec into two vectors with equal sum, then print the sums
double mean(const vector<int> & int_vec); //Calculate mean of int_vec
int sum(const vector<int> & int_vec); //Calculate sum of int_vec
double deviation(const vector<int> & int_vec);
int main(){
vector<int> int_vec = getFile();
char input = instructions();
while (input != 'q'){
if(input == 1){
int_vec = getFile();
}elseif(input == 2){
printContents(int_vec);
}elseif(input == 3){
printSum(int_vec);
}elseif(input == 4){
printMean(int_vec);
}elseif(input == 5){
printStandardDev(int_vec);
//}else if(input == 6){
// distributeVecotrs(int_vec);
}
input = instructions();
}
cout << "Bye!";
return 0;
}
//Print instructions
//Get user input
//Return user input
char instructions(){
char input;
cout << "Please select one of the following options" << endl;
cout << "1 - Open number file" << endl;
cout << "2 - Print data" << endl;
cout << "3 - Print the sum of the data" << endl;
cout << "4 - Print the average of the data" << endl;
cout << "5 - Print the standard deviation of the data" << endl;
cout << "6 - Distribute the values evenly into two files" << endl;
cout << "q - Quit" << endl;
cin >> input;
return input;
}
//PARAM: fname
//Open file and throw invalid_argument if file could not be opened
//Put file into a vector of ints
//Ignore inappropriate values
//Return vector
vector<int> readFile(string fname){
ifstream ist(fname.c_str());
while (!ist){
throw invalid_argument("File could not be opened");
}
vector<int> result;
int temp;
while (ist >> temp){
result.push_back(temp);
}
if (!ist){
ist.clear();
ist.ignore(1000, '/n');
}
return result;
}
// - Requests a file name from the user
// - Assigns the result of calling the readFile function to result
// - Handles any errors from readFile
// - Returns the result vector
vector<int> getFile(){
vector<int> result;
cout << "Please enter file name: ";
string fname;
cin >> fname;
try {
result = readFile(fname);
}catch(invalid_argument & ia){
cerr << ia.what() << endl;
cout<< "Please enter file name: ";
cin >> fname;
result = readFile(fname);
}
return result;
}
// Prints a vector of ints
// PARAM: int_vec is the vector to be printed
void printContents(const vector<int> & int_vec){
for(int i = 0; i < int_vec.size(); ++i){
cout << int_vec[i] << endl;
}
}
//Print sum of values in int_vec
void printSum(const vector<int> & int_vec){
cout << "The sum of the data: " << sum(int_vec) << endl;
}
//Calculate sum of values in int_vec
int sum(const vector<int> & int_vec){
int result = 0;
for (int i = 0; i < int_vec.size(); ++i){
result += int_vec[i];
}
return result;
}
//Print mean of values in int_vec
void printMean(const vector<int> & int_vec){
cout << "The average of the data: " << mean(int_vec) << endl;
}
//Calculate mean of values in in int_vec
double mean(const vector<int> & int_vec){
double result = sum(int_vec)/ int_vec.size();
return result;
}
//Call deviation()
//Print result
void printStandardDev(const vector<int> & int_vec){
cout << "The standard deviation: " << deviation(readFile()) << endl;
}
//Calculate standard deviation
double deviation(const vector<int> & int_vec){
double average = mean(int_vec);
double result = 0;
for (int i = 0; i < int_vec.size(); ++i){
result = result + pow(int_vec[i] - average, 2);
}
double standard_deviation = sqrt(result/int_vec.size());
return standard_deviation;
}
Sorry to have responded so late. A very quick pass shows that on lines 113, 126, and 153 the compiler is complaining about the sign of the types used in the comparison. You can fix this by using the right type in the loop, or by casting:
112 113 114 115 116
void printContents(const vector<int> & int_vec){
for(size_t i = 0; i < int_vec.size(); ++i){
cout << int_vec[i] << endl;
}
}
The last one is because you have declared a function typed:
vector<int> readFile();
but declared an identically-named (but different) function:
vector<int> readFile(const string& fname){
The compiler is complaining that it cannot find the no-arguments "readFile" function.
That isn't such a problem, though, but way down on line 146 you use readFile() when the data you should be using the data given to the argument of the function, like this:
145 146 147
void printStandardDev(const vector<int> & int_vec){
cout << "The standard deviation: " << deviation(int_vec) << endl;
}
I haven't tested your code for logical correctness.