Arrays:adding and averaging(text file input)

Hello, everyone I am making a program that reads the data from a text file in the format of a single line. The program will count the number of entries on the line, and it will average that data. The program will then look at each individual number and determine how far away that number is from the overall average. I have my code started down below.
tree.txt input: 6.5 7.8 -1.4 9 22 -2.7 0 17
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
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
void treefunction(ifstream&);//function prototype says that ifstream will be used in the function  

int main()
{
		ifstream treefile("tree.txt");//allows file to be opened 
	treefunction(treefile);//tells the program that the tree will be used and opened in this file.
		treefile.close();//closes treefile
	
	return 0;//return value at the end.

}
void treefunction(ifstream& treefile)//allows file to be opened, and used with ifstream&. The ifstream says it will be a treefile.
{


	if (!treefile)//if statement to check if the file has opened correctly
	{
		cout<<"Please try again, unable to open text file"<<endl;//output if file does not open correctly
		
	}
	int count=8;//counts numbers
	treefile>>count;
	int numbers[count];
	int sum=0;
	for(int b=0; b<count; b++)
	{
		treefile>>numbers[b];
		sum=sum+numbers[count];
		
	}
	int b=0;
	for(int a=0; a<b;a++)
	{
		cout<<"The number of entries is: "<<numbers[b]<<endl;
	}
}

Last edited on
First, thank-you for using code tags. Your code is so much easier to read that way.

Do you have a question? If you run into an error, it is best to show us your code (which you did) and tell us what the error is. Then we can zero in on your problem.

However, I did see a couple of problems.

1. You are trying to read floating point number, but the array you are populating is an array of ints. Make numbers (line 30) and sum (line 31) doubles.

2. In line 30 you must use a constant int expression to dimension your array if it is on the stack. Because you are reading count in from the file, you must dynamically allocate the numbers array from the heap (using new). Don't forget to deallocate when done (using delete).

3. In line 35 you are adding numbers[count] to sum. Count, however, is the number of elements in the array. The valid array indices are 0 - (count - 1). numbers[count] is out of bounds. You really want numbers[b].

Let us know if you have any more problems.
Hello, thank you for the response, I do not understand problem 2.. this is my first time applying arrays in a program what does this mean, or how would this work?
I would like to add up all the entries, how would I do this? Didn't I try to do this with sum? However, the number is short when I was messing around?
To find the average I would simply divide that number by x?
How would I display how close or above the entry is to the average? Is this done with if statements?
My program is not adding up the numbers correctly as it is not reading the first entry right? Here is the output when I run the program:

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 <string>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;
void treefunction(ifstream&);//function prototype says that ifstream will be used in the function  

int main()
{
		ifstream treefile("tree.txt");//allows file to be opened 
	treefunction(treefile);//tells the program that the tree will be used and opened in this file.
		treefile.close();//closes treefile
	
	return 0;//return value at the end.

}
void treefunction(ifstream& treefile)//allows file to be opened, and used with ifstream&. The ifstream says it will be a treefile.
{


	if (!treefile)//if statement to check if the file has opened correctly
	{
		cout<<"Please try again, unable to open text file"<<endl;//output if file does not open correctly
		
	}
	int count=8;//counts numbers
	treefile>>count;
	double numbers[count];
	double sum=0;
	int b=0;
	int x=0;
	double average;
	
	for(int b=-2; b<count; b++)
	{
		x++;//want to count number of entries 
		treefile>>numbers[b];
		sum=sum+numbers[b];
		cout<<"The entries: "<<numbers[b]<<endl;
		
	}
	average=sum/x;
	
	cout<<"The number of entries is: "<<x<<endl;//displays data
	
	cout<<"The average of the data is: "<<average<<endl;
	for(int a=0; a<b;a++)
	{
		
	}
}
Last edited on
tree.txt input: 6.5 7.8 -1.4 9 22 -2.7 0 17

Is this the entire input file? Then one of your problems is in line 31.

Line 31 reads an int from your input file. So, it sees '6' and then '.'. The period is not part of an integer value, so line 31 stops reading. Now count is set to 6 and the input file stream has the following contents:

.5 7.8 -1.4 9 22 -2.7 0 17


The program will now read and average the following values: 0.5, 7.8, -1.4, 9, 22 and -2.7.

I do not understand problem 2.. this is my first time applying arrays in a program what does this mean, or how would this work?


Line 32 is not valid C++ because the compiler does not know how much memory to allocate to the array at compile time. Some compilers (yours apparently, and for that matter, the 1 I use, too) allow this construct as a convenience to coders, but you should avoid using it. Instead, an array must be declared using a constant integer value. Like this:

1
2
const int count = 8;
double numbers[count];  // memory statically allocated on the stack 


If you want the file to declare how many entries there are (like in line 31), you need to do something like this:

1
2
3
4
5
6
7
8
9
int count;
double* numbers;
treefile >> count;
numbers = new double[count];  // new dynamically allocates memory on the heap
                               // the [count] means it's an array

// do all you processing here
// when you are done, do...
delete[] numbers; // delete[] deallocates memory in an array 


When you learn about the standard template library, you can use the std::vector class.
Topic archived. No new replies allowed.