js 数组找出最大值和最小值的下标
时间: 2023-12-30 21:03:53 浏览: 48
可以使用循环遍历整个数组,记录最大值和最小值以及它们对应的下标,代码如下:
```javascript
function findMaxAndMinIndex(arr) {
let maxIndex = 0;
let minIndex = 0;
for(let i = 1; i < arr.length; i++) {
if(arr[i] > arr[maxIndex]) {
maxIndex = i;
}
if(arr[i] < arr[minIndex]) {
minIndex = i;
}
}
return {
maxIndex: maxIndex,
minIndex: minIndex
};
}
// 示例
const arr = [1, 5, 3, 9, 2, 8];
const result = findMaxAndMinIndex(arr);
console.log(result.maxIndex); // 输出: 3
console.log(result.minIndex); // 输出: 0
```
上述代码中,我们使用 `maxIndex` 和 `minIndex` 分别记录最大值和最小值在数组中的下标。在循环中,如果找到更大的数,则更新 `maxIndex`,如果找到更小的数,则更新 `minIndex`。最后,返回一个包含 `maxIndex` 和 `minIndex` 的对象。
相关问题
C语言实现找出数组中的最大值和最小值
好的,我可以帮您解决这个问题。以下是一个 C 语言程序,可以找出数组中的最大值和最小值:
```c
#include <stdio.h>
int main() {
int arr[] = {1, 5, 3, 8, 2};
int n = sizeof(arr) / sizeof(arr[0]);
int max = arr[0];
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Max is %d\n", max);
printf("Min is %d\n", min);
return 0;
}
```
这个程序定义了一个整数数组 `arr`,并计算数组的长度 `n`。然后,通过遍历数组找到数组中的最大值和最小值,分别存储在变量 `max` 和 `min` 中。最后,输出找到的最大值和最小值。
遍历数组,找最大值和最小值
您可以按照以下步骤在C语言中遍历数组,找到其中的最大值和最小值:
1. 声明一个整型数组,可以是int类型,并定义数组长度为n:
```c
int arr[n];
```
2. 使用循环语句,如for循环,让用户输入n个数字,然后将它们存入数组中:
```c
for (int i = 0; i < n; i++) {
printf("请输入第%d个数字:", i+1);
scanf("%d", &arr[i]);
}
```
上述代码中,循环变量i从0开始,每次循环让用户输入一个数字,并将其存入数组中。
3. 声明两个变量max和min,分别存储数组中的最大值和最小值,初始化为数组的第一个元素:
```c
int max = arr[0];
int min = arr[0];
```
4. 使用循环语句遍历数组,如果当前元素比max大,则更新max;如果当前元素比min小,则更新min:
```c
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
```
上述代码中,循环从1开始,因为max和min已经初始化为数组的第一个元素了。
5. 最终,您可以输出数组的最大值和最小值:
```c
printf("数组的最大值为:%d\n", max);
printf("数组的最小值为:%d\n", min);
```
完整代码示例:
```c
#include <stdio.h>
int main() {
int n;
printf("请输入数组的长度:");
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
printf("请输入第%d个数字:", i+1);
scanf("%d", &arr[i]);
}
int max = arr[0];
int min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("数组的最大值为:%d\n", max);
printf("数组的最小值为:%d\n", min);
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)