对于上课,我必须编写一个计算12个不同等级"的程序。我使用了2个不同的数组 - "分子[]"和"分母[]"每个都有12个(12个不同的整数)。对于这个赋值,我必须使分母0的值为3,这表明这些分母的相应分子等于"奖励积分" (例如5/0级将等于5个奖励积分)。但是,现在我必须创建一个"数据清理"函数,它使用非零分母获取所有分数,并将它们移动到一个新的空数组(只留下"真实"分数,我作为newNumerator []和newDenominator [],以及每个数组的新大小" newSize"。数据清理功能需要具有这些参数 -
dataClean(int numerator[], int denominator[], int size, int newNumerator[], int newDenominator[], int &newSize)
我需要填充新数组并在cleanData函数中定义那些数组的newSize,并让它们的新值通过参数返回到main()。但是,我已经坚持了很长一段时间。我不确定是否需要使用指针,即使如此,我也不清楚如何使用它们。很抱歉,如果不清楚,这项任务对我来说非常困惑。任何帮助表示赞赏。如果您需要任何进一步的澄清,请告诉我。
分子和分母的数据文件(data.txt)
72 49 23 5 9 10 6 16 26 54 4 55//numerator[]
75 50 25 0 10 0 0 20 30 55 5 60//denominator[]
程序 -
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
typedef int arrayType[];
void readData(int numerator[], int denominator[], int size);
void reportData(arrayType, arrayType, int);
void reportOverall(int numerator[], int denominator[], int size);
void cleanData(int numerator[], int denominator[], int size, int newNumerator[], int newDenominator[], int &newSize);
int main()
{
const int size = 12;
int numerator[size];
int denominator[size];
int newSize = 12;
int newNumerator[newSize];
int newDenominator[newSize];
cout << fixed << showpoint << setprecision(1);
readData(numerator, denominator, size);
reportData(numerator, denominator, size);
reportOverall(numerator, denominator, size);
cout << "\n******************************\n";
cleanData(numerator, denominator, size, newNumerator, newDenominator, &newSize);
reportData(newNumerator, newDenominator, newSize);
system("pause");
return 0;
}
void readData(int numerator[], int denominator[], int size)
{
ifstream dataIn;
dataIn.open("data.txt");
if(!dataIn)
{
cout << "File not found\n";
system("pause");
exit(1);
}
int count;
for(count = 0; count < size; count++)
{
dataIn >> numerator[count];
}
for (count = 0; count < size; count++)
{
dataIn >> denominator[count];
}
dataIn.close();
}
void reportData(arrayType numerator, arrayType denominator, int size)
{
int count;
for (count = 0; count < size; count++)
{
if (denominator[count] == 0)
{
cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << "Bonus points!\n";
}
else
{
double percent = 100.0 * static_cast<double>(numerator[count]) / denominator[count];
cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << (percent) << "%\n";
}
}
}
void reportOverall(int numerator[], int denominator[], int size)
{
int count;
int totalNumerator = 0.0;
int totalDenominator = 0.0;
for (count = 0; count < size; count++)
{
totalNumerator += numerator[count];
}
for (count = 0; count < size; count++)
{
totalDenominator += denominator[count];
}
double overallPercent = 100.0 * static_cast<double>(totalNumerator) / (totalDenominator);
cout << "Total Points Earned (numerators): " << totalNumerator << endl;
cout << "Total Points Possible (denominators): " << totalDenominator << endl;
cout << "Overall Grade: " << overallPercent << "%\n";
}
void cleanData(int numerator[], int denominator[], int size, int *newNumerator[], int *newDenominator[], int &newSize)
{
int count;
for(count = 0; count < size; count++)
{
int count2 = 0;
if(denominator[count] != 0)
{
*newNumerator[count2] = numerator[count];
*newDenominator[count2] = denominator[count];
newSize--;
count2++;
}
}
}
答案 0 :(得分:0)
你需要通过适当的参数引用来修复函数调用和cleanData定义
within main:
cleanData(numerator, denominator, size, newNumerator, newDenominator, newSize);
void cleanData(int* numerator, int* denominator, const int size, int *newNumerator, int *newDenominator, int newSize)
{
int count;
for(count = 0; count < size; count++)
{
int count2 = 0;
if(denominator[count] != 0)
{
newNumerator[count2] = numerator[count];
newDenominator[count2] = denominator[count];
newSize--;
count2++;
}
}
}