...compiling error in my code....

hey every one..
I have tried my code but it has some errors and mistakes!!!!
any one could help me solve this problem,,,please resend the code with corrections

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
  
#include<iostream>
#include<fstream>
#include<string>
#include<cmath>
using namespace std;
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 C[];
ifstream infile;
int size;
int main()
{

	int selection;
	double result;
	string filename;
	double V[5];
	do{
		DisplayMenu();
		cout<<"Selection: "<<selection<<endl;
		cout<<"Please enter the name of the file which contains the list of data:";
				cin>>filename;
				infile.open(filename.c_str());
					if (infile.fail())
					{
						cout<<"Error opening input file"<<endl;
	 				return 1;
					}
	
	
			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++)
					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);
		infile.close();
	return 0;
}//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−−−−−−−−−√
For one, the "selection" variable is never allocated- you simply state its value and throw it into a switch. Yes, you returned the value of selection in displaymenu, but since you didn't actually store that returned value into selection, the input was meaningless. Second, your "statistics" case does absolutely nothing other than display the words that precede the statistics, then not actually call any functions. In your vector case, the arrays that you are reading from are also not allocated (empty brackets), not to mention that size is never defined as well, so those for loops will go on to whatever ungodly value is stuck in there. And, since the arrays were never allocated, the compiler would allow that... which could cause some serious issues. Your case 3, dot-product of vectors, also has the same issues as case 2... plus calling the dot product function while passing to it another undefined number, result. Your case 4 is pointless, and there is no default case in that switch. Also, the second and third cases don't break. Immediately following the switch is an infinite loop (you can't have a while loop where selection != 4 if selection never changes in that loop). Worse, your dotproduct function has the value (which, due to not being stated, is some gibberish number) sent to it as the address of result. ReadVector is also never used once, nor are any of your statistics functions. Also, your method for checking if the two vectors have the same size is foolish- you throw in an unspecified value into two arrays of unallocated memory, and compare the two. That tells you nothing about their size. In your un-used statistics file, you forgot to put braces around the first for loop.

However, if you want to know just what the compilation error is, you forgot to include cmath when you used the sqrt function.
Ohh my god.....
thank you very much
could you please resend my code with correction?

i'm a beginner student in c++
thanks Ispil
No way. You are the beginner. You deserve the opportunity to practice by writing code yourself. That will help you learn.
Topic archived. No new replies allowed.