Difference between revisions of "CSC231 Final Exam Fall 2017"

From dftwiki3
Jump to: navigation, search
Line 55: Line 55:
 
<br />
 
<br />
 
<br />
 
<br />
 +
=Solutions=
 +
==Bash Script==
 +
<br />
 +
::<source lang="bash">
 +
#! /bin/bash
 +
# funcMoodleChallenge.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: ./funcMoodleChallenge.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>

Revision as of 08:30, 2 November 2017


...