Difference between revisions of "CSC231 Final Exam Fall 2017"
(→Problem 3: Bash Script) |
|||
(21 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | + | ||
<bluebox> | <bluebox> | ||
− | <font color="purple">This exam is one-week, take-home, and is due on December | + | <font color="purple">This exam is one-week, take-home, and is due on December 20, at '''11:55 p.m.''' It must be done individually.</font> |
+ | <br /> | ||
+ | It is open books, open notes, and open Web. | ||
+ | <br /> | ||
+ | There won't be any office hours or TA hours during the exam period. | ||
<br /> | <br /> | ||
This exam is given under the rules of the ''honor code''. '''It has to be done individually'''. You have access to all your notes, to books, and to the Web. You cannot, however, discuss the details of the exam with anybody other than your instructor. <font color="magenta">'''Questions regarding the exam can only be asked in class, or using Piazza'''</font>. Do '''not post code''' on Piazza. Do not suggest or imply possible solutions in your posts on Piazza. | This exam is given under the rules of the ''honor code''. '''It has to be done individually'''. You have access to all your notes, to books, and to the Web. You cannot, however, discuss the details of the exam with anybody other than your instructor. <font color="magenta">'''Questions regarding the exam can only be asked in class, or using Piazza'''</font>. Do '''not post code''' on Piazza. Do not suggest or imply possible solutions in your posts on Piazza. | ||
<br /> | <br /> | ||
− | All | + | All six problems are worth the same number of points (16.6/100). |
<br /> | <br /> | ||
<font color="magenta">'''If you use material not in the class Web page or the on-line textbook we used for class, you need to list references to them in the header of your program.'''</font> | <font color="magenta">'''If you use material not in the class Web page or the on-line textbook we used for class, you need to list references to them in the header of your program.'''</font> | ||
Line 14: | Line 18: | ||
* Get a fresh copy of 231Lib.asm.. | * Get a fresh copy of 231Lib.asm.. | ||
* Make a copy of it under the name 231Lib2.asm | * Make a copy of it under the name 231Lib2.asm | ||
− | * Add a new function to 231Lib2.asm called | + | * Add a new function to 231Lib2.asm called _printCow() that gets a message in ecx, edx (similarly to _printString), and that prints a message with a cow, similarly to the bash command cowsay. See below for an example. |
− | * The message | + | * The message will always be at most 80 characters, and at least 1 character in length, and will be printed on one line only. Your program will not be tested with message shorter than 1 and longer than 80 characters. |
+ | * Use the Linux '''cowsay''' command to see examples of outputs (/usr/games/cowsay your message). | ||
+ | <br /> | ||
::<source lang="text"> | ::<source lang="text"> | ||
cs231a@aurora ~ $ /usr/games/cowsay Hello CSC231 | cs231a@aurora ~ $ /usr/games/cowsay Hello CSC231 | ||
Line 21: | Line 27: | ||
< Hello CSC231 > | < Hello CSC231 > | ||
-------------- | -------------- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
\ ^__^ | \ ^__^ | ||
\ (oo)\_______ | \ (oo)\_______ | ||
Line 36: | Line 33: | ||
|| || | || || | ||
cs231a@aurora ~ $ | cs231a@aurora ~ $ | ||
+ | |||
+ | </source> | ||
+ | * You should get the same output as shown above if you do this in your test program: | ||
+ | <br /> | ||
+ | ::<source lang="asm"> | ||
+ | Hello db "Hello CSC231" | ||
+ | HelloLen equ $-Hello | ||
+ | |||
+ | |||
+ | mov ecx, Hello | ||
+ | mov edx, HelloLen | ||
+ | call _printCow | ||
+ | |||
</source> | </source> | ||
+ | <br /> | ||
* Your function should not modify any of the registers. | * Your function should not modify any of the registers. | ||
* You are allowed to add other functions to your 231Lib2.asm file. | * You are allowed to add other functions to your 231Lib2.asm file. | ||
Line 44: | Line 55: | ||
You need to submit 231Lib2.asm on Moodle. It will be tested with a separate main program. | You need to submit 231Lib2.asm on Moodle. It will be tested with a separate main program. | ||
<br /> | <br /> | ||
+ | <br /><br /> | ||
+ | <br /><br /> | ||
=Problem 2: C function= | =Problem 2: C function= | ||
<br /> | <br /> | ||
− | Write a C program and its header file containing a function called zap2() that will work similarly to the zap() function of Homework 9, but will replace '''all''' occurrences of the second string in the first string. The C program should be called '''final.c''' and the header file '''final.h'''. | + | Write a C program and its ''header file'' containing a function called zap2() that will work similarly to the zap() function of Homework 9, but will replace '''all''' occurrences of the second string in the first string. The C program should be called '''final.c''' and the header file '''final.h'''. |
''zap2()'' will return 1 if one or more substitutions occur, and 0 otherwise. Note that zap2() does '''not''' return the number of substitutions, just 1 or 0. | ''zap2()'' will return 1 if one or more substitutions occur, and 0 otherwise. Note that zap2() does '''not''' return the number of substitutions, just 1 or 0. | ||
− | For example, zap2( "Mississippi", "ss" ) will change the first string in "Mi--i-- | + | For example, zap2( "Mississippi", "ss" ) will change the first string in "Mi--i--ippi", and will return 1. zap2( "Mississipi", "tt" ) will not change the first string and will return 0. |
You may add other helper function(s) in your final.c and final.h files. Your C file should '''not''' contain a main function, as it will be linked with a test program that will contain main(). | You may add other helper function(s) in your final.c and final.h files. Your C file should '''not''' contain a main function, as it will be linked with a test program that will contain main(). | ||
Line 58: | Line 71: | ||
<br /> | <br /> | ||
* Submit your file in the Problem 3 section of the final exam on Moodle. | * Submit your file in the Problem 3 section of the final exam on Moodle. | ||
+ | <br /> | ||
+ | ==Solution Zap Function from HW 9== | ||
+ | <br /> | ||
+ | ::<source lang="C"> | ||
+ | int zap( char *s1, char *s2 ) { | ||
+ | int i, ret=0; | ||
+ | char *p = strstr( s1, s2 ); | ||
+ | if ( p!=NULL ) { | ||
+ | ret = 1; | ||
+ | for ( i=0; i<strlen( s2 ); i++ ) | ||
+ | *(p++) = '-'; | ||
+ | } | ||
+ | return ret; | ||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | <br /><br /> | ||
+ | <br /><br /> | ||
<br /> | <br /> | ||
+ | |||
=Problem 3: Bash Script= | =Problem 3: Bash Script= | ||
<br /> | <br /> | ||
− | * Write a bash script called '''finalFact.sh''' that contains a ''recursive'' function called '''fact''' that will compute the '''factorial''' of a number passed on the command line. | + | * Write a bash script called '''finalFact.sh''' that contains a ''recursive'' function called '''fact''' that will compute the '''factorial''' of a number passed on the command line. |
− | |||
<br /> | <br /> | ||
Line 73: | Line 104: | ||
cs231a@aurora ~/handout $ '''./finalFact.sh 6''' | cs231a@aurora ~/handout $ '''./finalFact.sh 6''' | ||
fact( 6 ) = 208 | fact( 6 ) = 208 | ||
− | cs231a@aurora ~/handout $ ''' | + | cs231a@aurora ~/handout $ '''./finalFact.sh ''' |
− | >''' ./finalFact.sh $i | + | Syntax: ./funcMoodleChallenge.sh n |
+ | where n is an integer | ||
+ | cs231a@aurora ~/handout $ '''for i in 1 2 3 4 5 6 7 8 9 10 ; do''' | ||
+ | >''' ./finalFact.sh $i ''' | ||
> '''done''' | > '''done''' | ||
fact( 1 ) = 1 | fact( 1 ) = 1 | ||
fact( 2 ) = 2 | fact( 2 ) = 2 | ||
fact( 3 ) = 6 | fact( 3 ) = 6 | ||
+ | fact( 4 ) = 24 | ||
fact( 5 ) = 120 | fact( 5 ) = 120 | ||
fact( 6 ) = 208 | fact( 6 ) = 208 | ||
+ | fact( 7 ) = 176 | ||
+ | fact( 8 ) = 128 | ||
+ | fact( 9 ) = 128 | ||
fact( 10 ) = 0 | fact( 10 ) = 0 | ||
+ | <br /> | ||
+ | ==Output Format== | ||
+ | <br /> | ||
+ | Your script should include only one echo, of this form: | ||
+ | <br /> | ||
+ | ::<source lang="bash"> | ||
+ | echo "fact( $n ) = $?" | ||
+ | |||
+ | </source> | ||
<br /> | <br /> | ||
==Question == | ==Question == | ||
<br /> | <br /> | ||
− | In the header of your program, explain the program returns 0 for fact(10). If you are not sure, suggest the most likely reason. | + | In the header of your program, explain why the program returns 0 for fact(10). If you are not sure, suggest the most likely reason. |
<br /> | <br /> | ||
==Submission== | ==Submission== | ||
Line 91: | Line 138: | ||
Submit your program in the Problem 3 section on Moodle. | Submit your program in the Problem 3 section on Moodle. | ||
<br /> | <br /> | ||
+ | <br /><br /> | ||
+ | <br /><br /> | ||
+ | |||
=Problem 4: Fixed-Point Formats= | =Problem 4: Fixed-Point Formats= | ||
<br /> | <br /> | ||
Answer the questions in the Problem 4 section of the final exam on Moodle. | Answer the questions in the Problem 4 section of the final exam on Moodle. | ||
<br /> | <br /> | ||
+ | <br /><br /> | ||
+ | <br /><br /> | ||
+ | =Problem 5: Floating-Point Format= | ||
+ | <br /> | ||
+ | Assume that we want to create a 16-bit floating point format similar to the IEEE floating point format we have seen in class. | ||
+ | This format uses all the rules we have studied as being intrinsic to the 32-bit format, including the ''exceptions'', and the ''biased exponent''. | ||
+ | <br /> | ||
+ | In this 16-bit format, the sign, exponent, and mantissa are defined as follows: | ||
+ | <br /> | ||
+ | * The most significant bit is the sign bit: 0 for positive, 1 for negative | ||
+ | * The next 4 bits are the exponent. The stored exponent is offset by a ''bias'' relative to the true exponent. | ||
+ | * The lower 11 bits are the mantissa, and we assume that the mantissa contains a hidden 1. All numbers, as with the 32-bit format, are normalized as 1.bbb...bbb x 2^(true exponent). | ||
+ | <br /> | ||
+ | ==Questions== | ||
+ | <br /> | ||
+ | Section 5 of the Final Exam section on Moodle contains several questions relating to this format. | ||
+ | <br /><br /> | ||
+ | <br /><br /> | ||
+ | =Problem 6: Linked Lists in C= | ||
+ | <br /> | ||
+ | Your assignment is to complete the function ''makeList()'' in the program below, so that it outputs the correct information. Several examples of how the finished program should work are given below. | ||
+ | <br /> | ||
+ | ==Incomplete Program== | ||
+ | <br /> | ||
+ | ::<source lang="C"> | ||
+ | // makeList.c | ||
+ | // D. Thiebaut | ||
+ | // program to complete for CSC231, Final Exam | ||
+ | // | ||
+ | // Example: | ||
+ | // gcc -o makeList makeList.c | ||
+ | // ./makeList 100 200 300 1000 | ||
+ | // 1 | ||
+ | // 2 | ||
+ | // -5 | ||
+ | // 10 | ||
+ | // 20 | ||
+ | // -30 | ||
+ | // 5 | ||
+ | // | ||
+ | // 100 | ||
+ | // 200 | ||
+ | // 300 | ||
+ | // 1000 | ||
+ | // | ||
+ | // | ||
+ | #include <stdio.h> | ||
+ | #include <stdlib.h> | ||
+ | #include <ctype.h> | ||
+ | |||
+ | //--- structure used to implement the node --- | ||
+ | //--- of the linked-list --- | ||
+ | struct node { | ||
+ | int item; | ||
+ | struct node* next; | ||
+ | }; | ||
+ | typedef struct node node_t; | ||
+ | |||
+ | |||
+ | //-------------------------------------------------- | ||
+ | // makeList( array, arraySize ): takes the contents | ||
+ | // of the array and stores it, in the same order, in | ||
+ | // a singly linked list. | ||
+ | //-------------------------------------------------- | ||
+ | node_t *makeList( int* table, int n ) { | ||
+ | |||
+ | // add your code here... | ||
+ | |||
+ | return NULL; // you probably want to remove this line! | ||
+ | } | ||
+ | |||
+ | //-------------------------------------------------- | ||
+ | int main( int argc, char *argv[] ) { | ||
+ | int i, x, A[] = { 1, 2, -5, 10, 20, -30, 5 }; | ||
+ | int *B; | ||
+ | |||
+ | //--- make a linked list with A and print it in same order --- | ||
+ | node_t *p, *head = makeList( A, sizeof(A)/sizeof(int) ); | ||
+ | |||
+ | for ( p=head; p!=NULL; p=p->next ) { | ||
+ | printf( "%d\n", p->item ); | ||
+ | } | ||
+ | |||
+ | |||
+ | //--- if user entered ints on the command line, put them --- | ||
+ | //--- in array B and put B in linked list. Then print --- | ||
+ | //--- linked list. --- | ||
+ | if ( argc > 1 ) { | ||
+ | printf( "\n" ); | ||
+ | |||
+ | //--- create array B with same number of cells as ints on command line--- | ||
+ | B = (int *) malloc( (argc-1) * sizeof( int ) ); | ||
+ | |||
+ | //--- convert strings taken from command line in true integers --- | ||
+ | for ( i=1; i<argc; i++ ) { | ||
+ | B[i-1] = atoi( argv[i] ); | ||
+ | //printf( "B[%d] = %d\n", i-1, B[i-1] ); | ||
+ | } | ||
+ | |||
+ | //--- make linked list out of B, and print linked list --- | ||
+ | head = makeList( B, argc-1 ); | ||
+ | for ( p=head; p!=NULL; p=p->next ) { | ||
+ | printf( "%d\n", p->item ); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | <br /> | ||
+ | ==Examples== | ||
+ | <br /> | ||
+ | ::<source lang="text"> | ||
+ | cs231a@aurora ~ $ gcc -o makeList makeList.c | ||
+ | cs231a@aurora ~ $ ./makeList | ||
+ | 1 | ||
+ | 2 | ||
+ | -5 | ||
+ | 10 | ||
+ | 20 | ||
+ | -30 | ||
+ | 5 | ||
+ | cs231a@aurora ~ $ ./makeList 100 200 300 1000 | ||
+ | 1 | ||
+ | 2 | ||
+ | -5 | ||
+ | 10 | ||
+ | 20 | ||
+ | -30 | ||
+ | 5 | ||
+ | 100 | ||
+ | 200 | ||
+ | 300 | ||
+ | 1000 | ||
+ | cs231a@aurora ~ $ ./makeList 100 | ||
+ | 1 | ||
+ | 2 | ||
+ | -5 | ||
+ | 10 | ||
+ | 20 | ||
+ | -30 | ||
+ | 5 | ||
+ | |||
+ | 100 | ||
+ | cs231a@aurora ~ $ ./makeList hello there | ||
+ | 1 | ||
+ | 2 | ||
+ | -5 | ||
+ | 10 | ||
+ | 20 | ||
+ | -30 | ||
+ | 5 | ||
+ | |||
+ | 0 | ||
+ | 0 | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Submission== | ||
+ | <br /> | ||
+ | * Submit your code on Moodle, in the Problem 6 section. | ||
+ | <br /> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | <!-- ====================================================================== --> | ||
+ | <!-- ====================================================================== --> | ||
+ | <!-- ====================================================================== --> | ||
+ | |||
+ | |||
+ | <onlydft> | ||
=Solutions= | =Solutions= | ||
==Bash Script== | ==Bash Script== |
Latest revision as of 21:13, 15 December 2017
This exam is one-week, take-home, and is due on December 20, at 11:55 p.m. It must be done individually.
It is open books, open notes, and open Web.
There won't be any office hours or TA hours during the exam period.
This exam is given under the rules of the honor code. It has to be done individually. You have access to all your notes, to books, and to the Web. You cannot, however, discuss the details of the exam with anybody other than your instructor. Questions regarding the exam can only be asked in class, or using Piazza. Do not post code on Piazza. Do not suggest or imply possible solutions in your posts on Piazza.
All six problems are worth the same number of points (16.6/100).
If you use material not in the class Web page or the on-line textbook we used for class, you need to list references to them in the header of your program.
Contents
Problem 1: Assembly Cow
- Get a fresh copy of 231Lib.asm..
- Make a copy of it under the name 231Lib2.asm
- Add a new function to 231Lib2.asm called _printCow() that gets a message in ecx, edx (similarly to _printString), and that prints a message with a cow, similarly to the bash command cowsay. See below for an example.
- The message will always be at most 80 characters, and at least 1 character in length, and will be printed on one line only. Your program will not be tested with message shorter than 1 and longer than 80 characters.
- Use the Linux cowsay command to see examples of outputs (/usr/games/cowsay your message).
cs231a@aurora ~ $ /usr/games/cowsay Hello CSC231 ______________ < Hello CSC231 > -------------- \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || || cs231a@aurora ~ $
- You should get the same output as shown above if you do this in your test program:
Hello db "Hello CSC231" HelloLen equ $-Hello mov ecx, Hello mov edx, HelloLen call _printCow
- Your function should not modify any of the registers.
- You are allowed to add other functions to your 231Lib2.asm file.
Submission
You need to submit 231Lib2.asm on Moodle. It will be tested with a separate main program.
Problem 2: C function
Write a C program and its header file containing a function called zap2() that will work similarly to the zap() function of Homework 9, but will replace all occurrences of the second string in the first string. The C program should be called final.c and the header file final.h.
zap2() will return 1 if one or more substitutions occur, and 0 otherwise. Note that zap2() does not return the number of substitutions, just 1 or 0.
For example, zap2( "Mississippi", "ss" ) will change the first string in "Mi--i--ippi", and will return 1. zap2( "Mississipi", "tt" ) will not change the first string and will return 0.
You may add other helper function(s) in your final.c and final.h files. Your C file should not contain a main function, as it will be linked with a test program that will contain main().
Submission
- Submit your file in the Problem 3 section of the final exam on Moodle.
Solution Zap Function from HW 9
int zap( char *s1, char *s2 ) { int i, ret=0; char *p = strstr( s1, s2 ); if ( p!=NULL ) { ret = 1; for ( i=0; i<strlen( s2 ); i++ ) *(p++) = '-'; } return ret; }
Problem 3: Bash Script
- Write a bash script called finalFact.sh that contains a recursive function called fact that will compute the factorial of a number passed on the command line.
cs231a@aurora ~/handout $ ./finalFact.sh 4 fact( 4 ) = 24 cs231a@aurora ~/handout $ ./finalFact.sh 5 fact( 5 ) = 120 cs231a@aurora ~/handout $ ./finalFact.sh 6 fact( 6 ) = 208 cs231a@aurora ~/handout $ ./finalFact.sh Syntax: ./funcMoodleChallenge.sh n where n is an integer cs231a@aurora ~/handout $ for i in 1 2 3 4 5 6 7 8 9 10 ; do > ./finalFact.sh $i > done fact( 1 ) = 1 fact( 2 ) = 2 fact( 3 ) = 6 fact( 4 ) = 24 fact( 5 ) = 120 fact( 6 ) = 208 fact( 7 ) = 176 fact( 8 ) = 128 fact( 9 ) = 128 fact( 10 ) = 0
Output Format
Your script should include only one echo, of this form:
echo "fact( $n ) = $?"
Question
In the header of your program, explain why the program returns 0 for fact(10). If you are not sure, suggest the most likely reason.
Submission
Submit your program in the Problem 3 section on Moodle.
Problem 4: Fixed-Point Formats
Answer the questions in the Problem 4 section of the final exam on Moodle.
Problem 5: Floating-Point Format
Assume that we want to create a 16-bit floating point format similar to the IEEE floating point format we have seen in class.
This format uses all the rules we have studied as being intrinsic to the 32-bit format, including the exceptions, and the biased exponent.
In this 16-bit format, the sign, exponent, and mantissa are defined as follows:
- The most significant bit is the sign bit: 0 for positive, 1 for negative
- The next 4 bits are the exponent. The stored exponent is offset by a bias relative to the true exponent.
- The lower 11 bits are the mantissa, and we assume that the mantissa contains a hidden 1. All numbers, as with the 32-bit format, are normalized as 1.bbb...bbb x 2^(true exponent).
Questions
Section 5 of the Final Exam section on Moodle contains several questions relating to this format.
Problem 6: Linked Lists in C
Your assignment is to complete the function makeList() in the program below, so that it outputs the correct information. Several examples of how the finished program should work are given below.
Incomplete Program
// makeList.c // D. Thiebaut // program to complete for CSC231, Final Exam // // Example: // gcc -o makeList makeList.c // ./makeList 100 200 300 1000 // 1 // 2 // -5 // 10 // 20 // -30 // 5 // // 100 // 200 // 300 // 1000 // // #include <stdio.h> #include <stdlib.h> #include <ctype.h> //--- structure used to implement the node --- //--- of the linked-list --- struct node { int item; struct node* next; }; typedef struct node node_t; //-------------------------------------------------- // makeList( array, arraySize ): takes the contents // of the array and stores it, in the same order, in // a singly linked list. //-------------------------------------------------- node_t *makeList( int* table, int n ) { // add your code here... return NULL; // you probably want to remove this line! } //-------------------------------------------------- int main( int argc, char *argv[] ) { int i, x, A[] = { 1, 2, -5, 10, 20, -30, 5 }; int *B; //--- make a linked list with A and print it in same order --- node_t *p, *head = makeList( A, sizeof(A)/sizeof(int) ); for ( p=head; p!=NULL; p=p->next ) { printf( "%d\n", p->item ); } //--- if user entered ints on the command line, put them --- //--- in array B and put B in linked list. Then print --- //--- linked list. --- if ( argc > 1 ) { printf( "\n" ); //--- create array B with same number of cells as ints on command line--- B = (int *) malloc( (argc-1) * sizeof( int ) ); //--- convert strings taken from command line in true integers --- for ( i=1; i<argc; i++ ) { B[i-1] = atoi( argv[i] ); //printf( "B[%d] = %d\n", i-1, B[i-1] ); } //--- make linked list out of B, and print linked list --- head = makeList( B, argc-1 ); for ( p=head; p!=NULL; p=p->next ) { printf( "%d\n", p->item ); } } }
Examples
cs231a@aurora ~ $ gcc -o makeList makeList.c cs231a@aurora ~ $ ./makeList 1 2 -5 10 20 -30 5 cs231a@aurora ~ $ ./makeList 100 200 300 1000 1 2 -5 10 20 -30 5 100 200 300 1000 cs231a@aurora ~ $ ./makeList 100 1 2 -5 10 20 -30 5 100 cs231a@aurora ~ $ ./makeList hello there 1 2 -5 10 20 -30 5 0 0
Submission
- Submit your code on Moodle, in the Problem 6 section.