240 likes | 483 Views
模擬投擲硬幣 100 次 每次擲出都印出 『H』 (頭)或 『T』 (尾)並計算 印出每一面出現的 次數( 『H』 出現 xx 次, 『T』 出現 xx 次). 5/10 上機題. #include <stdio.h> #include <stdlib.h> #include <time.h> int flip(); int main(){ int loop; int Count[2] ={0}; srand( time( NULL ) );
E N D
模擬投擲硬幣100次 每次擲出都印出 『H』(頭)或 『T』(尾)並計算 印出每一面出現的 次數(『H』出現 xx次,『T』出現xx 次) 5/10 上機題 #include <stdio.h> #include <stdlib.h> #include <time.h> int flip(); int main(){ int loop; int Count[2] ={0}; srand( time( NULL ) ); for ( loop = 1; loop <= 100; loop++ ) { } return 0; } flip()函數
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int loop, r, Count[2] ={0}; srand( time( NULL ) ); for ( loop = 1; loop <= 100; loop++ ){ r=rand() %2; ++Count[r]; if ( r == 0 ) printf( "正 " ); else printf( "反 " ); if (loop%20==0) printf("\n"); } printf("\n 出現正面共 %d次\n", Count[0] ); printf("出現反面共 %d次\n", Count[1] ); return 0; } 不使用函式
int flip() { int HorT = rand() %2; if ( HorT == 0 ) printf("正" ); else printf("反" ); return HorT; } #include <stdio.h> #include <stdlib.h> #include <time.h> int flip(); int main(){ int loop, Count[2] ={0}; srand( time( NULL ) ); for ( loop = 1; loop <= 100; loop++ ){ ++Count[ flip() ]; if (loop%20==0) printf("\n"); } printf("\n 出現正面共 %d次\n", Count[0] ); printf("出現反面共 %d次\n", Count[1] ); return 0; } 使用函式
Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 a[1][1] a[0][0] a[1][3] a[2][0] a[0][1] a[2][3] a[1][2] a[0][2] a[2][2] a[0][3] a[1][0] a[2][1] 6.9 多維陣列 • 多重下標陣列 • 經常用來表示表格 (m x n陣列) • 好像矩陣一般:首先指定列數,接著行數
1 2 3 4 1 0 3 4 6.9 多維陣列 • 初始化 • int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; • 初始化以列為單位,用大括號括起來 • 如果不足,沒被指定的元素會指定為 0 int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; • 參數使用 • 先指定列再指定行 printf( "%d", b[ 0 ][ 1 ] );
#include <stdio.h> #define STUDENTS 3 #define EXAMS 4 int minimum( const int [][ EXAMS ], int, int ); int maximum( const int [][ EXAMS ], int, int ); double average( const int [], int ); void printArray( const int [][ EXAMS ], int, int ); int main() { int student; const int studentGrades[ STUDENTS ][ EXAMS ] = { { 77, 68, 86, 73 }, { 96, 87, 89, 78 }, { 70, 90, 86, 81 } }; const int[] 與int的不同: 一般傳遞陣列給函式如果 函式內更改陣列中的值會 把原陣列更改掉,加上 const可以避免修飾詞可以 確保該陣列不會受到更動。
printf( "The array is:\n" ); printArray( studentGrades, STUDENTS, EXAMS ); printf( "\n\nLowestgrade: %d\nHighestgrade: %d\n", minimum( studentGrades, STUDENTS, EXAMS ), maximum( studentGrades, STUDENTS, EXAMS ) ); for ( student = 0; student <= STUDENTS - 1; student++ ) printf( "The average grade for student %d is %.2f\n", student, average( studentGrades[ student ], EXAMS ) ); return 0; }
int minimum( const int grades[ ][ EXAMS ], int pupils, int tests ) { int i, j, lowGrade = 100; for ( i = 0; i <= pupils - 1; i++ ) for ( j = 0; j <= tests - 1; j++ ) if ( grades[ i ][ j ] < lowGrade ) lowGrade = grades[ i ][ j ]; return lowGrade; } int maximum( const int grades[ ][ EXAMS ], int pupils, int tests ) { int i, j, highGrade = 0; for ( i = 0; i <= pupils - 1; i++ ) for ( j = 0; j <= tests - 1; j++ ) if ( grades[ i ][ j ] > highGrade ) highGrade = grades[ i ][ j ]; return highGrade; }
double average( const int setOfGrades[ ], int tests ) { int i, total = 0; for ( i = 0; i <= tests - 1; i++ ) total += setOfGrades[ i ]; return ( double ) total / tests; } void printArray(const int grades[ ][ EXAMS ],int pupils,int tests) { int i, j; printf( " [0] [1] [2] [3]" ); for ( i = 0; i <= pupils - 1; i++ ) { printf( "\nstudentGrades[%d] ", i ); for ( j = 0; j <= tests - 1; j++ ) printf( "%-5d", grades[ i ][ j ] ); } }
The array is: [0] [1] [2] [3] studentGrades[0] 77 68 86 73 studentGrades[1] 96 87 89 78 studentGrades[2] 70 90 86 81 Lowest grade: 68 Highest grade: 96 The average grade for student 0 is 76.00 The average grade for student 1 is 87.50 The average grade for student 2 is 81.75
課本習題 6.15 研討 • 使用一個陣列來解決下列問題: 讀進20個10~100的整數,可以重複。當每個數讀進來的時候,如果之前沒有輸入過此數,便把它印出來。 Hint:因為最糟可能會出現20都不一樣,所以要準 備好一個20個元素的陣列,想想你要如何處 理可能重複的問題?
#include <stdio.h> #define MAX 20 int main() { int a[ MAX ] = { 0 }; int i, j, k = 0, duplicate, value; printf( "Enter 20 integers between 10 and 100:\n" ); for ( i = 0; i <= MAX - 1; i++ ) { duplicate = 0; scanf( "%d", &value ); for ( j = 0; j < k; j++ ) { if ( value == a[ j ] ) { duplicate = 1; break; } } if ( !duplicate ) { a[ k++ ] = value; } }
printf( "\n The nonduplicate values are: \n" ); for ( i = 0; a[ i ] != 0; i++ ) { printf( "%d ", a[ i ] ); } printf( "\n" ); return 0; }
Enter 20 integers between 10 and 100: 1 2 3 4 5 6 7 8 9 10 2 6 9 12 14 21 88 43 88 23 The nonduplicate values are: 1 2 3 4 5 6 7 8 9 10 12 14 21 23 43 88
課本習題 6.28 研討 (去除重複)請撰寫一個程式,處理20個介於1~20之間的亂數,把所有非重複的數值存在一個陣列裡並印出。
#include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 20 int main() { int loop, randNumber, loop2, subscript = 0, duplicate; int array[ SIZE ] = { 0 }; srand( time( NULL ) ); for ( loop = 0; loop <= SIZE - 1; loop++ ) { duplicate = 0; randNumber = 1 + rand() % 20; for ( loop2 = 0; loop2 <= subscript; loop2++ ) { if ( randNumber == array[ loop2 ] ) { duplicate = 1; break; } }
if ( !duplicate ) { array[ subscript++ ] = randNumber; } } printf( "Non-repetitive array values are:\n" ); for ( loop = 0; array[ loop ] != 0; loop++ ) { printf( "\t\t\t\tArray[ %d ]= %d\n", loop, array[ loop ] ); } return 0; }
Non-repetitive array values are: Array[ 0 ] = 7 Array[ 1 ] = 9 Array[ 2 ] = 1 Array[ 3 ] = 17 Array[ 4 ] = 15 Array[ 5 ] = 11 Array[ 6 ] = 18 Array[ 7 ] = 5 Array[ 8 ] = 8 Array[ 9 ] = 4 Array[ 10 ] = 2 Array[ 11 ] = 6
課本習題 6.19 研討 • 撰寫一個程式模擬投擲兩顆骰子。然後計算出兩顆骰子總共的點數,投擲共36000次,用一個一維陣列來記錄各種點數出現的次數,將結果以表列的方式印出。 • Hint:兩顆骰子的總數應從2~12,各數出現的機率應該不盡相同,例如:2與12應該出現得很少,7應該出現得最多。
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { long i; int j; int x; int y; int sum[ 13 ] = { 0 }; int expected[ 13 ] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1}; srand( time( NULL ) ); for ( i = 1; i <= 36000; i++ ) { x = 1 + rand() % 6; y = 1 + rand() % 6; ++sum[ x + y ]; }
printf( "%10s%10s%10s%10s\n", "Sum", "Total", "Expected", "Actual" ); for ( j = 2; j <= 12; j++ ) { printf( "%10d%10d%9.3f%%%9.3f%%\n", j, sum[ j ], 100.0 * expected[ j ] / 36, 100.0 * sum[ j ] / 36000 ); } return 0; }
Sum Total Expected Actual 2 1045 2.778% 2.903% 3 2012 5.556% 5.589% 4 3054 8.333% 8.483% 5 3963 11.111% 11.008% 6 4992 13.667% 13.867% 7 6061 16.667% 16.836% 8 4974 13.889% 13.817% 9 3934 11.111% 10.928% 10 2936 8.333% 8.156% 11 2035 5.556% 5.653% 12 994 2.778% 2.761%
5/18 上機課 • 撰寫一個程式模擬投擲四枚硬幣,正面計1分,反面計0分。然後計算出四枚硬幣總共的點數,投擲共32000次,用一個一維陣列來記錄各種 點數出現的次數 ,將結果以表列 的方式印出。 • 使用2維陣列, 印出右側的表格