一、填空題:給定程序中,函數fun的功能是:將N×N矩陣主對角線元素中的值與反向對角線對應位置上元素中的值進行交換。例如,若N=3,有下列矩陣:
1 2 3
4 5 6
7 8 9
交換後爲:
3 2 1
4 5 6
9 8 7
請在程序的下劃線處填入正確的內容並把下劃線刪除,使程序得出正確的結果。
不得增行或刪行,也不得更改程序的.結構!
給定源程序:
#include
#define N 4
/**********found**********/
void fun(int ___1___ , int n)
{int i,s;
/**********found**********/
for(___2___; i++)
{s=t[i][i];
t[i][i]=t[i][n-i-1];
/**********found**********/
t[i][n-1-i]=___3___;
}
}
main()
{int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42, 21,33,10}, i, j;
printf("nThe original array:n");
for(i=0; i
{for(j=0; j
}
fun(t,N);
printf("nThe result is:n");
for(i=0; i
{for(j=0; j
}
}
解題答案:
/**********found**********/
void fun(int t[][N] , int n)
/**********found**********/
for(i=0;i
/**********found**********/
t[i][n-1-i]=s;
二、改錯題:由N個有序整數組成的數列已放在一維數組中,給定程序MODI1.C中函數fun的功能是:利用折半查找算法查找整數m在數組中的位置。若找到,返回其下標值;反之,返回-1。
折半查找的基本算法是:每次查找前先確定數組中待查的範圍:low和high
(lowhigh,查找結束。
請改正程序中的錯誤,使它能得出正確結果。
注意:不要改動main函數,不得增行或刪行,也不得更改程序的結構。
給定源程序:
#include
#define N 10
/************found************/
void fun(int a[], int m)
{int low=0,high=N-1,mid;
while(low<=high)
{mid=(low+high)/2;
if(m
high=mid-1;
/************found************/
else If(m > a[mid])
low=mid+1;
else return(mid);
}
return(-1);
}
main()
{int i,a[N]={-3,4,7,9,13,45,67,89,100,180 },k,m;
printf("a數組中的數據如下:");
for(i=0;i
k=fun(a,m);
if(k>=0) printf("m=%d,index=%dn",m,k);
else printf("Not be found!n");
}
解題答案:
/************found************/
int fun(int a[],int m)
/************found************/
else if(m>a[mid])
******************************************