Introductory C Programming

UW Experimental College

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 for 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 3 < 4 ?

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 T 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 x 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 printf("b\n"); is (deliberately, for the sake of the question) misleading. It looks like it's part of the body of the for statement, but the body of the for 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 printf("a\n");. The two statements printf("b\n"); and printf("c\n"); are normal statements following the loop.

The code would be much clearer if the printf("b\n"); line were indented to line up with the printf("c\n"); 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 printf("a\n"); and printf("b\n"); .

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 day 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 x and y 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 x == 0 and y == 0.)

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 int. To write a portable program to compute factorials higher than 7, we would have to use long int.

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;
}
```