精華區beta NTU-Exam 關於我們 聯絡資訊
課程名稱︰計算機程式 課程性質︰必修 課程教師︰鄭士康 開課系所︰物理系 考試時間︰1/16 2004 試題 : Computer Programming, Final Exam, 1/16/2004 1. Answer the following questions regarding an array called table: (a) Declare the array to be an integer array and to have 3 rows and 3 columns: Assume that the constant variable arraySize has been declared to be 3. (2%) (b) How many elements does the array contain? (2%) (c) Use a for repetition structure to initialize each element of the array to the sum of its subscripts. Assume that the integer variable i and j are declared as control variables. (2%) (d) Write a program segment to print the values of each element of array table in tabular format with 3 rows and 3 columns. Assume that the array was initialized with the declaration int table[ arraySize ][ arraySize ] = { { 1, 8 }, { 2, 4, 6 }, { 5 } }; and the integer variable i and j are declared as control variables. Show the output. (2%) 2. For each of the following, write a single statement that performs the specified task. Assume that floating-point variable number1 and number2 have been declared and that number1 has been initialized to 7.3. Assume that variable ptr is of type char*. Assume that arrays s1 and s2 are each 100-element char arrays that are initialized with string literals. (a) Declare the variable fPtr to be a pointer to an object of type double. (2%) (b) Assign the address of variable number1 to pointer variable fPtr. (2%) (c) Print the value of the object pointed to by fPtr. (2%) (d) Assign the value of the object pointed to by fPtr to variable number2. (2%) (e) Print the value of number2. (2%) (f) Print the address of number1. (2%) (g) Print the address stored in fPtr. Is the value printed the same as the address of number1? (2%) (h) Copy the string stored in array s2 into array s1. (2%) (i) Compare the string in s1 with the string in s2, and print the result. (2%) (j) Append the first 10 characters from the string in s2 to the string in s1. (2%) (k) Determine the length of the string in s1, and print the result. (2%) (l) Assign to ptr the location of the first token in s2. The token delimiters are commas (,). (2%) 3. Find the error(s) in each of the following code segments. Assume the following declarations and statements: int *zPtr; // zPtr will reference array z int *aPtr = 0; void *sPtr = 0; int number; int z[ 5 ] = { 1, 2, 3, 4, 5 }; sPtr = z; (a) (2%) ++zPtr; (b) (2%) // use pointer to get first value of array number = zPtr; (c) (2%) // assign array element 2 (the value 3) to number number = *zPtr[2]; (d) (2%) // print entire array z for ( int i = 0; i <= 5; i++ ) cout << zPtr[ i ] << endl; (e) (2%) // assign the value pointed to by sPtr to the number number = *sPtr; (f) (2%) ++z; 4. Explain the following concepts briefly: (a) Bubble sort (3%) (b) Binary search (3%) (c) Constant pointer to non-constant data (3%) (d) Function pointers (3%) (e) Difference between struct and class (3%) (f) Statement friend (3%) (g) FORTRAN statement implicit none (3%) (h) FORTRAN labeled common blocks (3%) 5. What does the following program do? (6%) // Final2003Mystery\Main.cpp #include <iostream> using std::cout; using std::cin; using std::endl; void mystery( char *, const char * ); int main() { char string1[ 80 ]; char string2[ 80 ]; cout << "Enter two strings: "; cin >> string1 >> string2; mystery( string1, string2 ); cout << string1 << endl; return 0; } // end main // what does this function do? void mystery( char *s1, const char *s2 ) { while ( *s1 != '\0' ) ++s1; for ( ; *s1 = *s2; s1++, s2++ ) ; // empty statement } // end mystery 6. Finish a program according to the following description and templates. You need only write on your answer sheet the code segments corresponding to the comments labeled (a) to (c). Program Description: Create a class called Complex for performing arithmetic with complex numbers. Write a driver program to test your class. Use float-point variables to represent the private data of the class. Provide a constructor function that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions for each of the following: " Addition of two Complex numbers " Subtraction of two Complex numbers " Printing Complex numbers in the form (a, b) where a is the real part and b is the imaginary part. Template: Main.cpp // Main.cpp #include <iostream> using std::cout; using std::endl; #include "Complex.h" int main() { Complex b( 1, 7 ), c( 9, 2 ); b.printComplex(); cout << " + "; c.printComplex(); cout << " = "; b.add( c ); b.printComplex(); cout << '\n'; b.setComplexNumber( 10, 1 ); c.setComplexNumber( 11, 5 ); b.printComplex(); cout << " - "; c.printComplex(); cout << " = "; b.subtract( c ); b.printComplex(); cout << endl; return 0; } // main Template: Complex.h // Complex.h #ifndef COMPLEX_H #define COMPLEX_H /* (a) Write class declaration for Complex (10%)*/ #endif Template: Complex.cpp // Complex.cpp #include <iostream> using std::cout; #include "Complex.h" // Constructor Complex::Complex( double real, double imaginary ) { setComplexNumber( real, imaginary ); } // constructor // add complex numbers void Complex::add( const Complex& a ) { /* (b) Write statements to add the realPart and imaginary part of a to the class realPart and imaginaryPart, respectively (3%) */ } // add // subtract complex numbers void Complex::subtract( const Complex& s ) { /* (c) Write statements to subtract the realPart and imaginary part of s from the class realPart and imaginaryPart, respectively (3%)*/ } // subtract // print complex numbers void Complex::printComplex() { cout << '(' << realPart << ", " << imaginaryPart << ')'; } // printComplex // set complex number void Complex::setComplexNumber( double real, double imaginary ) { realPart = real; imaginaryPart = imaginary; } // setComplexNumber 7. Write a complete program according to the following description of requirements: 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. Provide for the "worst case" in which all 20 numbers are different. Use the smallest possible array to solve this problem. (10%) -- " 可謂是一個是無敵海景舉世無雙驚天動地鬼哭神號上鉤拳 一個是黯然銷魂情深義重牽腸掛肚聲淚俱下太極刀 " -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.247.137