求出二维数组中的最大元素值 和 求出它的两个下标,并求出一维数组的平均值。

package ShuZu;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 功能说明:求出二维数组中的最大元素值 和 求出它的两个下标,并求出一维数组的平均值。
 * @author 夏大昌
 * @Date 2022/7/27 18:31
 */
public class ErWeiShuZu {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("输入数组的行数:");
        int h = input.nextInt();
        System.out.print("输入数组的列数:");
        int l = input.nextInt();
        int[][] a = new int[h][l];
        int j = 0;
        int sum=0;//和
        int avg=0;//平均值
        int th=0;//总长度
        for(int i = 0; i < a.length; i++) {
            System.out.println("输入第" + (i + 1) + "个数组:");
            for (j = 0; j < a[i].length; j++) {
                a[i][j] = input.nextInt();
                sum = sum + a[i][j];
            }
            th = th + a[i].length;
        }
        avg = sum / th;
        Location  location1=locateLargest(a);
        System.out.println("最大元素值: "+location1.maxValue+"\n下表是:"+"("+location1.row+" , "+location1.column+")"+"\n数组平均值:"+avg);
    }
    public static Location locateLargest(int[][] a) {
        Location location = new Location();
        int h = 0;
        int l = 0;
        double maxValue = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if(a[i][j]>maxValue){
                    maxValue=a[i][j];
                    h =i;
                    l=j;
                }
            }
        }
        location.row = h;
        location.column = l;
        location.maxValue = maxValue;
        return location;
    }
}
class Location{
    public  int row;//下标
    public int column;//下标
    public  double maxValue;//最大值
}

来实现。谢谢! 非常感谢您的提问。根据您的要求,我可以为您提供以下 C 语言程序: ```c #include <stdio.h> #define ROWS 3 // 数组的行数 #define COLS 4 // 数组的列数 int main() { int arr[ROWS][COLS] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; // 二维数组 int row, col; // 行列的变量 double sum = 0; // 数组元素的总 double avg = 0; // 数组元素平均值 int max = arr[0][0]; // 数组元素最大 int max_row = 0; // 最大所在的行 int max_col = 0; // 最大所在的列 int min = arr[0][0]; // 数组元素的最小 int min_row = 0; // 最小所在的行 int min_col = 0; // 最小所在的列 // 遍历整个数组,求、比较大小 for (row = 0; row < ROWS; row++) { for (col = 0; col < COLS; col++) { sum += arr[row][col]; // 计算总 if (arr[row][col] > max) { max = arr[row][col]; // 更新最大下标 max_row = row; max_col = col; } if (arr[row][col] < min) { min = arr[row][col]; // 更新最小下标 min_row = row; min_col = col; } } } avg = sum / (ROWS * COLS); // 计算平均值 // 输出结果 printf("数组的平均值为:%.2f\n", avg); printf("数组的最大为:%d,下标为[%d][%d]\n", max, max_row, max_col); printf("数组的最小为:%d,下标为[%d][%d]\n", min, min_row, min_col); return 0; } ``` 运行结果如下: ``` 数组的平均值为:6.50 数组的最大为:12,下标为[2][3] 数组的最小为:1,下标为[0][0] ``` 希望这个程序能够帮助到您,如果您有其他问题,欢迎随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值