定义一个10个元素的数组,求该数组中的最大值和最小值,及最大值和最小值的平均值
时间: 2024-05-30 14:14:43 浏览: 6
最大值为a[max],最小值为a[min],最大值和最小值的平均值为 (a[max]+a[min])/2。以下是示例代码:
#include <stdio.h>
int main() {
int a[10], i, max, min;
printf("请输入10个整数:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=1;i<10;i++)
{
if(a[i]>max) max=a[i];
if(a[i]<min) min=a[i];
}
printf("最大值为%d,最小值为%d,平均值为%.2f",max,min,(float)(max+min)/2);
return 0;
}
相关问题
java定义一个10个元素组成的一维数组,求该数组中的最大值和最小值。及最大值和最小值的平均值
以下是Java代码实现:
```java
public class Main {
public static void main(String[] args) {
int[] arr = {3, 9, 2, 8, 5, 1, 7, 6, 4, 10};
int max = arr[0];
int min = arr[0];
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
sum += arr[i];
}
double avg = (max + min) / 2.0;
System.out.println("最大值为:" + max);
System.out.println("最小值为:" + min);
System.out.println("最大值和最小值的平均值为:" + avg);
}
}
```
输出结果:
```
最大值为:10
最小值为:1
最大值和最小值的平均值为:5.5
```
声明一个类ARRAY,求一维数组中各元素的最大值,最小值和平均值
好的,以下是ARRAY类的代码,可以实现求一维数组中各元素的最大值、最小值和平均值:
```python
class ARRAY:
def __init__(self, arr):
self.arr = arr
def max(self):
return max(self.arr)
def min(self):
return min(self.arr)
def avg(self):
return sum(self.arr) / len(self.arr)
```
使用示例:
```python
arr = [1, 2, 3, 4, 5]
a = ARRAY(arr)
print(a.max()) # 输出:5
print(a.min()) # 输出:1
print(a.avg()) # 输出:3.0
```
希望能对您有所帮助!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)