CSCI 152: Introduction to Computer Science with C

Spring 2008Quiz 1

NAME:______

Date: Monday, February 4, 2008

Duration: 50 minute

Open notes, open books

Quiz 1consists of Two Parts

  • Part I - 60 points – 3 short questions
  • Part II - 40 points - 2 open question
  • In the open questions no comments are required

PART I (60 points)

Question 1 (20 points):

  1. What is the output of the following code fragment?
  2. How many parameters the function guess has?
  3. What is the type of the return value of the function guess?

#include < stdio.h >

double guess (int a, int b, int c);

int main( ){

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

printf("%f\n", guess(a, b, c));

return 0;

}

double guess (int a, int b, int c){

double result;

if ( a > b){

result = (a + b + c)/3;

}

else if ((c <= b) & (a%2 != 0)){

result = (a + b + c)/3.0;

}

else {

result = (a*b*c)/3.0;

}

return result;

}

Write your answer here:

Question 2 (20 points):

What is the output of the following code fragment?

#include < stdio.h >

int guess (int n, int k, char c);

int main( ){

int num1 = 4, num2 = 7;

char c = ‘b’;

int result = guess (num1, num2, c);

printf (“%d\n”, result);

return 0;

}

int guess (int n, int k, char c){

int i, res;

if (n > k){

for (i =0; i < k; i++){

printf (“%c”, c);

}

printf(“\n”);

res = n;

}

else{

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

printf (“%c”, c);

}

printf(“\n”);

res = k;

}

return res;

}

Write your answer here:

Question 3 (20 points):

What is the output of the following code fragment?

#include < stdio.h >

void guess ( void );

int main( ){

int a = 0;

while ( a < 4 ){

guess ( );

printf(“\n”);

a ++;

}

return 0;

}

void guess ( void ) {

int k = 8;

if (!k){

printf (“yana”);

}

else{

printf(“hello”);

}

}

Write your answer here:

Part II (40 points):

Problem 1 (20 points):

Write a function int square (int a) that calculates and returns square of a. For example, if the value of parameter a is 5, the function returns 25, and if the value of parameter a is 3 the function returns 9. Write a C program, that reads a sequence of non-zero integers. First zero input integer terminates the input. The program uses function square to print the square of each input number. The output should be printed on different lines.

Problem 2 (20 points)

Write a function float area (float radius) that returns the area of the circle with the radius that equals to the value of the parameter radius

(use the formula: area = 3.14 * radius* radius).

Write a C Program that reads 10 float numbers. Each positive number indicates the radius of the circle and program prints the area of these circles. If the input number is zero or negative the program prints the error message.