added another project

This commit is contained in:
Perry Kivolowitz 2023-01-19 12:02:47 -06:00
parent 1af3816bf1
commit 15b2dc1a22
7 changed files with 148 additions and 0 deletions

View file

@ -327,6 +327,9 @@ instructions. The program will "throw darts at a target," calculating
an approximation of PI by tracking how many darts "hit the target"
versus the total number of darts "thrown".
The [SINE](./projects/SINE/README.md) project stresses floating point
math and functions.
The [SNOW](./projects/snow/README.md) project uses 1970's era tech to
animate a simple particle system. This project demonstrates a reasonable
design process of breaking down complex problems into simpler parts.

Binary file not shown.

View file

@ -8,5 +8,6 @@ Here are a number of challenges for you to flex your new found knowledge.
| [FIRST](./first_project/README.md) | A good first project. |
| [FIZZBUZZ](../section_1/fizzbuzz/) | The interview question. A video is also provided. |
| [PI](./PI/README.md) | Compute an approximation of PI using stochastic (random) methods. |
| [SINE](./SINE/README.md) | Stresses functions and floating point math. |
| [SNOW](./snow/README.md) | A fun little animation. |
| [WALKIES](./walkies/README.md) | A fun little animation. |

Binary file not shown.

77
projects/SINE/README.md Normal file
View file

@ -0,0 +1,77 @@
# Compute Sine using Taylor Series
## Overview
This project stresses the use of floating point instructions to create
a program that computes the sine of an angle given to you in degrees
on the command line.
## Taylor Series
The sine of an angle given in radians can be found using the Taylor
Series:
```text
sin x = x - x^3/3! + x^5/5! - x^7/7! ...
```
Notice each term flips from addition to subtraction.
Notice each term is based on the odd integers starting at 1.
## Command line
You are to accept two arguments on the command line. `getopt` is not
being used here to concentrate on the floating point math. Both
arguments are therefore required.
* The angle in degrees whose sine you wish to calculate. Take this to
be a double.
* The number of terms to evaluate. The number of terms must lie between
1 and 10 inclusive.
## C version
To assist your efforts, [here](./c_version.c) is a version of this
project written in C.
## Errors to stderr
Error messages must be sent to `stderr`.
If you are using the convergence macros to allow your program to build
on both Apple Silicon Mac OS and Linux, note the special casing needed
to deal with `stderr`. If this is you, compile the C version on Mac OS
with the `-S` compiler option to see the generated assembly language and
search for `stderr`.
## Sample executions
```text
SINE % ./a.out 0 8
The sine of 0.00 degrees is 0.000000 in radians.
SINE % ./a.out 90 8
The sine of 90.00 degrees is 1.000000 in radians.
SINE % ./a.out 180 8
The sine of 180.00 degrees is -0.000001 in radians.
SINE % ./a.out 180 82
Number of terms is out of range.
SINE % ./a.out 180 -10
Number of terms is out of range.
SINE % echo $?
1
```
## CSC3510
The following applies to Carthage College CSC3510 students.
### Work rules
Work is to be done solo.
### What to hand in
Just the .S file. **Your name must be at the top of the file.**

BIN
projects/SINE/README.pdf Normal file

Binary file not shown.

67
projects/SINE/c_version.c Normal file
View file

@ -0,0 +1,67 @@
#include <stdio.h>
#include <stdlib.h>
double pi = 3.14159265359;
double D2R(double d) {
return d * pi / 180.0;
}
long Factorial(int n) {
long retval = 1;
if (n > 0) {
while (n > 1) {
retval = retval * n--;
}
}
return retval;
}
double IntegerPower(double b, int e) {
double retval = 1.0;
if (e > 0) {
while (e > 0) {
retval = retval * b;
e--;
}
}
return retval;
}
int main(int argc, char ** argv) {
double sin = 0.0;
if (argc != 3) {
fprintf(stderr, "Two numerical arguments must be given.\n");
return 1;
}
double angle = atof(argv[1]);
int terms = atoi(argv[2]);
if (terms < 1 || terms > 10) {
fprintf(stderr, "Number of terms is out of range.\n");
return 1;
}
double r_angle = D2R(angle);
for (int term = 0, base = 1; term < terms; term++, base += 2) {
double toggle = (term & 1) ? -1.0 : 1.0;
sin += toggle *
IntegerPower(r_angle, base) / Factorial(base);
/*
if (toggle > 0) {
printf("adding %d p/b intermediate: %f\n", base, sin);
} else {
printf("subtracting %d p/b intermediate: %f\n", base, sin);
}
*/
}
printf("The sine of %.2f degrees is %f in radians.\n", angle, sin);
return 0;
}