Part1:
1-1一維數組在內存中的存放及地址
1、數組元素在內存中是連續存放的。2、a+i和&a[i]都表示數組元素的地址,*(a+i)和a[i]都表示數組元素。但整形數據和字符型數據占用內存的大小不同。
1-2:二維數組在內存中的存放及地址
1、二維數組在內存中是按行存放的。2、a[i]+j和&a[i][j]都表示二維數組元素a[i][j]的地址,*(a[i]+j)和a[i][j]都表示二維數組元素a[i][j]。3、在二維數組中,a+i表示第i+1行的首地址,a[i]表示第一個元素的地址。
1-3使用指針變量間接訪問一維數組
1、在程序中,指針變量p在使用時對指針變量p進行了初始化把數組元素a【0】的地址賦給了p,指向了a【0】的地址。2、line12~line13執行完后p指向數組元素a【2】的地址,line16~line17執行完后,p指向數組元素a【2】。3、line22~line27執行完后,p指向數組元素a【2】,line26~line27執行完后指向數組元素。4、初始化順序不同。
1—4:使用指針變量間接訪問二維數組
1、可以改,a【0】和&a【0】【0】都表示數組第一個元素的地址。2、line19*q+j表示第a【j】的地址*(*q+j)表示數組元素a【j】。
Part 2綜合編程應用
2—1二分查找算法及編程應用
#include <stdio.h> #include <stdlib.h> const int N=5; int binarySearch(int x[], int n, int item); // 函數聲明 int main() { int a[N]={2,7,19,45,66}; int i,index, key; printf("數組a中的數據:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\n"); printf("輸入待查找的數據項: "); scanf("%d", &key); // 調用函數binarySearch()在數組a中查找指定數據項item,並返回查找結果給index // 補足代碼① // ××× index=binarySearch(a,N,key); if(index>=0) printf("%d在數組中,下標為%d\n", key, index); else printf("%d不在數組中\n", key); system("pause"); return 0; } //函數功能描述: //使用二分查找算法在數組x中查找特定值item,數組x大小為n // 如果找到,返回其下標 // 如果沒找到,返回-1 int binarySearch(int x[], int n, int item) { int low, high, mid; low = 0; high = n-1; while(low <= high) { mid = (low+high)/2; if (item == x[mid]) return mid; else if(item<x[mid]) high = mid - 1; else low = mid + 1; } return -1; }
#include <stdio.h> #include <stdlib.h> #define N 10 int fun(int *a,int m) { int low = 0, high = N-1, mid; /*************ERROR**************/ while(low <= high) { mid = (low+high)/2; /*************ERROR**************/ if(m < *(a+mid)) high = mid-1; /*************ERROR**************/ else if(m > *(a+mid)) low = mid+1; else return(mid); } return(-1); } int main() { int i,a[N]={-3,4,7,9,13,24,67,89,100,180},k,m; printf("a數組中的數據如下:\n"); for(i=0;i<N;i++) printf("%d ",a[i]); printf("\nEnter m: \n"); scanf("%d",&m); /*************ERROR**************/ k = fun(a,m); if (k>=0) printf("m=%d,index=%d\n",m,k); else printf("Not be found!\n"); system("pause"); return 0; }
2—2選擇排序算法及編程應用
2-2-1
2-2-2字符串選擇排序
#include <stdio.h> #include <string.h> #include <stdlib.h> void selectSort(char str[][20], int n ); // 函數聲明,形參str是二維數組名 int main() { char name[][20] = {"John", "Alex", "Joseph", "Taylor", "George"}; int i; printf("輸出初始名單:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); selectSort(name, 5); // 調用選擇法對name數組中的字符串排序 printf("按字典序輸出名單:\n"); for(i=0; i<5; i++) printf("%s\n", name[i]); system("pause"); return 0; } // 函數定義 // 函數功能描述:使用選擇法對二維數組str中的n個字符串按字典序排序 void selectSort(char str[][20], int n) { char temp[20]; int i,j,k; for(i=0;i<n-1;i++) { k=i; for(j=i+1;j<n;j++) if(strcmp(str[j],str[k])<0) k =j; if(k!=i){ strcpy(temp,str[i]); strcpy(str[i],str[k]); strcpy(str[k],temp); } } // 補足代碼 // ××× }
2——3用指針處理字符串
2-3-1
#include <string.h> #include <stdio.h> #include <stdlib.h> void fun(char *a) { /*****ERROR********/ int i; char *p = a; /****ERROR***/ while(*p && *p == '*') { a[i] = *p; i++; p++; } while(*p) { /******ERROR*******/ if(*p != '*') { a[i] = *p; i++; } p++; } /******ERROR*******/ a[i] = '\0'; } int main() { char s[81]; printf("Enter a string :\n"); gets(s); /***ERROR******/ fun(s); printf("The string after deleted:\n"); puts(s); system("pause"); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> void fun(char *a) { /**ERROR******/ int i; char *t = a, *f = a; char *q = a; while(*t) t++; t--; while(*t == '*') t--; while(*f == '*') f++; /***ERROR***/ while (q<f) { a[i] = *q; q++; i++; } while (q<t) { /***ERROR**/ if(*q != '*') { a[i] = *q; i++; } q++; } while (*q) { a[i] = *q; i++; q++; } /**ERROR**/ a[i]='\0'; } int main () { char s[81]; printf("Entre a string:\n"); gets(s); /**ERROR**/ fun(s); printf("The sting after deleted:\n"); puts(s); system("pause"); return 0; }
選擇題答案:ABCDEFG
實驗總結:
1、冒泡排序容易與選擇排序弄混淆,注意對比對象。2、數組與指針的本質都是地址,但還不熟悉,不能一眼看透,看來還是演多想想。3、字符串的賦值和比較要借助函數,不能直接進行。4、進行程序改錯的時候要注重邊界條件和邏輯關系。
還是需要進行更多的練習啊。