C++:输出问题

C++: Issue with output

本文关键字:出问题 输出 C++      更新时间:2023-10-16

我有一个C++程序的以下代码,设计用于执行以下操作:开发并运行一个程序,该程序有两个函数,分别在数组中查找最大值(称为findLargest)和在数组中寻找最小值(称之为findSmallest)。代码编译没有问题,但输出没有正确显示,我得到了一系列重复的数字,上面写着"-9.25596e+61"。如果有人能帮我解决这个问题,我将不胜感激。非常感谢。

#include <iostream>   
using namespace std;
double findLargest(const double LIST[], int);
double findSmallest(const double LIST[], int);
void printArray(const double LIST[], int);
int main()
{
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	cout.precision(2);
	const int MAX = 10;
	double list[MAX] = { 32, 54, 67.5, 29, -34.5, 80, 115, 44.5, 100, 65 };   // elements for the array
	printArray(list, MAX);
	cout << "nnThe largest number is: " << findLargest(list, MAX);
	cout << "nnThe smallest number is: " << findSmallest(list, MAX) << "nn";
	system("pause");
	return 0;
}
double findLargest(const double LIST[], int size)                //function to evaluate the largest elements and output it out
{
	double largest = LIST[0];
	for (int a = 0; a < size; a++)
	{
		if (LIST[a] > largest)
		{
			largest = LIST[a];
		}
	}
	return largest;
}
double findSmallest(const double LIST[], int size)     // function to evaluate the smallest and output it out
{
	double smallest = LIST[0];
	for (int a = 0; a < size; a++)
	{
		if (LIST[a] < smallest)
		{
			smallest = LIST[a];
		}
	}
	return smallest;
}
void printArray(const double LIST[], int size)                   // function to print array
{
	cout << "nnThe size of the element is: " << size << "nn";
	for (int a = 0; a < size; a++)
	{
		cout << setw(9) << LIST[a];
		if ((a + 1) % 8 == 0)
			cout << endl;
	}
}

据我所见,您的问题是需要包含以下文件:

<iomanip>

添加行:

#include <iomanip> 

到代码的开头,看看这是否有帮助。