Homework 2 - Introduction Programming and Problem Solving | ECS 030, Assignments of Computer Science

Material Type: Assignment; Class: Intro Prog&Prob Solving; Subject: Engineering Computer Science; University: University of California - Davis; Term: Unknown 2002;

Typology: Assignments

Pre 2010

Uploaded on 07/31/2009

koofers-user-iqa
koofers-user-iqa 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
H
OMEWORK
2 ECS 30-A — S
PRING
2002
Version of April 15, 2002 4:42 pm Page 1 of 2
Homework 2
Due Date:
Monday, April 29, 2002,
at 11:59PM
Points
: 100
UNIX System
1. (
10 points
) What program is running as process #1?
2. (
15 points
) Suppose I have a file called
x
in my current working directory. File
y
is a (hard) link to this file, and
file
z
is a symbolic link to it also. I now give the command:
mv x a
What would the outputs of the following commands be, and why?
a. cat x
b. cat y
c. cat z
C Programming
3. (
25 points
) Write a C program named words.c that reads a string from stdin as words, and prints each word and
its line number on stdout. Loop until stdin’s EOF, then terminate. A word is defined to be any contiguous
sequence of alphanumeric characters. Use the fgets function to read the input a line at a time. Your program
should handle lines of up to 100 characters. Don’t bother to check for longer lines; you'll fix that in a later pro-
gram.
Your program should print one word per line. For example:
Sample stdin Corresponding stdout
Hello, there, my old friend! 1 Hello
How are you today? 1 there
I am very well, thank you! 1 my
Goodbye ... 1 old
1 friend
2 How
2 are
2 you
2 today
3 I
3 am
3 very
3 well
3 thank
3 you
4 Goodbye
4. (
25 points
) The Fibonacci numbers play an important role in biology, mathematics, and other sciences. The first
two numbers of the sequence are 0 and 1, and the numbers of the sequence are formed by adding the two previ-
ous numbers; so, the first few terms of the seuence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …. Please write a pro-
gram which takes an integer
n
as a command line argument, and prints the first
n
numbers of the sequence.
Debugging
5. (
25 points
) What does the following program do when you run it? Comment it, expanding each argument of the
printf
in your comment so that anyone can understand what each argument is in simplest form. Just make one
header comment, and do not clean up the program!
pf2

Partial preview of the text

Download Homework 2 - Introduction Programming and Problem Solving | ECS 030 and more Assignments Computer Science in PDF only on Docsity!

H O M E W O R K 2 E C S 3 0 - A — S P R I N G 2 0 0 2

Version of April 15, 2002 4:42 pm Page 1 of 2

Homework 2

Due Date: Monday, April 29, 2002, at 11:59PM Points : 100

UNIX System

  1. ( 10 points ) What program is running as process #1?
  2. ( 15 points ) Suppose I have a file called x in my current working directory. File y is a (hard) link to this file, and file z is a symbolic link to it also. I now give the command: mv x a What would the outputs of the following commands be, and why? a. cat x b. cat y c. cat z

C Programming

  1. ( 25 points ) Write a C program named words.c that reads a string from stdin as words, and prints each word and its line number on stdout. Loop until stdin’s EOF, then terminate. A word is defined to be any contiguous sequence of alphanumeric characters. Use the fgets function to read the input a line at a time. Your program should handle lines of up to 100 characters. Don’t bother to check for longer lines; you'll fix that in a later pro- gram. Your program should print one word per line. For example: Sample stdin Corresponding stdout Hello, there, my old friend! 1 Hello How are you today? 1 there I am very well, thank you! 1 my Goodbye ... 1 old 1 friend 2 How 2 are 2 you 2 today 3 I 3 am 3 very 3 well 3 thank 3 you 4 Goodbye
  2. ( 25 points ) The Fibonacci numbers play an important role in biology, mathematics, and other sciences. The first two numbers of the sequence are 0 and 1, and the numbers of the sequence are formed by adding the two previ- ous numbers; so, the first few terms of the seuence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …. Please write a pro- gram which takes an integer n as a command line argument, and prints the first n numbers of the sequence.

Debugging

  1. ( 25 points ) What does the following program do when you run it? Comment it, expanding each argument of the printf in your comment so that anyone can understand what each argument is in simplest form. Just make one header comment, and do not clean up the program!

H O M E W O R K 2 E C S 3 0 - A — S P R I N G 2 0 0 2

Version of April 15, 2002 4:42 pm Page 2 of 2

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}

Extra Credit

  1. ( 10 points ) If you wrote the Fibonnaci program in problem 4 using recursion, write a second version using a loop (and not using recursion). Conversely, if you wrote the Fibonnaci program using loops and not recursion, write a second version using recursion.