数组最大值和下标的C++代码如下:
#include <iostream> int main() { int arr[] = {4, 2, 9, 7, 5}; int n = sizeof(arr) / sizeof(arr[0]); &今天新澳门开奖结果查询表下载最新版nbsp; int maxVal = arr[0]; int maxIndex = 0; for (int i = 1; i < n; ++i) { if (arr[i] > maxVal) { maxVal = arr[i]; maxIndex = i; } } std::cout << "最大值: " << maxVal << std::endl; std::cout << "最大值的下标: " << maxIndex << std::endl; return 0; }
这段代码首先定义了一个整数数组,然后通过循环找到数组中的最大值以及对应的下标,并输出它们。