Exam #3 Review

(Last Mod: 27 November 2010 21:38:42 )

ECE-1021 Home


Links:

Material include by reference:

Bookmarks:


Topics Covered

In addition to the topics covered on prior exams, the following are topics that are fair game for this exam:

Recursive Functions

File operations - opening/closing

Formatted I/O

Unformatted I/O

Typedef Statements

Structures


 

Review Questions

 

True/False

 

NOTE: You must explain your reasoning to receive full credit, T/F alone will only receive 1/4 credit.

 

1) If a file open operation using fopen() fails, then the file should be immediately closed using fclose().

 

2) When the name of an array is used as an argument to a function, the array is passed by reference.

 

3) When the name of a structure is used as an argument to a function, the structure is passed by reference.

 

4) The functions fread() and fwrite() need to know what type of data is being referenced.

 

5) The functions fprintf() and fscanf() need to know what type of data is being referenced.

 

6) An array can contain multiple values, but all must be of the same data type.

 

7) A structure can contain multiple values, but all must be of the same data type.

 

8) Given a recursive function, an interative function that performs the same task can always be written that will run more quickly and use less memory.

 

9) A recursive function need not have a non-recursive path through it in order to be successful.

 

10) When using printf(), the same format specifier is used for both a float and a double.

 

11) When using scanf(), the same format specifier is used for both a float and a double.

 

12) When using printf(), printing out a percent sign in the format string requires that it be escaped with a backslash just like a newline character or a double quote.

 

 

Long Answer

1)     Write the decimal integer ?31,677 as a 16-bit signed binary integer.

2)     Given the definition

int x;

how are the decimal values of x and ~x arithmetically related? In other words, write ~x as a function of x using only the normal arithmetic operators such as addition, subtraction, multiplication, division, and exponentiation.

3)     What is printed?

char char1 = ?P?;

char char2 = ?Q?;

char *p, *q;

p = &char1;

q = &char2;

*p = *q;

printf( ?[%c] [%c] [%c] [%c]?, char1, char2, *p, *q );


4)       What is printed?

 

int numbs[ 2 ];

int *ptr = numbs;

numbs[ 0 ] = 10;

numbs[ 1 ] = 1000;

printf( "%d\n", ++*ptr++ );
printf( "%d\n", ++*ptr );
printf( "%d\n", *ptr );
printf( "%d\n", ++*ptr++ );

 5)      Given the following code: 

int sum(int a[], int n);

int main( void )

{

int emissions[ 10 ][ 5 ], total;

...

total = sum( ( int* ) emissions, 50 );

...

}

int sum( int a[], int n )

{

      int i, val = 0;

     

      for( i = 0; i < n; i++)

      val += a[ i ];

 

return( val );

}  

Explain how sum() adds the values in the two-dimensional array emissions. In particular, how can it do so when it is treated as a one dimensional array within sum()?

6)    Write a line of code that uses sprintf() to convert an integer variable k of type long to a string of octal digits stored in the character array octstring. What is the minimum number of characters that the string should be dimensioned for if a long uses 32 bits? You must show how you arrived at this value in order to obtain partial credit if you are incorrect.

7)    Write a recursive function that computes  S(n) = 2 + 4 + 6 + ... + 2n  [Hint: S(n) = S(n-1) + 2n]

8)      Suppose that we define

 int numbs[100][100]; /* array of 10,000 ints */

and store in each cell the sum of the two indexes that reference that cell. For example, the contents of numbs[5][87] would be 92. Now assume that we map numbs into a one-dimensional array new_numbs, which also has 10,000 integer variables, in such a way that each cell in new_numbs has the same contents as the corresponding cell in numbs. What is printed? 

printf(  ?%d?, new_numbs[ 67 ] );  // value printed: _____

printf(  ?%d?, new_numbs[ 0 ] );   // value printed: _____

printf(  ?%d?, new_numbs[ 876 ] ); // value printed: _____

printf(  ?%d?, new_numbs[ 777 ] ); // value printed: _____

printf(  ?%d?, new_numbs[ 2 ] );   // value printed: _____

9)      Given the following structure declaration 

struct complex

{

double real;

double imag;

};

The conjugate of a complex number a + bi is a ? bi. Write a function with the prototype:

struct complex conj_c( struct complex c );

that returns the complex conjugate of c.

 10)  Given the following structure declaration 

struct complex

{

double real;

double imag;

};

The conjugate of a complex number a + bi is a ? bi. Write a macro conj_c( c ) that converts a complex number c to its conjugate.

The following questions refer to the structure declaration below that is suitable for storing the values of a three-dimensional vector in cartesian form. Following the structure declaration are the prototypes for it's primitive functions.

typedef struct v3 V3;

 

struct v3

{

    double x, y, z;

};

 

double GetV3_x(V3 *p);

double SetV3_x(V3 *p, double v);

double GetV3_y(V3 *p);

double SetV3_y(V3 *p, double v);

double GetV3_z(V3 *p);

double SetV3_z(V3 *p, double v);

11) Write the actual function definitions for the first two functions.

12) Write a utility function called magV3() that takes a V3 structure and returns its magnitude. The argument should be passed by value.

13) Write a utility function called magV3() that takes a V3 structure and returns its magnitude. The argument should be passed by reference.

14) Write a utility function called unitV3() that takes a V3 structure and returns a V3 structure that is a unit vector in same direction. Both the argument and the return value should be passed by value.

15) Write a utility function called unitV3() that takes a V3 structure that is passed by reference and converts it to a unit vector in same direction. The return value should be the magnitude of the original vector.

16) Write a utility function called dotV3() that takes two V3 structures and returns the dot product of the two vectors. The arguments should be passed by value.

17) Write a utility function called dotV3() that takes two V3 structures and returns the dot product of the two vectors. The arguments should be passed by reference.

18) Write a utility function called crossV3() that takes two V3 structures and returns the cross product of the two vectors. The arguments should be passed by value. The return value, which is also a V3 structure, should also be returned by value.

19) Write a utility function called crossV3() that takes three V3 structures, all passed by reference, and stores the cross product of the first two vectors in the third.

20) Write a utility function called CylindricalV3() that takes three arguments representing the cylindrical components of a vector - the radius, the radian angle from the x-axis, and the z-component - and stores the vector in a V3 structure.

EXTRA CREDIT ? TEN POINTS

Write a complete program that accepts two command line arguments where the first is the base name of a file and the second is the extension. If no arguments are given, the program issues a message to that effect. If only one argument is given, the program assumes a *.txt extension. The program should inform the User whether or not the file already exists.