I guess what i need to figure out is how to do this program. but i need to make the functions call by address. any help would be great.

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

int findLarge(int x[])
{
	int i = 0;
	int largest = -99999;
	while (i < 9)
	{
		if (largest < x[i])
			largest = x[i];
		i=i + 1;
	}
	return largest;

}



double calAverage(int x[])
{
    int i = 0;
	double avg = 0.0;
	while (i<9)
	{
		avg += x[i]; 
		i++;
	}
	return avg/9;
}


int main()
{
	int a[9], i=0, total=0, average=0, lgst=0;
	ifstream file1;
	ofstream file2;
	file1.open("quiz5data.txt", ios::in);
	file2.open("quiz5output.txt");
	if(file1.is_open()==true)
		for(i=0;i<9;i++)
		file1 >> a[i];

	findLarge(a);
	calAverage(a);
	
	
	file2<< "Largest = " << findLarge(a) << endl;
	cout << "Largest = " << findLarge(a) << endl;
	
	cout << "Avg = " << calAverage(a) << endl;
	file2<< "Avg = " << calAverage(a) << endl;
	
	return 0;
}

Recommended Answers

All 3 Replies

The program looks fine to me already. What's wrong with it?

make the functions call by address

I don't know what you mean by this.

Whenever you pass an array, it is just call by reference or call by address..

Right direction but lots of defects in OP code.
The OP title does not bear a relation to the real OP problems.
Think about:

// Never fix array size in a function!
int getLargest(const int* a, int n)
{
  int largest = 0;
  if (a && n > 0) {
      int largest = a[0];
      for (int i = 1; i < n; i++)
        if (largest < a[i])
            largest = a[i];
  }
  return largest;
}
// The same notes as above +
// integer division corrected.
double getAverage(const int* a, int n)
{
  double avg = 0.0;
  if (a && n > 0) {
      for (int i = 0; i < n; i++)
          avg += a[i];
      avg = (double)avg/n;
  }
  return avg;
}
...
// Pass array size to the functions
double average = getAverage(a,9);
int lgst    = getLargest(a,9);
// That's all, print these vars...

Yet another tip: instead of cumbersome

ifstream file1; // INPUT stream, why ios::in later?
...
file1.open("...",ios::in);
...
if (file1.is_open() == true) // why == true?

keep it simple:

ifstream file1("...");
// always test operator >> result:
for (i = 0; i < 9 && (file1 >> a[i]); i++)
    {}
if (i < 9) { // open failed or bad input
    ...    
}
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.