Exam #3 Review

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

ECE-1021 Home


Links:

Material include by reference:

Bookmarks:


Topics Covered

In addition to the topics covered on Exam #1, A review of the Course Syllabus shows that the following topics are fair game for the exam:

Pointers and pointer arithmetic.

Pointers as arguments to functions.

Pointers and multidimensional array.

Pointers to pointers.

Command Line arguments.

Storage classes, particularly auto and static.

File operations - opening/closing

Formatted I/O

Unformatted I/O

Typedef Statements

Structures

Compile-time versus Run-time storage allocation

Dynamic memory allocation.

Linked Lists


 

Review Questions

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 related.?

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 of type long to a string of octal digits.

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)      What is printed? 

#include <stdio.h>

#include <stdlib.h>

 

int g(int x );

int main( void )  /* originally ?main()? */

{

int i;

 

for( i = 1; i < 5; i++ )

printf( ?%d\n?, g( i ) );

return EXIT_SUCCESS;

}

 

int g( int x )

{

static int v = 1;

int b = 3;

 

v += x;

return v + x + b;

}

 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 replaces the complex number c by its conjugate.

 11)  What is printed?

int i;

float* flptr;

flptr = calloc( 3, sizeof( float ) );

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

   *flptr++ = i * 1.1;

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

   printf( ?\n%f\n?, *--flptr );

EXTRA CREDIT ? TEN POINTS

Part 1) (2pts) Write a structure definition for a structure named ?vector? that has two elements. The first is an integer giving the dimension of the vector and the second is a pointer to a dynamically allocated array of type double of the given dimension. Use a typedef statement to create an alias for "struct vector" known as "VECTOR".

Part 2) (4pts) Write a set of primitive functions than can be used to create a new VECTOR structure, free an existing VECTOR structure, and to set and retrieve any of its member elements. The function that sets the dimension should also allocate (or reallocate as appropriate) sufficient memory to store all of the components of the vector.

Part 3) (4pts) Write a function called DotProduct() that takes pointers to two structures of type VECTOR and returns the vector dot product of the two structures. The function should return zero if the two vectors are not the same size. Note that this is NOT a primitive function.