C++阵列程序的协助
C++ assistance with array program
程序正在运行,但是当打印结果时,我不断得到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_SIZE
。ARRAY_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
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++阵列程序的协助
- 简单的程序比较阵列运行但给出不正确的结果
- C 程序仅在字符阵列获得“ 1000!”的输入时才在DELETE上崩溃
- 程序停止 - 将阵列移动到函数 |C++
- 阵列不能两次将相同的名称保存,当要添加新字符串时,程序应检查以查看该名称是否已经存在
- opengl驱动程序在有限的VRAM中处理大型纹理阵列的效果如何
- 其他静态阵列破坏了OMAPL138目标(CCS5.2)上的DSP应用程序
- 顶点阵列GLFW崩溃C 程序
- 阵列相邻位置交换程序需要提示
- 动态内存阵列/程序崩溃
- 从队列中显示2D阵列后程序崩溃
- 阵列程序奇怪的输出
- 最大子阵列程序实现未终止
- C++中的典型阵列程序
- C++阵列程序
- 使用交换方法x86 MASM反转程序集中的外部阵列
- 阵列分割错误在斐波那契数程序