# so i am working with vectors and here are some question i have.

• 03-07-2011
avnis
so i am working with vectors and here are some question i have.
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

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.

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.

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).

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.

Write a function returns an int which is the sum of the values in its constant vector<int> reference parameter

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.

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).

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.

I amputting the code below could some guide with my catch and try and ivalid argument function and also go over my program to see if i got stuff wrong or right i am a beginner.

Code:

```#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); 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();}                 else if(input == 2){                         printContents(int_vec);}                 else if(input == 3){                         printSum(int_vec);}                 else if(input == 4){                         printMean(int_vec);}                 else if(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; }```