#include<iostream>//required for cin and cout
#include<fstream>//required for ifstream
#include<string>//required for string,c_str()
#include<cmath>//required for pow and sqrt
#include<iomanip>//required for setprecision
usingnamespace std;
//Declare function prototypes
void ReadVector(double V[], int &size, ifstream infile);
int DisplayMenu (void);
void VectorAdd(constdouble A[], constdouble B[], double C[], int size);
void DotProduct(constdouble A[], constdouble B[], int size, double &result);
void Simple_Stat(constdouble list[], int size, double &average, double&std_dev);
int selection;
constdouble A[];
constdouble B[];
double average,std_dev;
double C[];
ifstream infile;
int size;
int main()
{
//declare and initilize objects.
int selection;
double result;
string filename;
double V[5];
do{
DisplayMenu();
cout<<"Selection: "<<selection<<endl;
//prompt user for name of input file.
cout<<"Please enter the name of the file which contains the list of data:";
cin>>filename;
//open input file, exit if fail
infile.open(filename.c_str());
if (infile.fail())
{
cout<<"Error opening input file"<<endl;
return -1; // Unsuccessful termination
}
switch(selection)
{
case 1:
Simple_Stat(A, size,average,std_dev);
cout<<"Results of the Simple Statistics are:"<<endl;
cout<<"Average:"<<average<<endl;
cout<<"Standard Deviation:"<<std_dev<<endl;
break;
case 2:
cout<<"The vectors to have their Vector Addition are:"<<endl;
cout<<"Vector 1:";
for(int i=0 ; i < size ; i++)
//set format flags
cout<<A[i]<<endl;
cout<<"Vector 2:";
for(int i=0 ; i < size ; i++)
cout<<B[i]<<endl;
VectorAdd(A,B, C, size);
cout<<"The additional vector is: ";
for(int i=0 ; i<size ; i++)
cout<<C[i]<<endl;
break;
case 3:
cout<<"The vectors to have their Dot Product are:"<<endl;
cout<<"Vector 1: ";
for(int i=0 ; i < size ; i++)
cout<<A[i]<<endl;
cout<<"Vector 2: ";
for(int i=0 ; i < size ; i++)
cout<<B[i]<<endl;
DotProduct(A,B,size,result);
cout<<"Dot product result:"<<result<<endl;
break;
default:
break;
}//switch
}//do
while( selection!=4);
//close file and exit programe.
infile.close();
return 0;
}//end main
void ReadVector(double V[], int &size, ifstream infile)
{
int size;
infile>>size;
for(int i=0 ; i < size ; i++)
infile>>V[i];
}
// a function to display menu and return selection
int DisplayMenu (void)
{
cout<<"Please select one of the following choices in this menu:"<<endl;
cout<<"1.Simple Statistics"<<endl;
cout<<"2.Vector Addition"<<endl;
cout<<"3.Dot Product"<<endl;
cout<<"4. Exit"<<endl;
cin>>selection;
return selection;
}
void VectorAdd(constdouble A[], constdouble B[], double C[], int size)
{
int i;
//check that two vectors have same size
if(A[i] == B[i])
{
for ( int i = 0 ; i < size ; i++ )
C[i]= A[i] + B[i];
}
else
cout<<"the two vectors should have same size"<<endl;
}
// a function dot product
void DotProduct(constdouble A[], constdouble B[], int size, double &result)
{
double sum=0;
int i;
if(A[i] == B[i])
{
for ( int i = 0 ; i < size ; i++ )
result = A[i] * B[i];
sum +=result;
}
else
cout<<"the two vectors should have same size"<<endl;
}
//a function simple statistics
void Simple_Stat(constdouble list[], int size, double &average, double&std_dev)
{
double sum=0,sum2,result,result2;
for(int i=0; i< size ; i++)
{
result = list[i];
sum +=result;
}
average=sum/size;
for(int i=0; i< size ; i++)
{
result2 = pow((list[i]-average),2);
}
sum2 +=result2;
std_dev=sqrt(sum2/(size-1));
}
Write a user-friendly C++ program that allows a user to select repeatedly an operation to be performed from a displayed menu of actions. The menu includes the following items:
1. Simple Statistics
2. Vector Addition
3. Dot Product
4. Exit
The menu actions are described by the following points:
1- Simple Statistics: This action includes finding the average, and standard deviation of a list of data items. The option should allow the user to specify the name of a file from which the data are read into an array. All the results of this option are displayed on the screen.
2- Vector Addition: This action allows the user to enter the name of a file from which two vectors, V1 and V2, of equal size N are read. Accordingly, the two vectors are added, and displayed along with the resultant vector on the screen. Your program should check that the two added vectors are of the same size. Otherwise, the program should display a message and allow the user to make another choice from the menu.
3- Dot Product: This action is similar to the Vector Addition in terms of input, output, and checking validity of the vector operands.
4- Exit: This action terminates the program.
Your program should include at least the following functions:
1. A function, called ReadVector(), which reads a vector of any size from a given file name, and returns the vector and its size to the caller. The function prototype is defined as follows:
void ReadVector(double V[ ], int &size, ifstream infile);
2. A function, called DisplayMenu(), which displays the menu and returns the user%u2019s choice as an integer value between 1 and 4. The function prototype is defined as follows:
int DisplayMenu (void);
3. A function, called VectorAdd(), which performs the addition of two vectors, A and B, and returns the resultant vector, C. The function prototype is defined as follows:
Please don't do duplicate topics. It's annoying because people ask questions & give advice only to find the same questions & advice has already been given in the other post.
We are not going to fix your code for you - that is your job. It's ours to give advice.