KEY

CPSC 206 – 507Final Exam

Spring 2006

(20060510)

SID/SSN:

ScorePossiblePoints

Part One10 pts

Part Two35 pts

Part Three20 pts

Part Four35 pts

Part Five15 pts

Part Six25 pts

Total140 pts

PART ONE (10 points)

1.What is the output of the following code?

#include <stdio.h>

#include <string.h>

#define MAXSTRING 100

main()

{

char c=’a’, *p, s[MAXSTRING];

p=&c;

printf(“%c%c%c “, *p, *p+1, *p+2);

strcpy(s, “ABC”);

printf(“%s %c%c%s\n”,s, *s+6, *s+7,s+1);

strcpy(s,”she sells sea shells by the seashore”);

p=s+14;

for ( ; *p !=’\0’; ++p) {

if (*p == ‘e’)

*p=’E’;

if (*p == ‘ ‘)

*p=’\n’;

}

printf(“%s\n”, s);

}

abc ABC GHBC

she sells sea shElls

by

thE

sEashorE

PART TWO (35 points). Each question is worth 3 (+/-) points. Short Answers.

2. How is the char ‘e’ represented in the computer?

char e =101 base 10 = 65 base 8 = 01100101 base 2.

How is the logical value ‘true’ represented in the computer?

Any non-zero value.

How is the int ‘256’ represented in the computer?

256 base 10 = 0100 base 16 = 00000001 00000000 base 2.

How is the float ‘1.8’ represented in the computer?

1.8 base 10 = 1.2E07AE0 base 16 = 0 100 0000 0001 0111 0000 0011 1101 0111 = 401703D7 base 16

How is the double ‘1.8’ represented in the computer?

1.8 base 10 = 1.2E07AE0 base 16 =

How many bits are there in a byte?

8.

What is the significance of a byte?

A byte stores one char.

How many bits are there in a word?

32.

What is the general rule of casts?

Data type conversions are made to maintain the level of accuracy of the most accurate term in an

operation. IOW, convert int to float if one of the operands is an int and the other is a float.

What is meant by ‘scope’?

Scope is the term used to describe the set of C instructions which have access to a variable, or identifier.

Variables are accessible within the code blocks within which they are declared. Extern variables are

available everywhere.

Define each storage class in C?

auto storage class – variables declared within function bodies.

extern storage class –variables declared outside function bodies are accessible to all functions following

that declaration.

register storage class – suggests variables to the compiler to be placed in high speed registers.

static storage class – auto class variables which retain their previous values when a code block is

exited/reentered.

PART THREE (20 points). Each question is worth 5 (+/-) points. Short Answers.

3. What are the forms of information storage in a “normal” digital computer?

Integer – C ints.

Real – C floats/doubles.

Character – C chars.

4. Given a 12x4 array A, in C, what is the linear sequential element number of A[9] [3]?

(9-1)(4) + 3 = 35th element.

5. What do you know about:

a)Recursion?

Recursion is a function invoking itself. Most recursive forms can be written in an equivalent iterative

form. Useful on internally looping processes such as factorial computations, it also can be a dangerous

practice.

b)structs?

A struct allows a programmer to aggregate individually named variables, or members, into a single

named variable.

PART FOUR (35 points). Each question is worth 2 points.

6. Fill in the table below with the correct values for each of the statements. (Assume initialization as shown.)

If an expression is not valid, indicate this in the value column.

char *t[2][3] = {“abc”, “defg”, “hi”,

“jklmno”, “pqrstuvw”, “xyz”};

int i=2, j=3, k=4, m=5, *p=&i, *q=&j ,*r;

int a=2, b=3, c=4;

double x=2.0;

Expression / Value
i += j + k / 9
j *= k = m + 5 / 30
a > b & c < d / Invalid (no ‘d’)
a < != b || !! a / Invalid (!=)
a + b < ! C + c / 0 or Invalid (no C declared)
a – x || b * c & b / a / 1
p == & i / 1
p = i + 9 / invalid
* * & p / 9 or 2
r = & x / invalid
9 * * p / * q + 5 / 7
***t / 97 (a)
**t[1] / 106 (j)
**(t[1] + 2) / 120 (x)
*(*(t + 1) + 1)[7] / 64 (@)
(*(*(t + 1) + 1))[7] / 119 (w)
*(t[1][2] + 2) / 122 (z)

PART FIVE (15 points).

  1. Write a C function main() to interactively input a set of positive numbers from the user (one at a time), calculate the average of the numbers, and print it out. The function should allow the user to continue entering numbers until a negative number is entered, at which time the function will terminate.

#include <stdio.h>

int main(void)

{

float n = 0. ,x = 1.0, sumx = 0., average;

while (x >= 0.)

{

printf("input a number... negative to terminate... ");

scanf("%f", &x);

if (x >= 0.)

{

n = n + 1.0;

sumx = sumx + x;

}

}

average = sumx/n;

printf("\n\nThe average of %3.0f input numbers is %10.3f\n", n, average);

}

input a number... negative to terminate...

input a number... negative to terminate...

input a number... negative to terminate...

input a number... negative to terminate...

The average of 3 input numbers is 4.000

PART FOUR (25 points).

  1. Given the C statement sd = stddev(a,n); . Write the function‘stddev’ to calculate the standard deviation of the n elements in the real array a.The equation for the standard deviation is:

sd = Note:

float stddev(float a[], int n);

int main(void)

{

int n=3;

float a[10]={3.0, 4.0, 5.0},sd;

sd = stddev(a, n);

printf("The Standard Deviation of the %d elements in array a is %10.3f\n\n", n, sd);

return(0);

}

float stddev(float a[], int n)

{

int i;

float suma=0.,sumasq=0.,num,sd;

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

{

suma += a[i];

sumasq += a[i]*a[i];

}

num = sumasq - suma*suma/n;

sd = sqrt(num/(n-1));

return(sd);

}

The Standard Deviation of the 3 elements in array a is 1.000

1