Docsity

CS 1371 and CS 1372 Computing Practice Test - Fall Semester 2006 - Prof. David Smith, Exams of Computer Science

Partial preview of the text

Download CS 1371 and CS 1372 Computing Practice Test - Fall Semester 2006 - Prof. David Smith and more Exams Computer Science in PDF only on Docsity!

Georgia Institute of Technology

College of Computing

CS 1371 Computing for Engineers

Test 2 Practice Test - Fall Semester 2006

Use this practice test and the book questions for chapters 1-

excluding Sections 7.3 and 11.6.

There is some measurable probability that the real test will contain one

of these problems as written.

Problem 1 โ€“ Short Answer

a) the following questions refer to these declarations:

typedef struct { char first[20]; char last[20]; int score; char grade; } Student;

Student stu1, stu2;

Now consider the following statements. After each one, write either valid or the reason for

being invalid:

(i) stu2 = stu1; // valid

(ii) if(stu1 == stu2) printf("same structures"); // valid

(iii) if(stu1.score == stu2.grade) printf("nonsense"); // valid

(iv) printf("%d %d", stu1.score, stu1.grade); // invalid - %d for char

(v) printf("%s", stu1); // invalid - %s for structure

(vi) Student group[30]; // valid

b) answer these questions about recursive programming:

(i) which is generally more efficient โ€“ recursion or iteration? Iteration

(ii) What is wrong with this code: // recursive call doesnโ€™t return anything

int silly(int n) { if(n <= 0) return 1; else if(n%2 == 0) return n; else return silly(n - 3); }

Mark the change(s) necessary to fix it.

(iii) What causes stack overflow? Recursion not terminated

(iv) If a recursive function looks like this:

if( /some condition/ ) /* make recursive call */ ;

What can you say about the algorithm? The terminating condition does nothing

Problem 2 โ€“ Tracing

Consider the following code fragment:

#include <string.h> #include <stdlib.h>

char first[20], last[20]; char name[] = "Burdell, George P."; strncpy(last, name, 2); last[2] = '\0'; printf("%s", last); printf("%s\n", strcpy(first, name + 9));

strncpy(last, name, 7); last[7] = '\0'; first[7] = '\0'; printf("%s%s\n", first, last);

a. will it compile without error? No โ€“ stdio not included.

b. if not, mark the error on the code above.

c. if so, write the output below: with stdio included,

BuGeorge P. George Burdell

Problem 3 โ€“ Coding โ€“ Strings

Complete the main program below with code that will read a line of code from the user.

Your code must traverse the string typed by the user and capitalize each word.

1. You may assume that words are separated by only one space, and consist of nothing

but lower case letters.

2. You capitalize a character by adding โ€˜Aโ€™-โ€˜aโ€™.

3. You may not import the string.h header.

#include <stdio.h>

int main() { char str[80];

char *sptr = gets(&str); // get a string from the user

if(sptr) (sptr) += 'A'-'a';

while(*sptr) {

if(*sptr == ' ') 1[sptr] += 'A'-'a';

sptr++;

Problem 5 โ€“ Coding - Vectors

You are given the definition of a structure called vector containing the size of a vector, an int,

and a double pointer containing a pointer to a one dimensional array.

a. Write the definition of this structure.

// your code here

NOT NEEDED

b. Write the function linspace that consumes two doubles, the starting value and the

ending value, and an int, the number of elements. It returns a vector of the given size with

the values uniformly distributed between the starting and ending values.

// your code here

NOT NEEDED

c. Write a test main for your linspace function that uses linspace to create a vector with 11

values between 0 and 10. Your program should not leak memory, and should check that the

vector has the right length, and the first and last values.

Hints: 1. can you compare double values exactly?

2. you may not assume that a function exists to free a vector.

// your code here

NOT NEEDED

Problem 6 โ€“ Coding - Matrices

You are given the definition of a structure called matrix containing the number of rows and

columns, both ints, and a double ** pointing to the data.

a. Write the definition of this structure.

// your code here

NOT NEEDED

b. Write the function diag that consumes a matrix and returns a vector containing the

diagonal elements of the matrix.

// your code here

NOT NEEDED

c. Complete the following test main for your diag function by calling diag to extract the

diagonal from the matrix m and then verifying that the vector does contain the diagonal

values.

int main() { matrix m = buildRandSquarerMatrix(); // builds a square matrix

NOT NEEDED

free(m); // frees the memory for m }