Commit 9f81740b authored by Bruce Cowan's avatar Bruce Cowan

Initial commit

parents
Pipeline #1955 failed with stages
in 2 minutes and 1 second
project('ee312-tutorial', 'c')
cc = meson.get_compiler('c')
libm = cc.find_library('m')
executable('q1-family', 'q1-family.c')
executable('q2-thousand', 'q2-thousand.c')
executable('q3-nums', 'q3-nums.c')
executable('q4-qsort', 'q4-qsort.c')
executable('q5-cos', 'q5-cos.c', dependencies: libm)
#include <stdio.h>
int
main (void)
{
printf ("Mike, Douglas, Macy, Carolyn, Neil\n");
return 0;
}
#include <stdio.h>
int
main (void)
{
for (int i = 1; i <= 1000; i++)
printf ("%d\n", i);
return 0;
}
#include <stdio.h>
int
main (void)
{
int num1, num2;
printf ("What is number 1?\n");
scanf ("%d", &num1);
printf ("What is number 2?\n");
scanf ("%d", &num2);
int sum = num1 + num2;
printf ("Sum is %d\n", sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define N 5
static int
int_compare (const void *a,
const void *b)
{
// Dereferencing void pointers requires a cast
return *((int *) a) - *((int *) b);
}
int
main (void)
{
int array[N];
for (int i = 0; i < N; i++)
{
printf ("What is number %d?: ", i+1);
scanf ("%d", &array[i]);
}
qsort (array, N, sizeof (int), int_compare);
printf ("Sorted array is now:\n");
for (int i = 0; i < N; i++)
printf ("%d ", array[i]);
printf ("\n");
return 0;
}
#include <stdio.h>
#define N 5
int
main (void)
{
int array[N];
for (int i = 0; i < N; i++)
{
printf ("What is number %d?: ", i+1);
scanf ("%d", &array[i]);
}
// Simple sort, very inefficient
for (int i = 0; i < N; i++)
{
for (int j = i+1; j < N; j++)
{
if (array[i] > array[j])
{
// Standard idiom for swapping
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf ("Sorted array is now:\n");
for (int i = 0; i < N; i++)
printf ("%d ", array[i]);
printf ("\n");
return 0;
}
#include <math.h>
#include <stdio.h>
/**
* Converts from degrees to radians
*/
static float
deg_to_radians (float degrees)
{
float radians = degrees / (180.0 / M_PI);
printf ("%.6f degrees = %.6f radians\n", degrees, radians);
return radians;
}
/**
* Converts from radians to degrees
*/
static float
rad_to_degrees (float radians)
{
float degrees = radians * (180.0 / M_PI);
printf ("%.6f radians = %.6f degrees\n", radians, degrees);
return degrees;
}
/**
* Takes in a value in radians, outputs the cosine of it
*/
static float
calc_cos (float angle)
{
float ret = cosf (angle);
printf ("cos (%.6f) = %.6f", angle, ret);
return ret;
}
/**
* Finds the inverse cosine
*/
static float
calc_inv_cos (float arg)
{
float inv = acosf (arg);
printf ("acos (%.6f) = %.6f", arg, inv);
return rad_to_degrees (inv);
}
int
main (void)
{
float radians = M_PI;
float difference = radians - deg_to_radians (calc_inv_cos (calc_cos (radians)));
printf ("The difference is %.6f\n", difference);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment