以下是問題 - 編寫一個程序,使用函數來查找矩陣中的最大元素。使用雙指針數組的函數 - 在矩陣中查找最大值
功能規範:
INT findMax(INT **一個,INT米,INT N) 第一個參數對應於所述指針矩陣。 第二個參數對應於矩陣中的行數。 第三個參數對應於矩陣中的列數。
以下是我的代碼,雖然沒有編譯錯誤,但我不知道錯在哪裏。請提前幫助和感謝!
#include<stdio.h>
#include<malloc.h>
int findMax(int **a, int m, int n) {
int c,d, maximum=a[0][0];
for(c = 0 ; c < m ; c++)
{
for(d = 0 ; d < n ; d++)
{
if (a[c][d] > maximum)
maximum = a[c][d];
}
} return maximum;
}
int main()
{
int m, n, c, d, maximum;
int **a = (int **)malloc(10 * sizeof(int *));
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(c = 0 ; c < m ; c++)
{
for(d = 0 ; d < n ; d++)
{
scanf("%d",&a[c][d]);
}
}
printf("The matrix is\n");
for(c = 0 ; c < m ; c++)
{
for(d = 0 ; d < n ; d++)
{
printf("%d ",a[c][d]);
}
printf("\n");
}
maximum = findMax(a,m,n);
printf("The maximum element in matrix is %d\n", maximum);
return 0;
}
這真的很有幫助!非常感謝! –
再次感謝! :) –
@ user3193036,很高興我能幫上忙。 –