Docsity

Test 2 for System-Level Programming | CSC 3320, Exams of Computer Science

Toggle sidebar
bg1
Csc 3320 Test 2
Summer 2009
Name__________________ Date_____________________
1. Read the following program, and write the corresponding outputs when values of
nSerial are 1, 3, 5, 7, and 9 respectively. (25 pts)
#!/bin/sh
# Fibonacci number shell script
# It takes input one number and show Fibonacci number of that serial
# Prompts for an input
echo -n "Which fibonacci number do you want to see? "
# Read the number
read nSerial
# Initializes variables
a=0
b=1
count=2 # Used to control the number of iteration of the while loop
fibonacci_number=$a
# The loop to calculate the number
while [ $count -le $nSerial ]; do # Checks whether count is less than or equal to nSerial
fibonacci_number=$[$a+$b]
a=$b
b=$fibonacci_number
count=$[$count + 1]
done
# Print the output
echo "Fibonacci $nSerial = $fibonacci_number"
pf3
pf4

Partial preview of the text

Download Test 2 for System-Level Programming | CSC 3320 and more Exams Computer Science in PDF only on Docsity!

Csc 3320 Test 2

Summer 2009 Name__________________ Date_____________________

1. Read the following program, and write the corresponding outputs when values of nSerial are 1, 3, 5, 7, and 9 respectively. (25 pts) #!/bin/sh

Fibonacci number shell script

It takes input one number and show Fibonacci number of that serial

Prompts for an input

echo -n "Which fibonacci number do you want to see? "

Read the number

read nSerial

Initializes variables

a= b= count=2 # Used to control the number of iteration of the while loop fibonacci_number=$a

The loop to calculate the number

while [ $count -le $nSerial ]; do # Checks whether count is less than or equal to nSerial fibonacci_number=$[$a+$b] a=$b b=$fibonacci_number count=$[$count + 1] done

Print the output

echo "Fibonacci $nSerial = $fibonacci_number"

2. Read the following program, describe the function of the program, and write the values of num for each step and the final output if we have the input of 5 34 8 20 15. (25 pts) #!/bin/bash echo "Input numbers --" read -a num numlen=${#num[*]} x=$numlen temp=

Standard algorithm

while [ $x -gt 0 ]; do i= while [ $i -lt $[$numlen-1] ]; do j=$[$i+1] # If the previous element in the array is greater than the next then swap the values. if [ ${num[i]} -gt ${num[j]} ] then temp=${num[i]} num[i]=${num[j]} num[j]=$temp fi i=$j done x=$[$x-1] done echo “The new sequence is: ${num[*]} “ 1. Sort the inputted numbers in ascending order 2. Iteration 1: 5 8 20 15 34 Iteration 2: 5 8 15 20 34 Iteration 3, 4, 5: 5 8 15 20 34 3. The final output

4. Programming problem. Write a script to find the maximum, minimum, and the average of n numbers from the command line in Bourne Shell. (25 pts) [fgu@qubit ~]$./file 8 16 12 4 The maximum of 8 16 12 4 is: 16 The minimum of 8 16 12 4 is: 4 The average of 8 16 12 4 is: 10