C Program Control

 

1. 尋找一連串數字的最小值

Write a program that finds the smallest of several integers.  Assume that the first value read specifies the number of values remaining.

步驟:

Step 1: 先建立空的原始程式檔,1_1_1.c

Step 2: 宣告一整數變數 number,代表一連串數字的個數。使用者由鍵盤輸入值,存為number1_1_2.c

Step 3: 宣告一整數變數 smallest,代表目前之最小整數值。使用者由鍵盤輸入第1個值,存為smallest1_1_3.c

Step 4: 宣告一整數變數 value,代表新輸入值。使用for迴圈輸入第2至第number整數,將輸入值存為value,並將valuesmallest比較。若value smallest小,則將smallest更新為value值。1_1_4.c

Step 5: 印出smallest值。1_1_5.c

1_1_5.exe

 

2. for 外迴圈與內迴圈的應用

Write a program that prints the following patterns separately one below the other.

a)

*

**

***

****

*****

******

*******

********

*********

**********

b)

         *

        **

       ***

      ****

     *****

    ******

   *******

  ********

 *********

**********

 

步驟:

Step 1: 先建立空的原始程式檔。宣告整數變數 row, 整數變數col1_2_1.c

Step 2: 建立外迴圈,row=1, row=10, 10列。1_2_2.c

Step 3: 建立內迴圈,col=1, col=row,印出星號。1_2_3.c

Step 4: 加以換行後,進入B部份。建立外迴圈,row=1, row=10, 10列。1_2_4.c

Step 5: 宣告整數變數 space。建立內迴圈,space=1, space=10-row,印出空格。1_2_5.c

Step 6: 建立內迴圈,col=1, col=row,印出星號。1_2_6.c, 1_2_6.exe

 

(Exercise)

3. 使用for 外迴圈與內迴圈印出菱形

Write a program that prints the following diamond shape.

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

 

步驟:

Step 1: 先建立空的原始程式檔。宣告整數變數 line, 整數變數space, 整數變數asterisk1_3_1.c

Step 2: 先作上半部,建立外迴圈,line=1, row=9, 遞增為21_3_2.c

Step 3: 建立內迴圈,space=( 9 - line ) / 2, 1,印出空格。建立內迴圈,asterisk=1, line,印出星號。1_3_3.c

Step 4: 再作下半部,建立外迴圈,line=7, line=1, 遞減為 21_3_4.c

Step 5: 建立內迴圈,space=( 9 - line ) / 2, 1,印出空格。建立內迴圈,asterisk=1, line,印出星號。1_3_5.c

1_3_5.exe

 

4. 使用for 迴圈計算

Calculate the value of  from the infinite series

1_4.c, 1_4.exe

 

(Exercise)

5. 使用for 迴圈計算非零整數的階乘

n! = n*(n-1)*(n-2)*…*2*1.

a)      Write a program that reads a nonnegative integer and computes and prints its factorial.

b)      Write a program that estimates the value of the mathematical constant  by using the formula:

 

c)      Write a program that computes the value of  by using the formula

1_5a.c, 1_5a.exe

1_5b.c, 1_5b.exe

1_5c.c, 1_5c.exe

 

6. 使用for 迴圈印出1256整數之二進位、8進位、與16進位的轉換表

Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256. 

1_6.c, 1_6.exe

 

(Exercise)

7. 使用for 迴圈印出一介於19941999的年曆

Write a program that inputs the year in the range 1994 through 1999 and uses for-loop repetition to produce a condensed, neatly printed calendar.  Watch out for leap years.

1_7.c, 1_7.exe