Can't find average letters.

I can't tell what I'm doing wrong to get the average?

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
#include <iostream>
#include <cstring>

using namespace std;

int findWhitespace(char[]);
double findAve(char[], int);

int main()
{
	//modified version of Program 12-2...
	const int LENGTH = 80;
	char line[LENGTH];

	cout << "Enter a sentence of no more than ";
	cout << LENGTH - 1 << " characters: " << endl;

	cin.getline(line, LENGTH);
	if (line[0] == ' ' || line[0] == '\0') 
	{

		cout << "Invalid sentence..." << endl;
		cout << "Enter a sentence of no more than ";
		cout << LENGTH - 1 << " characters: " << endl;

		cin.getline(line, LENGTH);

	}//end of if

	int a = findWhitespace(line);

	cout << endl << "The sentence has ";
	cout << a << " words in it." << endl;

	cout << "Average letters per word " << findAve(line, a) << endl;

}//end of main

int findWhitespace(char c[])
{

	int n = 1;//for the first word...

	for (int i = 0; c[i] != '\0'; i++)
	{
		if (c[i] == ' ')
		{
			n++; 
		}
	}

	return n;

}//end of findWhitespace

double findAve(char c[], int words) 
{
	int num, index = 0, letters[words];
	double total, ave;

	for (int j = 0; c[j] != '\0'; j++)
	{
		if (c[j] != ' ' || c[j] != '.') {

			num++;

		} else {

			letters[index] = num;
			index++;
			total += letters[j];
		}
	} 

	ave = total / words;
	return ave;

}//end of findAve 
Last edited on
What are you trying to average? Why are you trying to use variables before they are initialized? Where do you actually assign any values tour letters array?
I'm trying to find the average number of letters of words in a sentence.

I thought that I was putting values into letters[index] when the loop came up to "." or " "?
IMO your findAve() function has several major problems. One of the biggest problems is that you're using at least one variable that hasn't been initialized.

Next you're using a VLA in your function, VLA are not allowed in C++ programs. Array sizes must be compile time constants.

Next the use of this array seems inconsistent. You add a value to letters[index] but you "count" letters[j]. These two indexes are not necessarily the same value.


Last edited on
Okay, new function works...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int findAve(char c[], int numOfWords, int numOfLetters)
{

        for (int j = 0; c[j] != '\0'; j++)
        {
                numOfLetters++;

                if (c[j] == ' ' || c[j] == '.') {

                        numOfLetters--;

                }
        }

        return numOfLetters / numOfWords;

}//end of findAve 


Is is wrong to create variables inside of functions? That, and what is VLA?
No, it is actually better to create variables inside functions unless the variable contains a value from outside of the function. In your function above you only need to pass the character array since the other two variables are only used inside this function.

A VLA is a Variable Length Array which are only guaranteed to be supported in C99 programs. These constructs allow the creation of arrays with run time sizes. In C++ the alternative is to use a std::vector.

We were doing c-strings this week in class so we couldn't use anything we wanted to on the homework. This assignment was supposed to modify the one that came before it.

A lot of the code I see online doesn't use using namespace std; and uses the scope resolution operator on everything. Is that a better standard?
A lot of the code I see online doesn't use using namespace std; and uses the scope resolution operator on everything. Is that a better standard?

IMO, yes using the scope resolution operator is usually a better practice.

Topic archived. No new replies allowed.