trouble with function

this program is supposed to get a file throught CMD input redirection, and i have it working, but the function for average seems to keep giving me the wrong number. i keep running through it on paper and i dont get the same answer that the program gives me. can anyone see a reason as to why that might be happening?

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
  #include <iostream>
#include <iomanip>
using namespace std;

void Display (int Count[]);
void WordLength (int Count[]);
float AverageLength (int Count[]);

const int SIZE=16;


int main()
{
	int Count[16]={0};
	
	WordLength (Count);
	Display (Count);
	return 0;
}

void Display (int *Count)
{
	cout<<"Word Length            Frequency"<<endl;
	cout<<"-----------            ---------"<<endl;
	cout<<"     1                    "<<Count[1]<<endl;
	cout<<"     2                    "<<Count[2]<<endl;
	cout<<"     3                    "<<Count[3]<<endl;
	cout<<"     4                    "<<Count[4]<<endl;
	cout<<"     5                    "<<Count[5]<<endl;
	cout<<"     6                    "<<Count[6]<<endl;
	cout<<"     7                    "<<Count[7]<<endl;
	cout<<"     8                    "<<Count[8]<<endl;
	cout<<"     9                    "<<Count[9]<<endl;
	cout<<"    10                    "<<Count[10]<<endl;
	cout<<"    11                    "<<Count[11]<<endl;
	cout<<"    12                    "<<Count[12]<<endl;
	cout<<"    13                    "<<Count[13]<<endl;
	cout<<"    14                    "<<Count[14]<<endl;
	cout<<"    15                    "<<Count[15]<<endl<<endl<<endl;
	cout<<"Average word length: "<<AverageLength(Count);
}


float AverageLength(int Count[])
{
	float Temp=0, Temp2=0, Temp3=0, Ans;
	int i;

	for (i=1; Count[i]<=SIZE; ++i)
	{
		Temp=i*Count[i];
		Temp2+=Temp;
	}
	for (i=1; Count[i]<=SIZE; ++i)
		Temp3+=Count[i];
	Ans= Temp2/Temp3;
	return Ans;
}


void WordLength (int Count[])
{
	char Letter;
	int Length;

	cin.get(Letter);
	while (Letter!='\n')
	{
		Length=0;
		while (isspace(Letter))
			cin.get(Letter);
		while (Letter!=' ')
		{
			if (Letter=='-')
			{
				cin.get(Letter);
				if (Letter==0)
					cin.get(Letter);
			}
			else if (Letter=='\n')
			{
				if (Length>15)
					++Count[15];
				else
					++Count[Length];
				return;
			}
			else
			{
				++Length;
				if (ispunct(Letter))
					--Length;
				cin.get(Letter);
			}
		}
		if (Length>15)
			++Count[15];
		else
			++Count[Length];
	}
}
I've never seen a more complicated average function.
http://www.mathsisfun.com/mean.html

compare the information in the site to what your program is doing
sorry for not explaining properly, it's not calculating the average of a set of values, it's calculating average word length of set of words, i need to get the total number of letters in all words divided by the number of words. if there's a simpler way to do that then the function above, do tell.
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
#include <iostream>
#include <string>
#include <algorithm>

double avgWordLength(const std::string& string) {
	//this function assumes that the string has less than or equal to ~65535 spaces
	//it also assumes that the string doesn't begin with a space (words are separated by spaces).
	//does not discriminate between alphabetical and non-alphabetical characters.
	//but, obviously, it discriminates against spaces.

	unsigned short nWords = std::count(string.begin(), string.end(), ' ')+1;
	return ((string.size()-(nWords-1))/(double)nWords);
}

int main(int argc, char* argv[]) {

	std::string string = "This is a string with words in it";

	double avg = avgWordLength(string);

	std::cout << "The average word length is " << avg << " characters." << std::endl;

	std::cin.get();
	return 0;
}


You would need to process the string before passing it to the avgWordLength() function, unless, like in the example above, your string is guaranteed to meet the restrictions of the avgWordLength() function (valid alphabetical characters, no excessive white space, etc).
Last edited on
Topic archived. No new replies allowed.