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;
}
Blog Archive
-
▼
2017
(115)
-
▼
February
(16)
- 杭州Rokid团队是什么? - 机器人 - 知乎
- 客中守岁(在柳家庄) -- 唐代春运
- Depth First Traversal or DFS for a Graph
- Resident Alien Claiming a Treaty Exemption for a S...
- 澜声科技有限公司
- 广告配音_广告配音服务_合成配音-配音阁,国内最专业的广告配音平台
- TensorFlow极速入门 | 雷锋网
- (2) What is the best text to speech software? - Quora
- 微信摇一摇电视的技术原理是什么?有没有相类似的APP? - 支付宝 - 知乎
- Kaldi的I/O机制 | Chinese Doc of Kaldi
- algorithms/theory.md at master · skseth/algorithms
- How can I become an expert at C++ quickly? - progr...
- As a programmer, what things impress on a resume?
- Google C++ Style Guide
- 谷歌云官方:一小时掌握深度学习和TensorFlow(视频+50PPT) | 新智元 | 数据观 |...
- Passing Arrays By Reference (debug)
-
▼
February
(16)
No comments:
Post a Comment