C++ 求数组中最大值

 Max value

//============================================= max
// From algorithms/arrayfuncs.cpp
// Returns the maximum value in an array.
float max(float a[], int size) {
    assert(size > 0);        // Note 1.
    float maxVal = a[0];     // Note 2.
    for (int i=1; i<size; i++) {
        if (a[i] > maxVal) {
            maxVal = a[i];
        }
    }
    return maxVal;
}//end max

Notes:

    Computing the maximum requires there is at least one value in the array. The "assert" enforces this requirement.
    The initial value for the maximum starts at the value of the first element instead of zero. Zero is commonly used, but doesn't work when all array elements are negative!

Max index
Another approach to the maximum is to return the index of the maximum value instead of the value. This is an advantage when the values are large or contain dynamically allocated values that makes assignment a non-trivial operation. Another reason to return the index is so that the value at that location in the array can be changed.


//============================================= maxIndex
// From algorithms/arrayfuncs.cpp
// Returns the index of the maximum value in an array.
int maxIndex(float a[], int size) {
    assert(size > 0);
    int maxIndex = 0;
    for (int i=1; i<size; i++) {
        if (a[i] > a[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;
}//end maxIndex

Extracted from algorithms/arrayfuncs.cpp

-------------------------------

-------------------------------

代码:#include <iostream>
using namespace std;
class Array_max
{
public:
 void set_value();
 void max_value();
 void show_value();
private:
 int array[10];
 int max;
};
void Array_max::set_value()
{
 int i;
 for(i=0;i<10;i++)
  cin>>array[i];
}
void Array_max::max_value()
{
 int i;
 max=array[0];
 for(i=1;i<10;i++)
  if(array[i]>max) max=array[i];
}
void Array_max::show_value()
{cout<<"max="<<max;}

int main()
{
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return 0;
}

----------------------------------------------------

---------------------------------------------------------

//C++ 指针的方式找出一维数组中的最大值和最小值
#include<iostream>
using namespace std;

void main()
{
 int c[]={1,4,0,2,5,3};
 int size_c=sizeof(c)/4;
 int max=*c;
 int min=*c;

 int i;
 int temp;
 for(i=1;i<size_c;++i)
 {
  temp=*(c+i);
  if(temp>max)
   max=temp;
  if(temp<min)
   min=temp;
 }
 cout<<"max="<<max<<endl
  <<"min="<<min<<endl;
}

/*
max=5
min=0
Press any key to continue
*/

 

### 回答1: 好的,关于数组中最大值,我可以为您提供帮助。 一种简单的方法是遍历数组中的每个元素,依次与当前的最大值进行比较,如果比当前最大值要大,就更新最大值。可以使用循环来实现这个过程。 以下是一个示例代码: ``` int findMax(int arr[], int n) { int max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } ``` 其中,arr是一个整数类型的数组,n表示数组的长度。函数返回数组中最大值。 希望这个代码可以帮助到您。 ### 回答2: 要找到一个数组中最大值,可以在程序中使用一个变量来保存当前的最大值,并进行比较。首先,假设数组中的第一个元素为最大值。然后,遍历整个数组,逐个比较每个元素是否大于当前最大值。如果找到一个更大的元素,就更新最大值为新的元素。最终遍历完成后,最大值变量中存储的就是整个数组中最大值。 以下是一个使用Python语言编写的示例代码: ```python def find_max(arr): max_value = arr[0] # 假设第一个元素为最大值 for i in range(1, len(arr)): if arr[i] > max_value: max_value = arr[i] return max_value # 示例 arr = [1, 3, 5, 2, 4] max_value = find_max(arr) print("数组中最大值为:", max_value) ``` 运行结果为: ``` 数组中最大值为: 5 ``` 这样就可以找到数组中最大值了。 ### 回答3: 数组中最大值,可以通过遍历数组的每个元素,与已存储的最大值进行比较,若当前元素大于最大值,则更新最大值。以下是具体步骤: 1. 声明一个变量max_value,用于存储最大值,默认值为数组的第一个元素,即max_value = arr[0]。 2. 遍历数组,从第二个元素开始,比较当前元素与max_value的大小关系。 3. 若当前元素大于max_value,则更新max_value的值,即max_value = 当前元素的值。 4. 继续遍历数组,重复上述步骤。 5. 当遍历完成后,max_value中存储的即为数组中最大值。 以下是示例代码: ```python def find_max(arr): max_value = arr[0] for i in range(1, len(arr)): if arr[i] > max_value: max_value = arr[i] return max_value # 测试代码 array = [1, 5, 3, 9, 2, 7] result = find_max(array) print("数组中最大值为:", result) ``` 输出结果为:数组中最大值为:9 通过遍历数组的每个元素,并与已知的最大值进行比较,可以解出数组中最大值
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值