vector program, unknown errors.

code to store exam scores, find average, and compute standard deviation, it has one REALLY long syntax error, i don't know what it is, please help =[

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
  #include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> score;

void fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while((cin>>input) && (input!=-99))
	{
		cin>>input;
		score.push_back(input);
	}
}
void displayArray(vector<int> & score)
{
	cout<<"The numbers in the vector are: "<<endl;
	for(int i=0; i<score.size();i++)
	cout << score[i] <<" ";
}
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores, average;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += exam_scores[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
};
double computeStandardDeviation(vector<int> &score, double average)
{
    double E=0;
      double inverse = 1.0 / static_cast<double>(score.size());
    for(unsigned int i=0;i<score.size();i++)
    {
        E += pow(static_cast<double>(score[i]) - average, 2);
    }
    return sqrt(inverse * E);
}

int main()
{
	cout<<fillArray(score)<<endl;
	cout<<displayArray(score)<<endl;
	cout<<"class average: "<<computeAvg(score)<<endl;
	cout<<"standard deviation: "<<computeStandardDeviation(score)<<endl;
return 0;
}
You can't print the value returned from a function that returns nothing (void).

Okay fixed that, and some others, now brought it down to only
"program4.cpp:11: error: ISO C++ forbids declaration of `fillArray' with no type
program4.cpp: In function `double computeAvg(std::vector<int, std::allocator<int> >&)':
program4.cpp:31: error: invalid types `int[int]' for array subscript
program4.cpp: In function `int main()':
program4.cpp:49: error: invalid operands of types `int' and `<unknown type>' to binary `operator<<'
program4.cpp:50: error: void value not ignored as it ought to be
program4.cpp:37: error: too few arguments to function `double computeStandardDeviation(std::vector<int, std::allocator<int> >&, double)'
program4.cpp:52: error: at this point in file"
Please post your modified code.
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
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> score;

fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while((cin>>input) && (input!=-99))
	{
		cin>>input;
		score.push_back(input);
	}
}
void displayArray(vector<int> & score)
{
	cout<<"The numbers in the vector are: "<<endl;
	for(int i=0; i<score.size();i++)
	cout << score[i] <<" ";
}
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores, average;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += sum_of_exam_scores[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
};
double computeStandardDeviation(vector<int> &score, double average)
{
    double E=0;
      double inverse = 1.0 / static_cast<double>(score.size());
    for(unsigned int i=0;i<score.size();i++)
    {
        E += pow(static_cast<double>(score[i]) - average, 2);
    }
    return sqrt(inverse * E);
}

int main()
{
	fillArray(score)<<endl;
	displayArray(score)<<endl;
	cout<<"class average: "<<computeAvg(score)<<endl;
	cout<<"standard deviation: "<<computeStandardDeviation(score)<<endl;
return 0;
}
Look at your fillArray() implementation, what does this function return. C++ requires the programmer to state the return type.

Next is sum_of_exam_scores an array?

Next look at the line numbers for the following messages:
1
2
3
program4.cpp: In function `int main()':
program4.cpp:49: error: invalid operands of types `int' and `<unknown type>' to binary `operator<<'
program4.cpp:50: error: void value not ignored as it ought to be


Do you notice anything "extra".

please tell me what you mean by line 49 & 50, what "extra" do you see ? (fixed the others btw)
You can't do this:
1
2
fillArray(score)<<endl;
	displayArray(score)<<endl;


It should be like this:
1
2
3
4
fillArray(score)
cout << endl;
	displayArray(score)
cout << endl;


Also:

Where is your type for the function here? You should declare a data type for this function.

1
2
3
4
5
6
7
8
9
10
fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while((cin>>input) && (input!=-99))
	{
		cin>>input;
		score.push_back(input);
	}
}


Also, here:

If you want to return average, which is int, why is your function type 'double'?
that could make a difference in calculations.

1
2
3
4
5
6
7
8
9
10
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores, average;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += sum_of_exam_scores[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
};


So changed to this:
1
2
3
4
5
6
7
8
9
10
11
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores;
double average;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += sum_of_exam_scores[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
};


Also:

cout<<"standard deviation: "<<computeStandardDeviation(score)<<endl;

Your function, computeStandardDeviation should take 2 arguments (two parameters), why are you passing only one here?

In your int main, you should declare variables to hold your function calls.

Like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    double Average = 0.0;
    double StandDev = 0.0;
	fillArray(score);
	cout << endl;
	displayArray(score);
	cout << endl;
	Average = computeAvg(score);
	cout<<"class average: "<<Average<<endl;
	StandDev = computeStandardDeviation(score, Average);
	cout<<"standard deviation: "<<StandDev<<endl;
    return 0;
}


Now this last function made me cry:

1
2
3
4
5
6
7
8
9
10
11
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores;
double average;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += sum_of_exam_scores[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
}


Wtf? Why are you treating sum_of_exam_scores like an array? Did you forget that it is a variable with datatype int? It's not an array.

I think what you're trying to do is this:
1
2
3
4
for (int count = 0; count < score.size(); count++)
{
	sum_of_exam_scores += score[count];
}


You want to iterate through the vector and add those values to sum_of_exam_scores;

so change that in the function.

Your code will be:
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
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> score;

void fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while((cin>>input) && (input!=-99))
	{
		cin>>input;
		score.push_back(input);
	}
}
void displayArray(vector<int> & score)
{
	cout<<"The numbers in the vector are: "<<endl;
	for(int i=0; i<score.size();i++)
	cout << score[i] <<" ";
}
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores = 0;
double average = 0.0;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += score[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
}
double computeStandardDeviation(vector<int> &score, double average)
{
    double E=0;
      double inverse = 1.0 / static_cast<double>(score.size());
    for(unsigned int i=0;i<score.size();i++)
    {
        E += pow(static_cast<double>(score[i]) - average, 2);
    }
    return sqrt(inverse * E);
}

int main()
{
    double Average = 0.0;
    double StandDev = 0.0;
	fillArray(score);
	cout << endl;
	displayArray(score);
	cout << endl;
	Average = computeAvg(score);
	cout<<"class average: "<<Average<<endl;
	StandDev = computeStandardDeviation(score, Average);
	cout<<"standard deviation: "<<StandDev<<endl;
    return 0;
}


Now it compiles but I think it doesn't do what it's supposed to do. So try to figure out what's the problem?

I will help you, the problem is your while at line 14, loop never stops. Because again I don't know why you have two conditions there.. do we really need that condition? I mean (cin>>input) && (input!=-99)

We don't need cin >> input, we only need input!=-99

So your code is now working.

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
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> score;

void fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while(input!=-99)
	{
		cin>>input;
		score.push_back(input);
	}
}
void displayArray(vector<int> & score)
{
	cout<<"The numbers in the vector are: "<<endl;
	for(int i=0; i<score.size();i++)
	cout << score[i] <<" ";
}
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores = 0;
double average = 0.0;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += score[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
}
double computeStandardDeviation(vector<int> &score, double average)
{
    double E=0;
      double inverse = 1.0 / static_cast<double>(score.size());
    for(unsigned int i=0;i<score.size();i++)
    {
        E += pow(static_cast<double>(score[i]) - average, 2);
    }
    return sqrt(inverse * E);
}

int main()
{
    double Average = 0.0;
    double StandDev = 0.0;
	fillArray(score);
	cout << endl;
	displayArray(score);
	cout << endl;
	Average = computeAvg(score);
	cout<<"class average: "<<Average<<endl;
	StandDev = computeStandardDeviation(score, Average);
	cout<<"standard deviation: "<<StandDev<<endl;
    return 0;
}


ONE last thing, the -99 will still go inside the vector which we don't want to do.

So we make an if statement inside while.

Look at this:

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
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

vector<int> score;

void fillArray(vector<int> & score)
{
	int input;
	cout<<"Enter a list of scores, -99 to mark the end of the list"<<endl;
	while(input!=-99)
	{
		cin>>input;
		if(input != -99)
		{
		    score.push_back(input);
		}
		
	}
}
void displayArray(vector<int> & score)
{
	cout<<"The numbers in the vector are: "<<endl;
	for(int i=0; i<score.size();i++)
	cout << score[i] <<" ";
}
double computeAvg(vector<int> &score)
{
int sum_of_exam_scores = 0;
double average = 0.0;
for (int count = 0; count < score.size(); count++)
    {
        sum_of_exam_scores += score[count];
    }
    average = sum_of_exam_scores / score.size();
    return average;
}
double computeStandardDeviation(vector<int> &score, double average)
{
    double E=0;
      double inverse = 1.0 / static_cast<double>(score.size());
    for(unsigned int i=0;i<score.size();i++)
    {
        E += pow(static_cast<double>(score[i]) - average, 2);
    }
    return sqrt(inverse * E);
}

int main()
{
    double Average = 0.0;
    double StandDev = 0.0;
	fillArray(score);
	cout << endl;
	displayArray(score);
	cout << endl;
	Average = computeAvg(score);
	cout<<"class average: "<<Average<<endl;
	StandDev = computeStandardDeviation(score, Average);
	cout<<"standard deviation: "<<StandDev<<endl;
    return 0;
}


Sample input:

1
2
3
4
5
6
7
8
9
10
11
Enter a list of scores, -99 to mark the end of the list
5
5
5
5
-99

The numbers in the vector are: 
5 5 5 5 
class average: 5
standard deviation: 0
Last edited on
Topic archived. No new replies allowed.