查找数组位置的最大值
finding the maximum value of an array its location
#include <iostream>
using namespace std;
int main()
{
const int MAXNUM = 10; // this code creates 10 variables in fmax
int fmax[MAXNUM], maximum, i, l; // initialize fmax that contains 10 variables , maximum, i for the loop, and l for storing the location
cout << "enter 10 numbers: ";
maximum = fmax[0]; // this sets the maximum to 0
for(i = 0; i < MAXNUM; i++) // this is the code for finding the maximum numbers
{
cin >> fmax[i];
if(fmax[i] > maximum){
maximum = fmax[i];
l = i;
}
else{
maximum = maximum;
l = l;
}
}
cout << "the maximum number: " << maximum << endl; // outputs the results
cout << "the location of the number: " << l << endl;
return 0;
}
我在这个练习中遇到问题问题是程序的输出。 它不显示最大数量及其所在位置它总是这样显示
最大数量为:1987579782
号码的位置是:26355764
我需要显示输入的最大数量及其下标,我不知道如何我的代码有问题
这是运动问题
a.编写、编译并运行一个 C++ 程序,将 10 个整数输入到名为 fmax 的数组中,并确定输入的最大值 您的程序应仅包含一个循环,最大值应在输入数组元素值时确定(提示。 将最大值设置为等于第一个数组元素,该元素应在用于输入剩余数组值的循环之前输入。 并跟踪数组和最大值的索引号。
初始化 fmax[MAXNUM]
数组,然后再使用它。简单地声明一个变量并不能保证它将是 0。目前,fmax[0]
的值可以是int
范围内的任何值,因为您尚未将其初始化为值。
您还需要将l
变量初始化为 0(这就是您获得错误位置的原因(
试试这个:
#include <iostream>
using namespace std;
int main()
{
const int MAXNUM = 10; // this code creates 10 variables in fmax
int fmax[MAXNUM], maxinum, i, l = 0; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location
for(i = 0; i < MAXNUM; ++i)
{
fmax[i] = 0;
}
cout << "enter 10 numbers: ";
maxinum = fmax[0]; // this sets the maxinum to 0
for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
{
cin >> fmax[i];
if(fmax[i] > maxinum){
maxinum = fmax[i];
l = i;
}
else{
maxinum = maxinum;
l = l;
}
}
cout << "the maxinum number: " << maxinum << endl; // outputs the results
cout << "the location of the number: " << l << endl;
return 0;
}
编辑:由于要求代码只包含一个循环,因此您可能希望更改执行此操作的方式。我上面提供的示例也不是一种非常优雅的方法。这就是我的做法(不使用任何可能令人困惑的标准库函数C++(
#include <iostream>
int main()
{
std::cout << "Enter 10 numbers: ";
const int MAXNUM = 10;
int fmax[MAXNUM] = {0}; // this is an easy way to initialize the array elements to zero
int maxNum = 0, location;
for(int i = 0; i < MAXNUM; i++) {
std::cin >> fmax[i];
if(fmax[i] > maxNum) {
maxNum = fmax[i];
location = i;
}
}
std::cout << "The maximum number is " << maxNum << std::endl;
std::cout << "The location of the number is " << location << std::endl;
return 0;
}
确保您了解为什么这有效 - 如果您有任何问题,请告诉我。
您正在使用垃圾值初始化maximum
:
maxinum = fmax[0]; // this sets the maxinum to 0
由于您尚未输入任何元素。
我建议您使用库中的内置函数std::max_element
algorithm
:它将返回指向max元素的指针,因此,您可以输出max元素的位置及其值:
#include <algorithm>
// Your code
// You should enter the whole array
auto max_element = std::max_element(std::begin(fmax), std::end(fmax));
std::cout << "Position: " << (max_element - std::begin(fmax)) << std::end;
std::cout << "Value: " << *max_element << std::endl;
局部变量(非类(在 C++ 中默认不初始化为零。您的问题是您使用 fmax[0]
的值初始化maxinum
,这在开始时是垃圾。如果您从不输入任何更大的数字,则I
的值永远不会更改,也是垃圾。您需要将这些变量显式初始化为零:
int fmax[MAXNUM] = { 0 };
into maxinum = 0, I = 0
maxinum = fmax[0]; // this sets the maxinum to 0
这不会将maxinum
设置为 0,因为您尚未将fmax[0]
设置为 0
。
您可以这样做:
#include <iostream>
using namespace std;
int main()
{
const int MAXNUM = 10; // this code creates 10 variables in fmax
int fmax[MAXNUM], maxinum, i, l; // initialize fmax that contains 10 variables , maxinum, i for the loop, and l for storing the location
cout << "enter 10 numbers: ";
for(i = 0; i < MAXNUM; i++) // this is the code for finding the maxinum numbers
{
cin >> fmax[i];
if(i==0)
maxinum = fmax[i]; //....... this will do what you are trying to achieve
if(fmax[i] > maxinum){
maxinum = fmax[i];
l = i;
} // the else block you wrote is not necessary :)
}
cout << "the maxinum number: " << maxinum << endl; // outputs the results
cout << "the location of the number: " << l << endl;
return 0;
}
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 求最大元素位置的分治算法
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 如何在数组中交换最小和最大的位置?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- C++不同的最小最大值实现
- YCBCR 帧的最大值和最小值在 openCV 中的位置
- 如何检查以前是否C++找到过最大值位置
- C++ 在 2D std::vector 中查找最大值的位置
- 查找数组位置的最大值
- 求向量的奇数位置最大值(以C++为单位)