hi there,
i need to create a function that reads a series of numbers from a file and returns to the main function the mean of those numbers. I am having trouble with the syntax of array parameters and arguments (i don't know how to call the function if it is an array). I need some help with my syntax. Here is my code so far, very unfinished. Can someone point me into the right direction? I know i have to use a loop statement in there somewhere but i don't know which one.
Help would be great

#include<iostream>
using namespace std;
#include <fstream>
double meanArray(double N1[],double average);
int main()
{
	double average
	double fileNumbers[]

	system("TITLE Third Project");
	system("COLOR 47");
	
	mymean = meanArray(fileNumbers, average)

	cout<<"\n";
	
	return 0;


}

double meanArray(double N1[],double average)
{
	ifstream infile;
	infile.open("DataFile.txt");

Recommended Answers

All 4 Replies

Hi there,

If you would past the sample source (DataFile.txt) file it would be better.

it is basically a file containing a list of numbers like this:
12
45
34
65
75
34
62
19
5
etc.

but my professor did not specify how many there were, all the numbers are integers though

I am not sure if I am using the right method:

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <numeric>

using namespace std;


int main(int argc, char *argv[])
{
int numbers[50];
float mean;
string FilePath("C:\\Test.txt");
ifstream inFile(FilePath.c_str());
int index = 0;

while(!inFile.eof())
{
	string line;
	string tempStr;
	getline(inFile, line);
	for(int i = 0; i <= line.size(); i++)
	{
		tempStr += line[i];
	}
	numbers[index] = atoi(tempStr.c_str());
	cout << numbers[index] << "\n";
	index += 1;
}

mean = accumulate(numbers, numbers+index, 0) / (index);
cout << "\nMean: " << mean;
getchar();
return 0;
}

@OP here is some syntax help:

#include<iostream>
using namespace std;
#include <fstream>

//returns the mean of the array
double meanArray(double N1[], int size);

int main()
{
	const int MAX_DATA = 1000;
	double fileData[MAX_DATA ] = {0}; //initialize the array to contain 0
	
	ifstream fileInput("data.txt"); //open the file
        /* read data from fileInput into fileData */

	double meanValue = meanArray(fileData,MAX_DATA);

	cout<<"The meanValue = " << meanValue << "\n";
	
	return 0;


}

double meanArray(double N1[],double average){
  /* calculate the mean value from the array N1 */
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.