Difference between revisions of "CSC231 Final Exam Fall 2017"

From dftwiki3
Jump to: navigation, search
(Problem 3: Bash Script)
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
<onlydft>
 
  
* Have file of ints, and put together a bash file that reads 10 integers, these are indexes of lines to get in the file.
+
<bluebox>
Each line contains a number that in hex contains ASCII charsThis way we can have a multi-char message hidden in a
+
<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>
large file.
+
<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 />
 +
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 />
 +
All six problems are worth the same number of points (16.6/100).
 +
<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>
 +
</bluebox>
 +
<br /><br />
 +
<br /><br />
 +
=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).
 +
<br />
 +
::<source lang="text">
 +
cs231a@aurora ~ $ /usr/games/cowsay Hello CSC231
 +
______________
 +
< Hello CSC231 >
 +
--------------
 +
        \  ^__^
 +
        \  (oo)\_______
 +
            (__)\      )\/\
 +
                ||----w |
 +
                ||    ||
 +
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>
 +
<br />
 +
* Your function should not modify any of the registers.
 +
* You are allowed to add other functions to your 231Lib2.asm file.
 +
<br />
 +
==Submission==
 +
<br />
 +
You need to submit 231Lib2.asm on Moodle.    It will be tested with a separate main program.
 +
<br />
 +
<br /><br />
 +
<br /><br />
 +
 
 +
=Problem 2: C function=
 +
<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 stringThe 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().
 +
<br />
 +
==Submission==
 +
<br />
 +
* 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 );
  
=Bash Script=
+
  if ( p!=NULL ) {
 +
    ret = 1;
 +
    for ( i=0; i<strlen( s2 ); i++ )
 +
      *(p++) = '-';
 +
  }
 +
  return ret;
 +
}
 +
</source>
 +
<br />
 +
<br /><br />
 +
<br /><br />
 +
<br />
  
 +
=Problem 3: Bash Script=
 +
<br />
  
* Write a bash script that contains a ''recursive'' function called '''fact''' that will compute the '''factorial''' of a number passed on the command line.  Call this script '''funcMoodleChallenge.sh'''.
+
* 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.   
* Examples of how it should work:
 
 
<br />
 
<br />
 
   
 
   
  cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 4'''
+
  cs231a@aurora ~/handout $ '''./finalFact.sh 4'''
 
  fact( 4 ) = 24
 
  fact( 4 ) = 24
  cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 5'''
+
  cs231a@aurora ~/handout $ '''./finalFact.sh 5'''
 
  fact( 5 ) = 120
 
  fact( 5 ) = 120
  cs231a@aurora ~/handout $ '''./funcMoodleChallenge.sh 6'''
+
  cs231a@aurora ~/handout $ '''./finalFact.sh 6'''
 
  fact( 6 ) = 208
 
  fact( 6 ) = 208
  cs231a@aurora ~/handout $ '''for i '''in 1 2 3 5 6 10 ; do'''  
+
  cs231a@aurora ~/handout $ '''./finalFact.sh '''
  >''' ./funcMoodleChallenge.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 />
 +
==Question ==
 +
<br />
 +
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 />
 +
==Submission==
 +
<br />
 +
Submit your program in the Problem 3 section on Moodle.
 +
<br />
 +
<br /><br />
 +
<br /><br />
 +
 +
=Problem 4: Fixed-Point Formats=
 +
<br />
 +
Answer the questions in the Problem 4 section of the final exam on Moodle.
 +
<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 />
 
<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 />
 
<br />
* For reference, here is a python version of what you have to do:
+
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 />
 
<br />
::<source lang="python">
+
==Incomplete Program==
#! /usr/bin/env python3
+
<br />
# factorial.py
+
::<source lang="C">
# D. Thiebaut
+
// makeList.c
from __future__ import print_function
+
// D. Thiebaut
import sys
+
// 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
  
def fact( n ):
+
100
    if n==1:
+
cs231a@aurora ~ $ ./makeList hello there
        return 1
+
1
    res = fact( n-1 )
+
2
    return n * res
+
-5
 +
10
 +
20
 +
-30
 +
5
  
if len( sys.argv ) != 2:
+
0
    print( "Syntax: ./factorial.py nnnn" )
+
0
    print( "where nnnn is a positive integer" )
 
    sys.exit()
 
  
n = int( sys.argv[1] )
 
print( "fact(", n, ") =", fact( n ) )
 
 
</source>
 
</source>
 
<br />
 
<br />
 +
==Submission==
 +
<br />
 +
* Submit your code on Moodle, in the Problem 6 section.
 +
<br />
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
 +
<!-- ====================================================================== -->
 +
<!-- ====================================================================== -->
 +
<!-- ====================================================================== -->
 +
 +
 +
<onlydft>
 +
=Solutions=
 +
==Bash Script==
 
<br />
 
<br />
 +
::<source lang="bash">
 +
#! /bin/bash
 +
# finalFact.sh
 +
# D. Thiebaut
 +
# uses a recursive function to compute the factorial of
 +
# a positive integer.  The integer is passed on the command
 +
# line.
  
 +
if [ "$#" -ne "1" ] ; then
 +
    echo "Syntax: ./finalFact.sh n"
 +
    echo "where n is an integer"
 +
    exit
 +
fi
  
 +
function fact {
 +
    if [ "$1" -eq "1" ] ; then
 +
return 1
 +
    fi
 +
    newN=$( expr $1 - 1 )
 +
    fact $newN
 +
    return $( expr $? \* $1 )
 +
}
 +
 +
 +
n=$1
 +
fact $n
 +
echo "fact( $n ) = $?"
 +
 +
 +
</source>
 +
<br />
 +
<br />
 
</onlydft>
 
</onlydft>

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.





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.











...