Blog Archive

Wednesday, February 1, 2017

Passing Arrays By Reference (debug)

Passing Arrays By Reference - C++ Forum:


Question:
One example about passing arrays by reference in c++ was provided in:
http://www.cplusplus.com/forum/beginner/30698/
Unfortunately, it is buggy. How can you solve it?

Answer:

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

int readFile(int, int &count, int (&grades) [10]);
int writeFile(int (&grades) [10]);
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(int (&grades) [10])
{
   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);
   }
   for(auto i: grades)
       outStream << i<<endl;

   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, grades);
   average = averageGrades(sum, count, average);
   writeFile(grades);
   return 0;
}



No comments:

Post a Comment