check my code and tell me the mistakes and how to fix it

check my code and tell me the mistakes and how to fix it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #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
using namespace std;
//Declare function prototypes
void ReadVector(double V[], int &size, ifstream infile);
int DisplayMenu (void);
void VectorAdd(const double A[], const double B[], double C[], int size);
void DotProduct(const double A[], const double B[], int size, double &result);
void Simple_Stat(const double list[], int size, double &average, double&std_dev);
int selection;
const double A[];
const double 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(const double A[], const double 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(const double A[], const double 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(const double 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));
}
Last edited on
the original question is this....

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:

void VectorAdd(const double A[ ], const double B[ ], double C[ ], int size);

The sum of two vectors, A = (a1, a2, %u2026, an) and B = (b1, b2, %u2026, bn) of size n, is

defined as: C = A + B.

Where, C = (c1, c2, ....., cn), and ci = ai + bi, for1< i < n

4. A function, called DotProduct(), which performs the dot product of two vectors, A

and B, and returns the result. The function prototype is defined as follows:

void DotProduct(const double A[], const double B[], int size, double &result);

The dot-product of two vectors A = (a1, a2,......, an) and B = (b1, b2,......, bn) of size n

is defined as

A.B=
∑ni=1ai.bi=a1.b1+a2.b2+....an.bn

5. A function, called Simple_Stat(), which finds the maximum, minimum, average,

and standard deviation of a list of data items passed to it by a one-dimensional

array. The function returns the average and standard deviation values. The function

prototype is defined as follows:

void Simple_Stat(const double list[], int size, double &average, double

&std_dev);

The average, m of N numbers X1, X2, %u2026,XN is

m=
∑Ni=1XiN

and the standard deviation, S is:

S=
∑Ni=1(Xi−m)2N−1−−−−−−−−−√
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.

http://www.cplusplus.com/forum/beginner/101202/
Last edited on
Topic archived. No new replies allowed.