Passing Arrays By Reference

I am absolutely HORRIBLE at passing functions, and even more so with passing arrays. I'm trying to pass my "grades" array to the other functions, but I know I have to do it by reference since I can't return it. Can someone help shed some light?

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
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int readFile(int, int &count, int (&grades [10]));
int writeFile();
int averageGrades(int, int);

/**********************************************************************
* Reads an external file of grades and then returns those values to
* other functions in the program.
*********************************************************************/
int readFile(int sum, int &count, int (&grades [10]))
{
   //Declare variable
   char sourceFile[16];
   ifstream inStream;

   //Asking for user input
   cout << "Source file: ";
   cin >> sourceFile;

   //Open file
   inStream.open(sourceFile);
   if (inStream.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read from file and place in array
   for (int i = 0; i < 10 && inStream >> grades[i]; i++)
   {
      if (grades[i] == -1)
      {
         count++;
      }

      if (grades[i] != -1)
      {
         sum += grades[i];
      }
   }
   return sum;
}

/**********************************************************************
 * Reads an external file of grades and then returns those values to
 * other functions in the program.
 *********************************************************************/
int writeFile()
{
   ofstream outStream;
   char destinationFile[16];

   //Asking for user input
   cout << "Destination file: ";
   cin >> destinationFile;

   //Open file
   outStream.open(destinationFile);
   if (outStream.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }

   outStream << grades[i];

   return 0;
}


/**********************************************************************
* Finds the average of the ten grades from the previous function.
***********************************************************************/
int averageGrades(int sum, int count, int average)
{
   //The magic formula to find the average
   if (count == 10)
   {
      cout << "Average Grade: ---%" << endl;
   }

   else
   {
      average = (sum / (10 - count));
      cout << "Average Grade: " << average << "%" << endl;
   }

   return average;
}


/**********************************************************************
* Basically a delegator. Calls other functions to do its dirty work.
***********************************************************************/
int main()
{
   //Declaring Variables
   int average = 0;
   int sum = 0;
   int count = 0;
   int grades[10];

   //Calling other functions
   sum = readFile(sum, count);
   average = averageGrades(sum, count, average);

   return 0;
}
Arrays are by default passed by reference; you know that, right? Also you don't need to declare the size of the array your passing if and only if it's one-dimensional. :P

Aside from that, is there anything else you needed help with? (Are you sure you have your functions right at lines 108 and 109?)

-Albatross
What do you mean they're passed by default? Meaning, I don't have to pass them at all? How does that work then? Because when I to use "grades" in any other function, it gives me errors that say, "blah blah blah not declared in this scope."

I'm confused... This program seems a little over my head.
I didn't say they're passed by default, I said they're by default passed by reference. So, in short, change line 6 and get rid of the &. ;)

And are you sure you have line 108 right?

http://cplusplus.com/doc/tutorial/functions/
http://cplusplus.com/doc/tutorial/functions2/
^These might help.

-Albatross
Last edited on
Topic archived. No new replies allowed.