Trouble with arrays

I need to write a program that reads a file, then puts the information into an array. It must then calculate the average, standard deviation, and the min and max. I am having trouble with the min/max function. I keep getting errors. My teacher told me I need to use a void function but the compiler doesn't like that. Everything I've tried wont seem to work. Any help is certainly appreciated. Here's what I have so far.

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

const int MAX_ARRAY = 500;

void getData(ifstream& inFile, int inValues[], int& size); 
double getAverage(int inValues[], int dataSize); 
void compute_max_min [theData, numElems, max, min];

int main() 
{ 
    ifstream fileIn;

    int max, min;
    int theData[MAX_ARRAY];     // Array for data storage 
    int numElems;               // Actual number of values in array  
                                // (1 more than largest index) 

    fileIn.open("pgm5data.txt"); // Open file 
     
    if (fileIn.fail() )         // Test for file existence 
    { 
      cout <<  "Problem opening file"; 
      exit(-1); 
    } 
     
    // Read file and count values in array 
    getData(fileIn,theData,numElems); 
     
   // Determine and write average; test first for non-empty data file    
    if (numElems > 0) 
       cout << "Average: " << getAverage(theData,numElems) << endl << endl; 
     else 
       cout << "ERROR:  No data processed" << endl << endl; 

	compute_max_min[theData, numElems, max, min];

    // Close file 
    fileIn.close();     
     
    return 0;                 
} 

// This function reads integers from a file and stores the values in 
// an array.  It returns the loaded array and the number of elements  
// in the array 
void getData(ifstream& inFile, int inValues[], int& numVals) 
{ 
    int i = 0; 
    inFile >> inValues[i];                 
    while (!inFile.eof() && i < MAX_ARRAY)    // Test for end of file and array     
    { 
       i++;         
       inFile >> inValues[i]; 
    }             
    numVals = i; 
} 

// This function receives an array of integers, calculates the average 
// of the array values, and returns it. 
double getAverage(int inValues[], int dataSize) 
{ 
    double sum = 0.0; 
     
    for (int i = 0; i < dataSize; i++) 
       sum = sum + inValues[i];     
        
    return sum / dataSize; 
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void compute_max_min (int theData[], int& dataSize, int& iCurrMin, int& iCurrMax)
{
    int iCurrMax = 0;
    for (int i = 1; i < dataSize; ++i) {
        if (theData[iCurrMax] > theData[i])
            iCurrMax = i;


    int iCurrMin = 0;
    for (int i = 1; i < dataSize; ++i) {
        if (theData[iCurrMin] < theData[i])
            iCurrMin = i;


}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

double computer_stdDev (int theData[], int dataSize);
{


}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void print_quartile ();
Your prototype :
void compute_max_min [theData, numElems, max, min];

should be :

void compute_max_min (int theData[], int& dataSize, int& iCurrMin, int& iCurrMax);

the function prototype should match the function definition
Ok I fixed the prototype but now when I try to call it in main, I get an error saying expression must be a pointer to a complete object type. Here's what I have where I try to call it.
cout << "min" << compute_max_min[theData, numElems, max, min];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void compute_max_min (int theData[], int& dataSize, int& iCurrMin, int& iCurrMax)
{
    int iCurrMax = 0;
    for (int i = 1; i < dataSize; ++i) { //Why this parenthesis?
        if (theData[iCurrMax] > theData[i])
            iCurrMax = i;


    int iCurrMin = 0;
    for (int i = 1; i < dataSize; ++i) { //?? Where is the closing one?
        if (theData[iCurrMin] < theData[i])
            iCurrMin = i;


}


Also, why not just open/close the file on the function that you used to read data from it? Making it hairier than it has to be.

AND as to your updated problem, that is because you are using brackets instead of parenthesis. You had brackets in your prototype and you have brackets when you are calling the function. Why?

And why is there a semicolon in line 93 and 101 (rather, why is the 101 prototype way at the bottom)?

Also you are using iCurrMin and iCurrMax for two variables. Name your int variables in the function differently from those that you sent there through reference.
Last edited on
Topic archived. No new replies allowed.