processing an array from a file

I am working on a project that has me taking information out of an array and finding the average and smallest array. I then need to print all the arrays, the min, and the average onto a separate file. I can get the program to read the file and I can get it to print, but I am getting this '5844ED48' before every number and the average and min comeback as 0. Am i missing something?

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
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;

void GetData(ifstream&, int [], int&);
void FindAverage (int [], int, int, int);
void FindSmallest (int [], int, int, int);
void PrintArray (int [], int, int, int, ofstream&);
int index[100];
int i;
int sum;
int smallestNum;
int minIndex;
int average;
ifstream inData;
ofstream outData;

void GetData (ifstream& inData, int index[ ], int& i)
{
     i = 0;
     inData >> index[i];
     while (inData && i < 13)
     {
           i++;
           inData >> index[i];
     }
}

void FindSmallest (int index[], int minIndex, int i, int smallestNum)
{
	minIndex = 0;
	for (i = 1; i < 13; i++)
      if (index[minIndex] > index[i])
            minIndex = i;
		smallestNum = index[minIndex] ; 

}

void FindAverage (int index[], int sum, int i, int average)
{
	sum = 0;
	for (i = 0; i < 13; i++)
		sum = sum + index[i] ;

	average = sum / 13;

}

void PrintArray (int index[], int sum, int i, int average, ofstream& outData)
{
	outData << cout << "The array contains these numbers ";
			
	for (i = 0; i < 13; i++)
	{
		outData << cout << index[i] << " ";
		cout<< endl;
	}
	outData << cout << '\n';
	outData << cout << "The smallest array is " << minIndex << '\n';
	outData << cout << "The average size is " << average;
}

int main()
{

	inData.open("C:\\Users\\CaseySmith\\Documents\\Visual Studio 2010\\Projects\\Lab9 - B\\data1.txt");
	outData.open("C:\\Users\\CaseySmith\\Documents\\Visual Studio 2010\\Projects\\Lab9 - B\\data1_output.txt");

	GetData (inData, index, i);
	FindAverage (index, sum, i, average);
	FindSmallest (index, minIndex, i, smallestNum);
	PrintArray (index, sum, i, average, outData);

	inData.close();
	outData.close();

    
    return 0;
}


Here are the numbers in the file

2000
1113
6244
240
864
23450
1258
3579
2462
9713
1596
2552
7005



Here is the output

5844ED48The array contains these numbers 5844ED482000 5844ED481113 5844ED486244 5844ED48240 5844ED48864 5844ED4823450 5844ED481258 5844ED483579 5844ED482462 5844ED489713 5844ED481596 5844ED482552 5844ED487005 5844ED48
5844ED48The smallest array is 0
5844ED48The average size is 0


Any ideas on my next step?
Hi,

firstly your print to file problem is that you dont need to put cout in the << commands printArray
works when done as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
void PrintArray (int index[], int sum, int i, int average, ofstream& outData)
{
	outData << "The array contains these numbers \n";
			
	for (i = 0; i < 13; i++)
	{
		outData << index[i] << " ";		
		outData << endl;
	}
	outData <<  '\n';
	outData <<  "The smallest array is " << minIndex << '\n';
	outData <<  "The average size is " << average;
}


Your second problem which is the smallest and average is caused by using variables in
your definition

eg
 
void FindAverage (int index[], int sum, int i, int average)


instead of
 
void FindAverage (int index[])


in the first version sum, i and index are local variables which are initialized from the calling
function, but modifying them will not change the variables that they are initialized from.

You have also declared sum,i & average as global variables so you shouldnt need
to pass them in your function call.

If you wish to change the original variable you would do something like this.

1
2
3
4
5
void findAverage(int index[],int* sum)

//and then dereference in your function

*sum = *sum + 1;


Hope this was helpful
Shredded
Thats perfect! Thank you so much for your help. Everything is working perfectly now.
Topic archived. No new replies allowed.