Docsity

Practice Quiz #6 - Programming Using C | CGS 3460, Quizzes of Computer Science

Toggle sidebar
bg1
CGS 3460 Quiz 6 Name:______________________
April 24, 2006 UF ID: _____________________
Gatorlink ID: ________________
Sign your name in the honor code below if you agree and wish to receive a grade for this quiz.
“On my honor, I have neither given nor received unauthorized aid in completing this
quiz.” Signature (attest honor code): ______________________________
Before turn in your answer sheets, make sure you sign your initials on top of each page.
1. Linked List. Given the following structure definition, you’re asked by your
supervisor to implement a linked list that sort the elements in ascending order based
on the completion time of the athletes participating in a downhill skiing competition.
(50%)
a) Define a structure athlete to support this linked list. Each node should include at
least the athlete’s name and completion_time. (8%)
struct athlete {
char name[NAME_LEN+1];
float completion_time;
struct athlete *next;
};
b) How to create a new node for Jimmy who completes the event in 310.25
seconds? (8%)
struct athlete *new_node;
new_node = malloc(sizeof(struct athlete));
strcpy(new_node->name,“Jimmy”);
new_node->completion_time = 310.25;
new_node->next = NULL;
c) Given an established list struct athlete * list and a new node struct
athlete * new_node that has the smallest completion time, how do you insert
the new_node to the list? (8%)
new_node->next = list;
list = new_node;
d) Given an established list struct athlete * list, how to use for loop to print
out all the completion_time of every participating athletes, one entry per line?
(8%)
Page 1/4
pf3
pf4
pf5

Partial preview of the text

Download Practice Quiz #6 - Programming Using C | CGS 3460 and more Quizzes Computer Science in PDF only on Docsity!

CGS 3460 Quiz 6 Name:______________________ April 24, 2006 UF ID: _____________________ Gatorlink ID: ________________ Sign your name in the honor code below if you agree and wish to receive a grade for this quiz. “ On my honor, I have neither given nor received unauthorized aid in completing this quiz. ” Signature (attest honor code): ______________________________ Before turn in your answer sheets, make sure you sign your initials on top of each page.

  1. Linked List. Given the following structure definition, you’re asked by your supervisor to implement a linked list that sort the elements in ascending order based on the completion time of the athletes participating in a downhill skiing competition. (50%) a) Define a structure athlete to support this linked list. Each node should include at least the athlete’s name and completion_time. (8%) struct athlete { char name[NAME_LEN+1]; float completion_time; struct athlete *next; }; b) How to create a new node for Jimmy who completes the event in 310. seconds? (8%) struct athlete *new_node; new_node = malloc(sizeof(struct athlete)); strcpy(new_node->name,“Jimmy”); new_node->completion_time = 310.25; new_node->next = NULL; c) Given an established list struct athlete * list and a new node struct athlete * new_node that has the smallest completion time, how do you insert the new_node to the list? (8%) new_node->next = list; list = new_node; d) Given an established list struct athlete * list, how to use for loop to print out all the completion_time of every participating athletes, one entry per line? (8%)

struct athlete *p; for(p=list; p!=NULL; p=p->next) printf(“%f \n”, p->completion_time); e) Given struct athlete * node, which operator allows you to refer to the completion time? (6%) Answer: 3  node.time  nodetime  nodetime  node(time)  none of the above f) Given struct athlete node, which operator allows you to refer to the completion time? (6%) Answer: 1  node.time  nodetime  nodetime  node(time)  none of the above g) Which is of the below is not a benefit of using a linked list instead of an array? (6%) Answer: 3  Linked list is more flexible  Linked list allow you sort the elements more easily  Linked list allow you to randomly access one of its elements  Linked list can grow and shrink as needed  All of the above are its benefits

How many times fibonacci function would be called before this program terminates? __ 3 _

  1. Identify the 5 errors in the following c program. Mark 1 – 5 right next to the error in the code segment, and give a one-line explanation corresponding to each marked number on the space provided. If there are multiple instances of the same type of error, they will only be counted as one error. You should assume you’re compiling and executing this code on grove. You should consider improper usages of certain statement an error even though they may not be picked up by the compiler. (20%) #include <stdio.h> #include <string.h> main(int argc, char ** argv) { #define STRING_LENGTH = 8; /* 1 / FILE * output = fopen(argv[1], “r”); / 2 / int i; struct member { int id; char name[STRING_LENGTH]; / 3 / }; char header[30]; header = “Paid Member List\n”; / 4 / member paidMembers[2] = {{101, “Robinson”}, {102, “Davidson”}}; / 5*/ fprintf(output, “%s\n”, header); for (i = 0; i < 2; i++) { fprintf(output, “Member Id: %5d\t Name: %8s\n”, paidMembers[i].id, paidMembers[i].name); } fclose(output); }
    1. It should be written as #define STRING_LENGTH 8
    2. Open the file from command line for writing instead of reading. It should be “w”.
    3. String length should be STRING_LENGTH+1; 4 ) String assignment is wrong. It should be strcpy(header,"Paid Member List\n"); 5 ) It should be struct member paidMembers[2]