data txt file, display and void

Am totally lost, please help?? very frustrated

I/O
1
2
3
4
5
This is array a: 44 33 19 9 70 11
Average: 31.00
Data values above average: 44 33 70
Maximum: 70
Minimum: 9


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
#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

const int N = 6;

void CopyData(string [], int n);
void DisplayArray(int a);
float FindAve(int x[], int N);
void Display(int a, int Ave);
void FindMaxMin(int a, int N, int&max, int&min);


int main()
{
	int a[N];
	fstream f;

	//Copy data from file into array a
	CopyData("data.txt", a);
	
	//Display array a 
	DisplayArray(a);

	//Find and display average of numbers in array a
	float Ave = FindAve(a, N);
	cout << "Average = " << Ave << endl;

	//Display numbers whose value is >=average
	Display(a, Ave); 

	//Find and display the maximum and minimum numbers in array a
	int max, min;
	FindMaxMin(a, N, max, min);
	Display(max, min);

	//terminate program
	return 0;
}


void CopyData(int a[]) 
{
	for (int i = 0; i < N; i++)
	{
		cin >> a[i];
	}
}

void DisplayArray(int a[], int n)
{
	for (int i = 0; i < N; i++)
	{
		std:: cout << "This is array: " << a[i];
	}


}

void Display(a, int&max, int&min){
	cout << " Maximum = " << FindMax(a, N) << endl;
	cout << " Minimum = " << FindMin(a, N) << endl;
}


int FindMaxMin(const int a[], int n)
{
	int max = a[0], min = a[0];

	for (int i = 1; i < n; i++)
	{
		if (a[i] > max) max = a[i];
		if (a[i] < min) min = a[i];
	}
}
Last edited on
There are several issues in this code, for starters:

CopyData is declared as:

void CopyData ( string [], int)

on main() you call:

void CopyData (string, int [])

And on the function definition it is:

void CopyData (int [])

To the compiler, those three are three different functions, so your code probably does not build.

You are trying to read things from a file, correct? Why is the function name "CopyData" to begin with?


Also, you declared a const int N on the begining of the code, and you interchangeably define another variable called n, type int, on some functions you parse an integer, but on the code you use the global const int N on loops, for example:

1
2
3
4
5
6
7
void DisplayArray(int a[], int n)
{
	for (int i = 0; i < N; i++)
	{
		std:: cout << "This is array: " << a[i];
	}
}


You also have the same type/arguments problems on the functions Display and FindMaxMin.

The function FindAve is never defined.

You are using the variables max and min with no initialization. Etc.


So solve all those issues first, after you code compile, then send over the code again if there are still problems. If you absolutely can not get it to compile, or if you dont understand what I said, you will need to step back and wrap your head better around variables, types and functions first, perhaps with a more simple task. GL!

Topic archived. No new replies allowed.