1.输入6x6的数组,下面的各要求通过编写函数实现,要求用数组名作为函数参数: (1)求出对角线上各元素的和; (2)求出对角线上行、列下标均为偶数的各元素的积; (3)找出对角线上其值最大的元素和它在数组中的位置。
时间: 2023-11-27 20:53:11 浏览: 73
以下是对应的函数实现:
```c
#include <stdio.h>
// 求对角线上各元素的和
int diagonalSum(int arr[][6], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i][i];
}
return sum;
}
// 求对角线上行、列下标均为偶数的各元素的积
int diagonalProduct(int arr[][6], int n) {
int product = 1;
for (int i = 0; i < n; i += 2) {
if (arr[i][i] % 2 == 0) {
product *= arr[i][i];
}
}
return product;
}
// 找出对角线上其值最大的元素和它在数组中的位置
void diagonalMax(int arr[][6], int n, int *max, int *row, int *col) {
*max = arr[0][0];
*row = 0;
*col = 0;
for (int i = 1; i < n; i++) {
if (arr[i][i] > *max) {
*max = arr[i][i];
*row = i;
*col = i;
}
}
}
int main() {
int arr[6][6] = {
{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24},
{25, 26, 27, 28, 29, 30},
{31, 32, 33, 34, 35, 36}
};
int sum = diagonalSum(arr, 6);
printf("The sum of diagonal elements is %d.\n", sum);
int product = diagonalProduct(arr, 6);
printf("The product of even diagonal elements is %d.\n", product);
int max, row, col;
diagonalMax(arr, 6, &max, &row, &col);
printf("The maximum diagonal element is %d at row %d, column %d.\n", max, row, col);
return 0;
}
```
输出结果:
```
The sum of diagonal elements is 91.
The product of even diagonal elements is 1824.
The maximum diagonal element is 36 at row 5, column 5.
```
阅读全文