C++阵列程序的协助

C++ assistance with array program

本文关键字:程序 阵列 C++      更新时间:2023-10-16

程序正在运行,但是当打印结果时,我不断得到0和1,而不是我应该得到的最高,最低,总和和平均数字的实际数字。我知道我的整体代码一定有问题,但我不确定它是什么。 这是该程序附带的文本文件之一:
-53-22-87-103-3



#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
// Function prototypes
int getHighest(const int numbers[],int ARRAY_SIZE);
int getLowest(const int numbers[],int ARRAY_SIZE);
int getSum(const int numbers[],int ARRAY_SIZE);
int getAverage(const int numbers[],int ARRAY_SIZE);
int main () {
// Variables
int numbers [ARRAY_SIZE]; 
int count = 0; // loop counter variable
string filename;

//  Open file
cout << "Enter input filename:";
cin >> filename;
ifstream inputFile(filename);   // input file stream object
// Read numbers from file into array
while(count <ARRAY_SIZE && inputFile >> numbers[count])
count ++;
// Print results
cout<<ARRAY_SIZE<<" numbers read from input file."<<endl;
cout<<"The highest value is: "<<getHighest(numbers,ARRAY_SIZE)<<endl;
cout<<"The lowest value is: "<<getLowest(numbers,ARRAY_SIZE)<<endl;
cout<<"The sum of the numbers is: "<<getSum(numbers,ARRAY_SIZE)<<endl;
cout<<"The average of the numbers is: "<<getAverage(numbers,ARRAY_SIZE)<<endl;
}
int getHighest( const int numbers[], int ARRAY_SIZE)
{
int highest;
highest = numbers[0];
for(int count = 1 ; count < ARRAY_SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
return highest;
}
int getLowest(const int numbers[], int ARRAY_SIZE)
{
int lowest;
lowest = numbers[0];
for (int count = 1; count < ARRAY_SIZE; count++)
{
if (numbers[count] < lowest)
lowest = numbers[count];
}
return lowest;
}
int getSum(const int numbers[], int ARRAY_SIZE)
{
int sum = 0;
for(int count = 0; count < ARRAY_SIZE; count++)
sum+= numbers[count];
return sum;
}
int getAverage(const int numbers[], int ARRAY_SIZE)
{
return getSum(numbers, ARRAY_SIZE) / ARRAY_SIZE;
}

所以主要错误是你在应该使用count的时候使用了ARRAY_SIZEARRAY_SIZE是数组的大小,但您读取的数字数是count

cout<<ARRAY_SIZE<<" numbers read from input file."<<endl;
cout<<"The highest value is: "<<getHighest(numbers,ARRAY_SIZE)<<endl;

应该是

cout<<count<<" numbers read from input file."<<endl;
cout<<"The highest value is: "<<getHighest(numbers,count)<<endl;

等等。

而且,虽然没有错,但重命名函数中的变量肯定会有所帮助,因此请更改此设置

int getHighest(const int numbers[], int ARRAY_SIZE)
{
int highest;
highest = numbers[0];
for (int count = 1 ; count < ARRAY_SIZE; count++)
{
if (numbers[count] > highest)
highest = numbers[count];
}
return highest;
}

对此

int getHighest(const int numbers[], int count)
{
int highest;
highest = numbers[0];
for (int i = 1 ; i < count; i++)
{
if (numbers[i] > highest)
highest = numbers[i];
}
return highest;
}

我不确定您如何定义ARRAY_SIZE但是如果我假设您在某个时候将其设置为=文件中行数的常量,那么我能够让它与g ++一起使用。通过将字符串更改为 C 字符串,我能够让 ifstream 读取文件。

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
// Function prototypes
int getHighest(const int numbers[],int ARRAY_SIZE);
int getLowest(const int numbers[],int ARRAY_SIZE);
int getSum(const int numbers[],int ARRAY_SIZE);
int getAverage(const int numbers[],int ARRAY_SIZE);
int main () {
//changed
const int ARRAY_SIZE = 5;
// Variables
int numbers [ARRAY_SIZE];
int count = 0; // loop counter variable
string filename;

//  Open file
cout << "Enter input filename:";
cin >> filename;
// changed
ifstream inputFile (filename.c_str());   // input file stream object
// Read numbers from file into array
while(count <ARRAY_SIZE && inputFile >> numbers[count])
count ++;
// Print results
cout<<ARRAY_SIZE<<" numbers read from input file."<<endl;
cout<<"The highest value is: "<<getHighest(numbers,ARRAY_SIZE)<<endl;
cout<<"The lowest value is: "<<getLowest(numbers,ARRAY_SIZE)<<endl;
cout<<"The sum of the numbers is: "<<getSum(numbers,ARRAY_SIZE)<<endl;
cout<<"The average of the numbers is: "<<getAverage(numbers,ARRAY_SIZE)<<endl;
}

这是输出:

5 numbers read from input file.
The highest value is: -3
The lowest value is: -103
The sum of the numbers is: -268
The average of the numbers is: -53