All 4 questions in the attachments. The answers should be in a word file.
CS221-3
C and Systems Programming
Page 2 of 5
Q.1)
25 points
The following program stores 80 bool values into a char arr[10]. Complete
setBool and getBool. In both functions, index is the index of the bool
values, from 0 to 79. getBool returns 1 if the bool value at index is true
otherwise 0.
Hint: You will probably need most bitwise operations including shifting.
void setBool ( char * arr , int index , int boolValue ) {
}
int getBool ( char * arr , int index ) {
}
int main ( void ) {
char arr [10];
memset ( arr , 0 , 10);
setBool ( arr , 78 , 1);
setBool ( arr , 40 , 0);
int b78 = getBool ( arr , 78); // b78 is 1
int b40 = getBool ( arr , 40); // b40 is 0
return 0;
}
Student’s name:
Please go on to the next page. . .
CS221-3
C and Systems Programming
Q.2)
Page 3 of 5
25 points
Given n points P1 (x1 , y1 , z1 ), P2 (x2 , y2 , z2 ) . . . Pn (xn , yn , zn ), implement the
function centerPoint() that finds the center point using the following formula:
x1 + x2 + . . . + xn y1 + y2 + . . . + yn z1 + z2 + . . . + zn
C(xc , yc , zc ) = (
,
,
)
n
n
n
centerPoint() takes three arguments: a pointer to an array of struct
point*, the number of points in the array, and a pointer to struct point
center. It then calculates the coordinates of the center point (for all the
points in the array) and stores it in struct point center. You may not
use any square brackets ([]) in centerPoint().
struct point {
float x ;
float y ;
float z ;
};
void centerPoint ( struct point ** points , int n ,
struct point * center ) {
}
int main ( void ) {
struct point center ;
struct point * points [100];
setPoints ( points , 100); // Dynamically allocates the points
// and initializes the points
centerPoint ( points , 100 , & center );
printf ( ” center point is (% f , %f , % f ) n , center .x , center .y , center . z );
return 0;
}
Student’s name:
Please go on to the next page. . .
CS221-3
C and Systems Programming
Page 4 of 5
Q.3)
25 points
Complete function reverseList, which takes the head and tail of a linked
list, and then reverses it. For example, the linked list, h → n1 → n2 → n3 →
t, after calling reverseList(&h, &t), becomes, t → n3 → n2 → n1 → h.
You can assume the list has at least three elements.
struct linkNode {
int value ;
struct linkNode * next ;
};
void reverseList ( struct linkNode * head ,
struct linkNode * tail ) {
}
Student’s name:
Please go on to the next page. . .
CS221-3
C and Systems Programming
Q.4)
Page 5 of 5
25 points
Implement a function wordCount, which reads a text file, and returns the
number of words in the file. You can assume the file only contains the
alphabet characters and the space character. However there may be more
than one space character between two words, so “abc abc†and “abc abcâ€Â,
for example, are both two words.
int wordCount ( const char * filename ) {
}
Student’s name:
End of exam
Purchase answer to see full
attachment