2 Replies - 261 Views - Last Post: 28 October 2010 - 03:33 PM Rate Topic: -----

#1 brycematheson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 19-October 10

Passings Arrays By Reference

Posted 28 October 2010 - 03:22 PM

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?

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Passings Arrays By Reference

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,142
  • Joined: 08-August 08

Re: Passings Arrays By Reference

Posted 28 October 2010 - 03:31 PM

Arrays are passed by reference by default.

void myfunction( int grades[])
{
   // changes to grades here will be made on the array passed to this function.
}


Your sum is an integer (and not summed up!), and your average is also an integer! Think this through one step at a time or you're going to have more trouble than passing an array.
Was This Post Helpful? 0
  • +
  • -

#3 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Passings Arrays By Reference

Posted 28 October 2010 - 03:33 PM

Read this and bookmark it for future reference
http://www.cplusplus...utorial/arrays/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1