Assignment 1

This first assignment will give you some experience using basic features of C. You will write one program that is composed of three independent parts. (Hint: Each independent part should be a separate function.)

In the first part, you will write a function to output a square multiplication table, given the size of the table as a parameter.

In the second part, you will write a function to output a table with four columns: angle in degrees, sine of the angle, cosine of the angle, and sum of the squares of the sine and cosine. One row should be output for each angle from 0 up to (but not including) 90 degrees, in increments of 15. Note: The math functions sin(x) and cos(x) are defined in the header <math.h> and accept angles in radians, so you'll want a function to convert degrees to radians. Make pi a constant.

In the third part, you will list all primes less than 100 using the Sieve of Erastosthenes. The algorithm is as follows. Create an array of size 100, and initialize each element to "true". Then loop over each element of the array, starting with 2. If the current element is "true", then the corresponding number is prime, so output it, and set to "false" every multiple of the number less than 100, since they can't be prime. Hint: Since 100 is used repeatedly, declare it to be a named constant.

Use the following main() to call the functions you wrote:

int main() {
  multtable(4);
  printf("\n");
  multtable(12);
  printf("\n");
  trigtable();
  printf("\n");
  sieve();
  return 0;
}
    

So that you can see the output format I expect, here's the output from my program:

 x |  1|  2|  3|  4
-------------------
  1|  1|  2|  3|  4
  2|  2|  4|  6|  8
  3|  3|  6|  9| 12
  4|  4|  8| 12| 16

 x |  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12
---------------------------------------------------
  1|  1|  2|  3|  4|  5|  6|  7|  8|  9| 10| 11| 12
  2|  2|  4|  6|  8| 10| 12| 14| 16| 18| 20| 22| 24
  3|  3|  6|  9| 12| 15| 18| 21| 24| 27| 30| 33| 36
  4|  4|  8| 12| 16| 20| 24| 28| 32| 36| 40| 44| 48
  5|  5| 10| 15| 20| 25| 30| 35| 40| 45| 50| 55| 60
  6|  6| 12| 18| 24| 30| 36| 42| 48| 54| 60| 66| 72
  7|  7| 14| 21| 28| 35| 42| 49| 56| 63| 70| 77| 84
  8|  8| 16| 24| 32| 40| 48| 56| 64| 72| 80| 88| 96
  9|  9| 18| 27| 36| 45| 54| 63| 72| 81| 90| 99|108
 10| 10| 20| 30| 40| 50| 60| 70| 80| 90|100|110|120
 11| 11| 22| 33| 44| 55| 66| 77| 88| 99|110|121|132
 12| 12| 24| 36| 48| 60| 72| 84| 96|108|120|132|144

 Deg |  Sin  |  Cos  | S^2+C^2 
-------------------------------
   0 | 0.000 | 1.000 |  1.000 
  15 | 0.259 | 0.966 |  1.000 
  30 | 0.500 | 0.866 |  1.000 
  45 | 0.707 | 0.707 |  1.000 
  60 | 0.866 | 0.500 |  1.000 
  75 | 0.966 | 0.259 |  1.000 

Primes: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
    

You may discuss the assignment with others, but each person is expected to write and test their own implementation of the program. Turn in a printout of your program and its output at the lecture on Monday, February 1.