Cos1511 Assignment

Assignment #2 Answers

Introductory C Programming

UW Experimental College

Assignment #2 ANSWERS


Question 1. What are the two different kinds of division that the operator can do? Under what circumstances does it perform each?

The two kinds of division are integer division, which discards any remainder, and floating-point division, which yields a floating-point result (with a fractional part, if necessary). Floating-point division is performed if either operand (or both) is floating-point (that is, if either number being operated on is floating-point); integer division is performed if both operands are integers.

Question 2. What are the definitions of the ``Boolean'' values true and false in C?

False is always represented by a zero value. Any nonzero value is considered true. The built-in operators , , , , , , , , and always generate 0 for false and 1 for true.

Question 3. Name three uses for the semicolon in C.

Terminating declarations, terminating statements, and separating the three control expressions in a loop.

Question 4. What would the equivalent code, using a while loop, be for the example

for(i = 0; i < 10; i = i + 1) printf("i is %d\n", i); ?

Just move the first (initialization) expression to before the loop, and the third (increment) expression to the body of the loop:

i = 0; while(i < 10) { printf("i is %d\n", i); i = i + 1; }

Question 5. What is the numeric value of the expression ?

1 (or ``true''), because 3 is in fact less than 4.

Question 6. Under what conditions will this code print ``water''?

if(T < 32) printf("ice\n"); else if(T < 212) printf("water\n"); else printf("steam\n");

If is greater than or equal to 32 and less than 212. (If you said ``greater than 32 and less than 212'', you weren't quite right, and this kind of distinction--paying attention to the difference between ``greater than'' and ``greater than or equal''--is often extremely important in programming.)

Question 7. What would this code print?

int x = 3; if(x) printf("yes\n"); else printf("no\n");

It would print ``yes'', since is nonzero.

Question 8. What would this code print?

int i; for(i = 0; i < 3; i = i + 1) printf("a\n"); printf("b\n"); printf("c\n");

It would print

a a a b c The indentation of the statement is (deliberately, for the sake of the question) misleading. It looks like it's part of the body of the statement, but the body of the statement is always a single statement, or a list of statements enclosed in braces {}. In this case, the body of the loop is the call . The two statements and are normal statements following the loop.

The code would be much clearer if the line were indented to line up with the line. If the intent was that ``b'' be printed 3 times, along with ``a'', it would be necessary to put braces around the pair of lines and .

Exercise 1. Write a program to find out how many of the numbers from 1 to 10 are greater than 3.

#include <stdio.h> int main() { int i; int count = 0; for(i = 1; i <= 10; i = i + 1) { if(i > 3) count = count + 1; } printf("%d numbers were greater than 3\n", count); return 0; }

Tutorial 4b. Print ``On the first day'' instead of ``On the 1 day'' in the days-of-Christmas program.

Simply replace the lines

printf("On the %d day of Christmas, ", day); printf("my true love gave to me\n"); with printf("On the "); if(day == 1) printf("first"); else if(day == 2) printf("second"); else if(day == 3) printf("third"); else if(day == 4) printf("fourth"); else if(day == 5) printf("fifth"); else if(day == 6) printf("sixth"); else printf("%d", day); printf(" day of Christmas, "); printf("my true love gave to me\n"); The final else printf("%d", day); is an example of ``defensive programming.'' If the variable ever ends up having a value outside the range 1-6 (perhaps because we later added more verses to the song, but forgot to update the list of words), the program will at least print something.

Exercise 2. Write a program to compute the average of the ten numbers 1, 4, 9, ..., 81, 100.

#include <stdio.h> int main() { int i; float sum = 0; int n = 0; for(i = 1; i <= 10; i = i + 1) { sum = sum + i * i; n = n + 1; } printf("the average is %f\n", sum / n); return 0; }

Exercise 3. Write a program to print the numbers between 1 and 10, along with an indication of whether each is even or odd.

#include <stdio.h> int main() { int i; for(i = 1; i <= 10; i = i + 1) { if(i % 2 == 0) printf("%d is even\n", i); else printf("%d is odd\n", i); } return 0; }

Exercise 4. Print out the 8 points of the compass, based on the and components of the direction of travel.

if(x > 0) { if(y > 0) printf("Northeast.\n"); else if(y < 0) printf("Southeast.\n"); else printf("East.\n"); } else if(x < 0) { if(y > 0) printf("Northwest.\n"); else if(y < 0) printf("Southwest.\n"); else printf("West.\n"); } else { if(y > 0) printf("North.\n"); else if(y < 0) printf("South.\n"); else printf("Nowhere.\n"); } (You will notice that this code also prints ``Nowhere'' in the ninth case, namely when and .)

Exercise 5. Write a program to print the first 7 positive integers and their factorials.

#include <stdio.h> int main() { int i; int factorial = 1; for(i = 1; i <= 7; i = i + 1) { factorial = factorial * i; printf("%d %d\n", i, factorial); } return 0; }

The answer to the extra credit question is that 8!, which is how mathematicians write ``eight factorial,'' is 40320, which is not guaranteed to fit into a variable of type . To write a portable program to compute factorials higher than 7, we would have to use .

Exercise 6. Write a program to print the first 10 Fibonacci numbers.

#include <stdio.h> int main() { int i; int fibonacci = 1; int prevfib = 0; int tmp; for(i = 1; i <= 10; i = i + 1) { printf("%d %d\n", i, fibonacci); tmp = fibonacci; fibonacci = fibonacci + prevfib; prevfib = tmp; } return 0; } (There are many ways of writing this program. As long as the code you wrote printed 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, it's probably correct. If the code you wrote is clearer than my solution above, so much the better!)

Exercise 7. Make some improvements to the prime number printing program.

Here is a version incorporating both of the suggested improvements:

#include <stdio.h> #include <math.h> int main() { int i, j; double sqrti; printf("%d\n", 2); for(i = 3; i <= 100; i = i + 2) { sqrti = sqrt(i); for(j = 2; j < i; j = j + 1) { if(i % j == 0) break; /* not prime */ if(j > sqrti) { /* prime */ printf("%d\n", i); break; } } } return 0; }

This page by Steve Summit // Copyright 1995-9 // mail feedback

 

COS1511/202/1

3

SEMESTER MARK

Assignments 1 and 2 contribute towards your semester mark for this module. Assignment 1 carries a weight of 30% and Assignment 2 a weight of 70%. The semester mark contributes 10% to the final mark for this module.Suppose a student gets 60% for Assignment 1 and 50% for Assignments 2. In order to calculate the semestermark, the mark obtained for the specific assignment is multiplied by the weight. This then forms part of the 10%that the semester mark contributes to the final mark. Therefore:Assignment MarksobtainedWeight Contribution to semester mark 1 60% 30% 60/100 x 30/100 x 10 1.802 50% 70% 50/100 x 70/100 x 10 3.503 0%TOTAL 5.30

Hence, in this example the semester mark is 5.30. Note that the semester mark will not form part of thefinal mark if the supplementary examination is written.

If you followed the guidelines given in the questions, got the same output for the given input data, and used goodprogramming techniques, your programs need not be identical to ours.NOTE: In the Study Guide we introduced certain conventions, amongst others

that names of variables and the names of functions should start with lowercase characters and furtherwords in the names with uppercase characters,

that the names of void functions should be verbs (or contain a verb) and

that the names of value-returning functions should be nouns or adjectives.

Use constants where needed.Avoid the use of global variables.We also want to stress again that correct indenting is extremely important.Also, remember to include comments

.

0 Replies to “Cos1511 Assignment”

Lascia un Commento

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *