c語言編程全部練習_第1頁
c語言編程全部練習_第2頁
c語言編程全部練習_第3頁
c語言編程全部練習_第4頁
c語言編程全部練習_第5頁
已閱讀5頁,還剩3頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、命名 文件和郵件名相同 壓縮包內文件無所謂 3414_E01.zip在下周二上課前交Exercise 1: Programming Environment and Basic Input/Output1. Write a program that prints “This is my first program!” on the screen. (a) Save this program onto your own disk with the name of e2-1a;(b) Run this program without opening Turbo C;(c) Modify this p

2、rogram to print “This is my second program!”, then save it as e2-1b. Please do not overwrite the first program.2. Write a program that prints the number 1 to 4 on the same line. Write the program using the following methods:(a) Using four “printf” statements.(b) Using one “printf” statement with no

3、conversion specifier (i.e. no %).(c) Using one “printf” statement with four conversion specifiers3(a) Write a program that calculates and displays the number of minutes in 15 days. (b) Write a program that calculates and displays how many hours 180 minutes equal to. (c) (Optional) How about 174 minu

4、tes?Exercise 2: Data Types and Arithmetic Operations1. You purchase a laptop computer for $889. The sales tax rate is 6 percent. Write and execute a C program that calculates and displays the total purchase price (net price + sales tax).2Write a program that reads in the radius of a circle and print

5、s the circles diameter, circumference and area. Use the value 3.14159 for “p”.3Write a program that reads in two numbers: an account balance and an annual interest rate expressed as a percentage. Your program should then display the new balance after a year. There are no deposits or withdraws just t

6、he interest payment. Your program should be able to reproduce the following sample run:Interest calculation program.Starting balance? 6000Annual interest rate percentage? 4.25Balance after one year: 6255.00Exercise 3: Selection structure1 Write a C program that accepts a students numerical grade, co

7、nverts the numerical grade to Passed (grade is between 60-100), Failed (grade is between 0-59), or Error (grade is less than 0 or greater than 100).2 Write a program that asks the user to enter an integer number, then tells the user whether it is an odd or even number. 3 Write a program that reads i

8、n three integers and then determines and prints the largest in the group.Exercise 4: switch statement and simple “while” repetition statement1. Write a program that reads three integers an abbreviated date (for example: 26 12 94) and that will print the date in full; for example: 26th December 1994.

9、 The day should be followed by an appropriate suffix, st, nd, rd or th. Use at least one switch statement.2Write a C program that uses a while loop to calculate and print the sum of the even integers from 2 to 30.3. A large chemical company pays its sales staff on a commission basis. They receive 20

10、0 per week plus 9% of their gross sales for that week. For example, someone who sells 5000 of chemicals in one week will earn 200 plus 9% of 5000, a total of 650. Develop a C program that will input each salespersons sales for the previous week, and print out their salary. Process one persons figure

11、s at a time.Enter sales in pounds (-1 to end): 5000.00Salary is: 650.00Enter sales in pounds (-1 to end): 00.00Salary is: 200.00Enter sales in pounds (-1 to end): 1088.89Salary is: 298.00Enter sales in pounds (-1 to end): -1Optional:兩重的while4.A mail order company sells five different products whose

12、retail prices are shown in the following table:Product NumberRetail Price (in pounds)12.9824.5039.9844.4956.87Write a C program that reads in a series of pairs of numbers as follows:(1). Product number(2). Quantity sold for one dayYour program should use a switch statement to help determine the reta

13、il price for each product, and should use a sentinel-controlled loop to calculate the total retail value of all products sold in a given week (7days).Exercise 5: for and do while” repetition statements 1. Write a program which uses a do/while loop to print out the first 10 powers of 2 other than 0 (

14、ie. it prints out the values of 21, 22, ., 210). Use a for loop to do the same.2.The constant p can be calculated by the infinite series:p = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 +.Write a C program that uses a do/while loop to calculate p using the series. The program should ask the user how many terms

15、in the series should be used. Thus if the user enters 3, then the program should calculate p as being 4 - 4/3 + 4/5.Nested repetition 3. Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single blank. Maximize your use

16、 of repetition (with nested for statements) and minimize the number of printf statements.*4. Write a program to print a table as follows:1*1= 12*1= 2 2*2= 43*1= 3 3*2= 6 3*3= 9.9*1= 9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81Exercise 6: Simple Functions 1. Write a C program that reads

17、several numbers and uses the function round_to_nearest to round each of these numbers to the nearest integer. The program should print both the original number and the rounded number. 2. Write a program that reads three pairs of numbers and adds the larger of the first pair, the larger of the second

18、 pair and the larger of the third pair. Use a function to return the larger of each pair.3.A car park charges a 2.00 minimum fee to park for up to 3 hours, and an additional 0.50 for each hour or part hour in excess of three hours. The maximum charge for any given 24-hour period is 10.00. Assume tha

19、t no car parks for more than 24 hours at a time.Write a C program that will calculate and print the parking charges for each of 3 customers who parked their car in the car park yesterday. The program should accept as input the number of hours that each customer has parked, and output the results in

20、a neat tabular form, along with the total receipts from the three customers:Car Hours Charge1 1.5 2.002 4.0 2.503 24.0 10.00TOTAL 29.5 14.50The program should use the function calculate_charges to determine the charge for each customer.Exercise 7: More Functions1.Write a program that uses sentinel-c

21、ontrolled repetition to take an integer as input, and passes it to a function even which uses the modulus operator to determine if the integer is even. The function even should return 1 if the integer is even, and 0 if it is not. The program should take the value returned by the function even and us

22、e it to print out a message announcing whether or not the integer was even.2.Write a C program that uses the function integerPower1(base, exponent) to return the value of:baseexponentso that, for example, integerPower1(3, 4) gives the value 3 * 3 * 3 * 3. Assume that exponent is a positive, non-zero

23、 integer, and base is an integer. The function should use a for loop, and make no calls to any math library functions.3.Write a C program that uses the recursive function integerPower2(base, exponent) to return the value of:baseexponentso that, for example, integerPower2(3, 4) gives the value 3 * 3

24、* 3 * 3. Assume that exponent is a positive, non-zero integer, and base is an integer. The function should make no calls to any math library functions.(Hint: the recursive step will use the relationship:baseexponent = base . baseexponent - 1and the base case will be when exponent is 1 since : base1

25、= base.)Exercise 8: Arrays1. Write a program that reads ten numbers supplied by the user into a single subscripted array, and then prints out the average of them.2.Write a program that reads ten numbers supplied by the user into a 2 by 5 array, and then prints out the maximum and minimum values held

26、 in:(a) each row (2 rows)(b) the whole array3 Use a single-subscripted array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in wh

27、ich all 20 numbers are different. Use the smallest possible array to solve this problem.Exercise 9: More Arrays1. Write a program that enters 5 names of towns and their respective distance (an integer) from London in miles. The program will print of the names of the towns that are less than 100 mile

28、s from London. Use arrays and character strings to implement your program.2.Write a program that prompts the user to type in four character strings, places these in an array of strings, and then prints out: (e.g. I am Peter Pan)(i) The four strings in reverse order. (e.g. Pan Peter am I)(ii) The fou

29、r strings in the original order, but with each string backwards. (e.g. I ma reteP naP)(iii) The four strings in reverse order with each string backwards. (e.g. naP reteP ma I)Exercise 10: Pointers1.Write a program that reads 5 integers into an array, and then uses four different methods of accessing

30、 the members of an array to print them out in reverse order.2.Write a program that reads 8 floats into an array and then prints out the second, fourth, sixth and eighth members of the array, and the sum of the first, third, fifth and seventh, using pointers to access the members of the array.3.Write a program that use a SINGLE FUNCTION (用一個函數)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論