Difference between revisions of "CSC231 Final Exam Fall 2017"
Line 53: | Line 53: | ||
Store your program in a file containing zap2() and any other function you need. Your program should not contain a main function, as it will be linked with a test program that will contain main(). | Store your program in a file containing zap2() and any other function you need. Your program should not contain a main function, as it will be linked with a test program that will contain main(). | ||
<br /> | <br /> | ||
− | =Bash Script= | + | =Problem 3: Bash Script= |
<br /> | <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. Call this script '''funcMoodleChallenge.sh'''. |
* Examples of how it should work: | * Examples of how it should work: | ||
<br /> | <br /> | ||
− | cs231a@aurora ~/handout $ '''./ | + | cs231a@aurora ~/handout $ '''./finalFact.sh 4''' |
fact( 4 ) = 24 | fact( 4 ) = 24 | ||
− | cs231a@aurora ~/handout $ '''./ | + | cs231a@aurora ~/handout $ '''./finalFact.sh 5''' |
fact( 5 ) = 120 | fact( 5 ) = 120 | ||
− | cs231a@aurora ~/handout $ '''./ | + | 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 $ '''for i '''in 1 2 3 5 6 10 ; do''' | ||
− | >''' ./ | + | >''' ./finalFact.sh $i |
> '''done''' | > '''done''' | ||
fact( 1 ) = 1 | fact( 1 ) = 1 | ||
Line 108: | Line 108: | ||
::<source lang="bash"> | ::<source lang="bash"> | ||
#! /bin/bash | #! /bin/bash | ||
− | # | + | # finalFact.sh |
# D. Thiebaut | # D. Thiebaut | ||
# uses a recursive function to compute the factorial of | # uses a recursive function to compute the factorial of | ||
Line 115: | Line 115: | ||
if [ "$#" -ne "1" ] ; then | if [ "$#" -ne "1" ] ; then | ||
− | echo "Syntax: ./ | + | echo "Syntax: ./finalFact.sh n" |
echo "where n is an integer" | echo "where n is an integer" | ||
exit | exit |