CSCI 151: Introduction to Computer Science with C

Total Page:16

File Type:pdf, Size:1020Kb

CSCI 151: Introduction to Computer Science with C

CSCI 152: Introduction to Computer Science with C Spring 2009 Quiz 1 Preparation

 Date: Friday, January 30, 2009  Duration: 50 minute  Open notes, open books  Quiz 1 consists of Two Parts

 Part I - 60 points – 3 short questions  Part II - 40 points - 2 open question  In the open questions you would need to write a short code fragment, without the header comments, in order to save your time.

Question 1

a. What is the output of the following code fragment? b. How many parameters the function guess has? c. What is the type of the return value of the function guess?

#include < stdio.h > double guess (int a, int b); int main( ){ int a = 6, b = 7; printf("%f\n", guess(a, b)); return 0; }

double guess (int a, int b){ double result; if ( a < b){ result = (a+b)/2; } else { result = (a+b)/2.0; } return result; }

Write your answer here: Question 2 What is the output of the following code fragment, if the input was AbCd? In this question you would need to use the ASCII table. The ASCII table is attached to the quiz.

#include < stdio.h > void guess (char c);

int main( ){ int i; char letter; for (i = 1; i <= 4; i++){ letter = getchar(); guess (letter); } return 0; }

void guess (char c){ int i, res; res = ((int) c) % 10;

for (i =0; i < res; i++){ printf (“%c”, c); } }

Write your answer here: Question 3

What is the output of the following code fragment?

#include < stdio.h > int guess ( char c, char b); int main( ){ char c = ‘B’; char b = ‘a’; int a = 0; while ( a < 4 ){ printf(“%d\n”, guess(c,b)); a ++; } return 0; }

int guess ( char c, char b) {

int res;

if ( ( (c >= ’A’) && ( c <= ’Z’) ) && ( ( b >= ’A’) && (b <=’Z’) ) ) res = 1;

else{ res = 0; }

return res;

}

Write your answer here: Question 4 : a. What is the output of the following code? b. How many parameters the function fun has? c. What is the type of the return value of the function fun? d. What function fun is doing?

#include < stdio.h > void fun (char c, int n); int main( ){ char c = 'b'; int i; for (i = 0; i < 3; i ++){ fun (c, i); printf ("\n"); } return 0; }

void fun(char c, int n){ int i; for (i = 0; i < n; i ++){ printf ("%c", c); }

}

Write your answer here:

Question 5: What is the output of the following code fragment? #include < stdio.h > int fun (int a, int b); int main( ){ int a = 0, b = 4; printf("%d\t %d\t %d\n", fun( a, b), a, b); return 0; }

int fun(int a, int b){ int c;

if (a == 0 ){ a++; } if ( b == 0){ b++; }

return ( (a % b) && (a > b)) }

Write your answer here:

Part II (40 points): Problem 1 Write a function int twoToN(int n) that calculates and returns the value of 2^n, (2 in power of n), where n is the value of the function parameter. Write a C program that reads one integer K, and calculates the following sum: 2^0 + 2^1 + 2^2 +…+2^K. Use function twoToN to calculate each term of the sum. Pay attention, there are K+1 terms in the sum.

Problem 2 Write a function int counttEven (int n) that reads a sequence of n integers and finds and returns the amount of even numbers among input numbers. Write a C program, that reads one integer that indicates the amount of the input numbers. The program uses function countEven to find the amount of even numbers among input numbers. Question 3: Write a function int divisor (int n) that finds and returns the sum of all divisors of the parameter n. You can assume that n is positive. Write a C program that reads 5 integers. For each input number the program performs the following: if the input is zero or negative, the program prints an error message, and if the input is positive, the program prints the sum of all divisors of the input number. The program uses function divisor to calculate the sum of the divisors.

For example, if the input is 2, 6, 0, 8, 3

The output should be:

Sum of divisors of 2 is 3 Sum of divisors of 6 is 12 Error, please enter positive value The sum of divisors of 8 is 15 The sum of divisors of 3 is 4

Recommended publications