## Sample Exam 2

1. (20 points) Write a complete C function that displays a string with all its a's removed. For example, if the function is given the string

`Are we having any fun, yet?`

it will display the string

`Are we hving ny fun, yet?`

You may use either array subscript notation or pointer notation to solve this problem.

2. (20 points)

(a)
In a single statement, declare an array of type float called numbers with 5 elements, and initialize the elements to the values 0.0, 1.1, 2.2, 3.3, 4.4.

(b)
Declare a pointer variable called nptr that can point to an object of type float.

(c)
Give two different statements that assign the starting address of array numbers to the pointer variable nptr.

(e)
Write a for-loop that adds 1.0 to each element of the array. Access the elements of the array using pointers (instead of subscripts).

3. (15 points) What would be displayed by the following program?
```#include <stdio.h>

void passPointers(int *p1, int *p2);

int main()
{
int array[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int *pBeg, *pEnd;
int i;

pBeg = array;
pEnd = &array[9];

passPointers (pBeg, pEnd);

for (i=0; i<10; i++)
printf("%d  ", array[i]);
return 0;
}

void passPointers (int *p1, int *p2)
{
int i;

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

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

while (p1 != p2){
*p1 = *p1 + 5;
p1++;
}

return;
}
```

Questions 4 - 6 are worth 5 points each:

4. Given these C declarations:
```  float x;
float *z=&x;
```
which scanf statement would successfully read a float value into x?
• `scanf ("%f", x);`
• `scanf ("%f", z);`
• `scanf ("%f", *z);`
• a and b
• a, b, and c

5. Complete the following sentence: The function fwrite
• should only be used with text files.
• transfers a specified number of bytes beginning at a specified location in memory to a file that has been opened with fopen.
• transfers a specified number of bytes from a file that has been opened with fopen to a specified location in memory.
• is equivalent to function fprintf, except that fwrite can only write to the stream `stdout`.

6. An advantage of text files over binary files is that
• text files are more efficient than binary files
• text files generally take up less space than binary files
• text files can be processed (i.e. read from or written to) faster than binary files
• text files are human-readable, while binary files are not

7. (15 points) Here is a makefile:
```

# makefile for exam 2

projectAlpha:  main.o tree.o list.o
gcc main.o tree.o list.o -o projectAlpha

main.o:  main.c tree.h list.h
gcc -c -Wall main.c

tree.o:  tree.c tree.h
gcc -c -Wall tree.c

list.o:  list.c list.h
gcc -c -Wall list.c

# end of makefile

```
• List the target file(s)

• What will be the name of the executable file?

• Assume that make is run to create the executable file, and then later a change is made to tree.h. If you run make again, which compilation command(s) will be executed? (Write down the actual command(s) that will be executed.)

8. (15 points) The babies array declared below holds the birth records of babies born at General Hospital:
```struct date{
int month;
int day;
int year;
};

struct birthrecord{
char lastname[25];  /* a null-terminated string */
char firstname[25]; /* a null-terminated string */
struct date dob;    /* date of birth            */
float length;
int pounds;
int ounces;
};

struct birthrecord babies[500];
```
• The babies whose records are in array positions 23 and 24 are twins. Write a single C statement that will copy the last name of the baby in array position 23 to the last name of the baby at array position 24.

• Write a single C statement that will print the second letter of the first name of the baby whose record is stored in array position 10.

• Write a single C statement that will store the value 6 as the birth month for the baby whose record is at array position 1.