並列配列の分割について質問があります。私はC ++にかなり慣れていません。私のプログラムでは、並列配列 (atBats[] と hits[]) を分割し、結果を空の配列 (batAvg[]) に格納しています。分割すると、他の 2 つの配列が正しいデータを保持しているにもかかわらず、新しい配列は空白のままになります。新しいデータを格納するために、batAvg 配列が更新されない理由を知りたいだけです。
int main() {
//declare variables and arrays
const int SIZE = 20;
int playerNum[SIZE],
atBats[SIZE],
hits[SIZE],
runs[SIZE],
rbis[SIZE];
double batAvg[SIZE];
int numberPlayers;
//Load number of players from the loadArrays method
numberPlayers = loadArrays(playerNum, atBats, hits, runs, rbis);
batAverage(atBats, hits, batAvg, numberPlayers);
system("pause");
return 0;
}//end main
int loadArrays(int playerNum[], int atBats[], int hits[], int runs[], int rbis[]) {
//Define variables
int i = 0;
//Open file and read arrays
ifstream inputFile;
inputFile.open("BaseballStats.txt");
//Let user know if the file fails to open
if (inputFile.fail())
cout << "There was an error opening the file.\n";
//Load the arrays from the file and increment count each loop
while (inputFile >> playerNum[i]) {
inputFile >> atBats[i];
inputFile >> hits[i];
inputFile >> runs[i];
inputFile >> rbis[i];
i++;
}//end while loop
//Close file and return count as reference to the number of players
inputFile.close();
return i;
}//end loadArrays method
ここまでは問題ありませんが、batAverage 関数は、batAvg 配列がデータを正しく格納していない場所です。配列は、694、417、389、488 などの数値を格納する必要がありますが、すべてゼロを読み取ります。
void batAverage(int atBats[], int hits[], double batAvg[], int numberPlayers) {
for (int i = 0; i < numberPlayers; i++) {
batAvg[i] = (hits[i] / atBats[i]) * 1000;
}//end for loop
}//end batAverage method
これは、プログラムに読み込んでいるファイルからのデータです。
10 36 25 2 5
2 12 5 0 1
34 18 7 1 0
63 41 20 4 2
12 10 3 1 0
14 2 1 1 1
27 55 27 10 8
8 27 12 3 4
42 32 8 2 1
33 19 4 1 0