Answer to Quiz 2(b) Thu 30 Aug 2001


1. Consider the program below.  Write in the table at the 
   right the values of the variables after the execution
   of each corresponding instruction at the left.

                          --------------------------
   include<stdio.h>       | values after each step |
                          |------------------------|
   int main(void)         |   i   |   j   |   k    |
   {                      |========================|
      int i,j,k;          |   -   |   -   |   -    |
      int j = k = 3;      |   -   |   3   |   3    |
      i = j++;            |   3   |   4   |   3    |
      k += --i;           |   2   |   4   |   5    |
      return 0;           |       |       |        |
   }                      --------------------------

2. Write a program that reads in an integer and writes the
   number in ternary notation (using 0, 1, and 2 as digits),
   starting with the least significant digit.

A sample session showing
  345 = 0 . 3^0 + 1 . 3^1 + 2 . 3^2 + 0 . 3^3 + 1 . 3^4 + 1 . 3^5 is

       [jan@galois]% ./terdeco
       Give positive integer : 345
         345 = 012011 in ternary notation
       [jan@galois]%


#include<stdio.h>

int main(void)
{
   int n;

   printf("Give positive integer : ");
   scanf("%d",&n);

   printf("  %d = ", n);

   while (n > 0)
   {
      printf("%d",n%3);
      n = n/3;
   }
   printf(" in ternary notation\n");

   return 0;
}