首页 > 用户发贴区 > 编程问题提问区 > final考试题目.望高手能解答
2006
10-08

final考试题目.望高手能解答

1.(a) Write C language declarations for the following (use suitable variable/function names):


(1)A function that has no arguments but returns a floating-point
 value.



(2)A function that has a string as an argument and returns an
 integer value.



(3)An array of 4 integers initialised to 0, -100, 100, and 42.



(4)A two-dimensional (12×3) array of characters.



(5)A pointer to a string.



(6)A structure that contains a character, a string (of size 20) and a
 floating-point number.



(7)A void function that has a pointer to the structure in (6) as an
argument.



(b)State whether the following is true or false:


The function in (7) above can alter the value of the float in the structure that is passed to it.



(c)If your answer to (b) is true, write an assignment statement to alter the value of the float.


If your answer to (b) is false, how would you alter the definition of the function to allow it to alter the value?



2.(a)  Name one property of all valid recursive functions.



(b)Write a program that contains a recursive function. You may use   any of the examples from the lecture notes, or you may create an entirely new function.


Does your program in part (b) feature direct or indirect recursive
functions?



(d)Write a non-recursive version of the function you created in 2(b), using a for loop.



3. Write your own version of the strcmp function string_compare. The function has two strings as arguments and returns an integer. The value of the integer is:
negative if the first string is sorted before the second string,
zero if the two strings are identical,positive if the first string is sorted after the second string.

4. One way of finding out if a particular number n, stored in a global integer variable, exists in a sequence of 100 numbers, sorted in ascending order and stored in a global integer array a[100], is to apply a method called a ‘binary chop’.


The binary chop method uses a recursive function to find out whether the middle value in a range of the integers is equal to the particular number n, is less than n or is greater than n.


 int binary_chop(int begin, int end);


 where the function:


 calculates the value of (begin+end)/2 and stores it in a local variable m, then:


 returns 1 if a[m] is the same as n,


 returns 0 if begin is equal to end,


 returns the result of performing a binary chop using the range (begin, m) if a[m] is greater than n.


 otherwise returns the result of performing a binary chop using the range (m+1,end) if a[m] is less than n.


 Write the recursive function binary_chop. You do not have to write a main() function. You may assume that the global array a[100] exists and has been initialised, and that the global integer n also exists and has been initialised..




5.(a)Declare a C structure that can contain data describing a music CD: the title of the CD (string), the performer’s name (string), the number of tracks (integer), the title of each track (an array of strings), and the total playing time of the CD in minutes (integer). (The maximum number of tracks is 5.)
   



(b)Declare a global variable of the above structure, and initialise it with reasonable values. (Do not use assignment statements.)
 



(c)Declare a global variable that is a pointer to the above structure and initialise it with the address of the variable declared in (b).
 


(d)Write two different assignment statements, one using the variable in (b) and one using the pointer in (c) to change the total playing time of the CD. 
   



(e)Assume that you already have a 50-element array of the above structure, which has been initialised with the data of 50 CDs. Write a piece of C code that uses a loop to find the CD with the longest total playing time, (there will be one CD that is longer than the rest), and then prints the CD’s title and number of tracks.  



6. You have been asked to write a C program to manage a word game similar in style to “Hangman”. In this game a user is trying to guess a hidden word. The word is initialised and stored in the program. The user is prompted for a character. The program then reveals all the occurrences of the character in the word, and asks for another character.


 If all of the characters of the word are revealed the user wins.


 If the character does not exist, the program keeps a count of the number of misses, and the user loses the game after the 5th miss.


 e.g. (the hidden word is massey)


 (——) guess a character: m
 (m—–) guess a character: s
 (m-ss–) guess a character: p
 p is not in the word – 1 failure
 (m-ss–) guess a character: e
 (m-sse-) guess a character: y
 (m-ssey) guess a character: a
 (massey) well done, you win!


 or (the hidden word is dog)
 
 (—) guess a character: p
 p is not in the word – 1 failure
 (—) guess a character: e
 e is not in the word – 2 failures
 (—) guess a character: d
 (d–) guess a character: n
 n is not in the word – 3 failures
 (d–) guess a character: i
 i is not in the word – 4 failures
 (d–) guess a character: b
 b is not in the word – 5 failures
 you lose!


 


final考试题目.望高手能解答》有 2 条评论

  1. normanxie 说:

    就没人知道么????

  2. lisypro 说:

    先翻译一下吧

留下一个回复