Skip to content
Snippets Groups Projects
Commit fcdda296 authored by Bruce Cowan's avatar Bruce Cowan :airplane:
Browse files

Remove files which shouldn't have been there

Turned out everything was under the 'winter' directory
parent ff3ae5e7
No related branches found
No related tags found
No related merge requests found
Showing
with 1 addition and 951 deletions
...@@ -21,10 +21,10 @@ executable('sizeof', 'sizeof.c') ...@@ -21,10 +21,10 @@ executable('sizeof', 'sizeof.c')
executable('snprintf', 'snprintf.c') executable('snprintf', 'snprintf.c')
executable('strtol', 'strtol.c') executable('strtol', 'strtol.c')
executable('taylor', 'taylor.c') executable('taylor', 'taylor.c')
executable('winter', 'winter.c')
executable('world', 'world.c') executable('world', 'world.c')
subdir('sockets') subdir('sockets')
subdir('winter')
if openmp_dep.found() if openmp_dep.found()
subdir('openmp') subdir('openmp')
......
File moved
/* Adds up 2 integers, not the most exciting thing in the world */
#include <stdio.h>
int
main (int argc, char **argv)
{
int a, b;
if (argc != 3)
{
printf ("Please input an integer value: ");
scanf ("%d", &a);
printf ("Please input an integer value: ");
scanf ("%d", &b);
}
else
{
sscanf (argv[1], "%d", &a);
sscanf (argv[2], "%d", &b);
}
printf ("%d + %d = %d\n", a, b, a + b);
return 0;
}
#include <stdio.h>
#include "utils.h"
int
main (int argc,
char **argv)
{
int *vals = get_ints (argc, argv, 2, "Please input an integer value");
printf ("%d + %d = %d\n", vals[0], vals[1], vals[0] + vals[1]);
return 0;
}
#include <stdio.h>
#include <tgmath.h>
#define DEG_TO_RAD(x) ((x) * M_PI / 180.0)
int
main (int argc, char **argv)
{
double angle, angle_rad;
double sin_a, cos_a, tan_a;
if (argc != 2)
{
printf ("Input the angle: ");
scanf ("%lf", &angle);
}
else
sscanf (argv[1], "%lf", &angle);
angle_rad = DEG_TO_RAD (angle);
sin_a = sin (angle_rad);
cos_a = cos (angle_rad);
tan_a = tan (angle_rad);
printf ("sin (%f) = %f\n", angle, sin_a);
printf ("cos (%f) = %f\n", angle, cos_a);
printf ("tan (%f) = %f\n", angle, tan_a);
return 0;
}
subdir('src')
if glib_dep.found()
subdir('test')
endif
/* array.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"
#include "mem.h"
#define MIN_SIZE 2
struct _Array
{
void **data;
size_t length;
size_t capacity;
FreeFunc free_func;
int ref_count;
};
static void
resize_array (Array *array,
size_t required)
{
size_t size = MIN_SIZE;
while (size < required)
{
size = (size_t) ceil (size * 1.5);
}
array->data = check_realloc (array->data, sizeof (void *) * size);
array->capacity = size;
}
static bool
index_in_array (const Array *array,
size_t index)
{
if (index < array->length)
return true;
else
{
errno = EINVAL;
return false;
}
}
static void
maybe_shrink (Array *array)
{
if (array->length <= (size_t) ceil (array->capacity * 0.3))
resize_array (array, array->length);
}
/**
* array_new:
* @func: (nullable): A #FreeFunc which is used to free deleted items
*
* Creates a new #Array.
*
* Returns: The new #Array, free with array_free()
*/
Array *
array_new (FreeFunc func)
{
Array *array = check_malloc (sizeof (Array));
array->data = check_malloc (sizeof (void *) * MIN_SIZE);
array->length = 0;
array->capacity = MIN_SIZE;
array->free_func = func;
array->ref_count = 1;
return array;
}
/**
* array_ref:
* @array: The array to increase the reference count of
*
* Increase the reference account of the array.
*
* Returns: The array
*/
Array *
array_ref (Array *array)
{
array->ref_count++;
return array;
}
/**
* array_unref:
* @array: The array to decrease the reference count of
*
* Decrease the reference account of the array. If it becomes 0, it will be
* freed.
*/
void
array_unref (Array *array)
{
array->ref_count--;
if (array->ref_count == 0)
{
if (array->free_func)
{
for (size_t i = 0; i < array->length; i++)
array->free_func (array->data[i]);
}
free (array->data);
free (array);
}
}
/**
* array_add:
* @array: An array
* @val: The value to add to the array
*
* Adds a value to an array.
*/
void
array_add (Array *array,
const void *val)
{
if (array->length == array->capacity)
resize_array (array, array->length + 1);
array->data[array->length] = (void *) val;
array->length += 1;
}
/**
* array_remove_fast:
* @array: An array
* @index: The index of the item to remove from @array
*
* Removes an item from an array. This function copies the last member of the
* array to the location of the item to be removed, which means that ordering
* is not preserved.
*
* Returns: %true if successful
*/
bool
array_remove_fast (Array *array,
size_t index)
{
if (!index_in_array (array, index))
{
perror ("array_remove_fast");
return false;
}
if (array->free_func)
array->free_func (array->data[index]);
void *last = array->data[array->length - 1];
array->data[index] = last;
array->length -= 1;
maybe_shrink (array);
return true;
}
/**
* array_remove:
* @array: An array
* @index: The index of the item to remove from @array
*
* Removes an item from an array. This function moves the items following the
* removed item down one space. This preserves the ordering of the items, but is
* slower than array_remove_fast().
*
* Returns: %true if successful
*/
bool
array_remove (Array *array,
size_t index)
{
if (!index_in_array (array, index))
{
perror ("array_remove");
return false;
}
if (array->free_func)
array->free_func (array->data[index]);
memmove (array->data + index,
array->data + index + 1,
sizeof (void *) * (array->length - index - 1));
array->length -= 1;
maybe_shrink (array);
return true;
}
/**
* array_set:
* @array: An array
* @index: The index of the item to set a value for
* @val: The value to set the item to
*
* Sets an element of @array to @val, using the @index.
*
* Returns: %true if successful
*/
bool
array_set (Array *array,
size_t index,
const void *val)
{
if (!index_in_array (array, index))
{
perror ("array_set");
return false;
}
array->data[index] = (void *) val;
return true;
}
/**
* array_get:
* @array: An array
* @index: The index of @array to get a value from
*
* Get a value of @array, using @index.
*
* Returns: (transfer none): The value at @index of @array
*/
const void *
array_get (const Array *array,
size_t index)
{
if (index_in_array (array, index))
return array->data[index];
else
{
perror ("array_get");
return NULL;
}
}
/**
* array_get_length:
* @array: An array
*
* Gets the number of elements in an array.
*
* Returns: The length of the array
*/
size_t
array_get_length (const Array *array)
{
return array->length;
}
/**
* array_get_capacity:
* @array: An array
*
* Get the capacity (proportional to memory usage) of @array.
*
* Returns: The capacity, in items.
*/
size_t
array_get_capacity (const Array *array)
{
return array->capacity;
}
/**
* array_sort:
* @array: An array
* @func: A function which returns -1 for less than, 0 for equal, and 1 for more than
*
* Sorts the array
*/
void
array_sort (Array *array,
CompareFunc func)
{
qsort (*array->data, array->length, sizeof (void *), func);
}
/* array.h
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdbool.h>
#include <stddef.h>
typedef struct _Array Array;
typedef void (*FreeFunc) (void *element);
typedef double (*ValueFunc) (const void *element);
typedef int (*CompareFunc) (const void *a, const void *b);
// Fundamental functions
Array * array_new (FreeFunc func);
Array * array_ref (Array *array);
void array_unref (Array *array);
void array_add (Array *array,
const void *val);
bool array_remove_fast (Array *array,
size_t index);
bool array_remove (Array *array,
size_t index);
bool array_set (Array *array,
size_t index,
const void *val);
// Getters
const void * array_get (const Array *array,
size_t index);
size_t array_get_length (const Array *array);
size_t array_get_capacity (const Array *array);
// Misc
void array_sort (Array *array,
CompareFunc func);
/* mem.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "mem.h"
#include <stdlib.h>
void *
check_malloc (size_t size)
{
void *ptr = malloc (size);
if (!ptr)
exit (EXIT_FAILURE);
return ptr;
}
void *
check_realloc (void *ptr,
size_t size)
{
void *new = realloc (ptr, size);
if (!new)
exit (EXIT_FAILURE);
return new;
}
/* mem.h
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stddef.h>
void * check_malloc (size_t size);
void * check_realloc (void *ptr,
size_t size);
src = files('array.c', 'mem.c')
lib = shared_library('array', src, dependencies: libm)
lib_inc = include_directories('.')
/* average.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <math.h>
#include <glib.h>
#include <array.h>
static gdouble
get_total (const Array *array)
{
double total = 0;
size_t length = array_get_length (array);
for (size_t i = 0; i < length; i++)
total += (double) GPOINTER_TO_INT (array_get (array, i));
return total;
}
static gdouble
get_average (const Array *array)
{
return get_total (array) / array_get_length (array);
}
static Array *
create_array (size_t length)
{
Array *array = array_new (NULL);
for (size_t i = 1; i < length; i++)
array_add (array, GINT_TO_POINTER (i));
return array;
}
static void
expected_values (Array *array,
double *total,
double *average)
{
size_t n = array_get_length (array);
gdouble sum = (n * (n+1)) / 2;
if (total)
*total = sum;
if (average)
*average = sum / n;
}
static void
test_total (void)
{
Array *array = create_array (100);
gdouble expected_total;
gdouble total = get_total (array);
expected_values (array, &expected_total, NULL);
g_assert_cmpfloat (total, ==, expected_total);
array_unref (array);
}
static void
test_average (void)
{
Array *array = create_array (100);
gdouble expected_average;
gdouble average = get_average (array);
expected_values (array, NULL, &expected_average);
g_assert_cmpfloat (average, ==, expected_average);
array_unref (array);
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/array/total", test_total);
g_test_add_func ("/array/average", test_average);
return g_test_run ();
}
/* basic.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <glib.h>
#include "array.h"
#define LENGTH 100
static Array *
create_array (size_t length)
{
Array *array = array_new (g_free);
for (size_t i = 0; i < length; i++)
{
gchar *str = g_strdup_printf ("String #%zu", i);
array_add (array, str);
}
return array;
}
static void
test_get (void)
{
Array *array = create_array (LENGTH);
for (size_t i = 0; i < LENGTH; i++)
{
const gchar *element = (const gchar *) array_get (array, i);
g_autofree gchar *compare = g_strdup_printf ("String #%zu", i);
g_assert_cmpstr (element, ==, compare);
}
array_unref (array);
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/array/get", test_get);
return g_test_run ();
}
/* empty.c
*
* Copyright 2018 Bruce Cowan <bruce@bcowan.me.uk>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <glib.h>
#include "array.h"
static void
test_length (void)
{
Array *array = array_new (NULL);
size_t length = array_get_length (array);
g_assert_cmpint (length, ==, 0);
array_unref (array);
}
static void
test_remove_fast (void)
{
Array *array = array_new (NULL);
gboolean success = array_remove_fast (array, 1);
g_assert_false (success);
array_unref (array);
}
static void
test_remove (void)
{
Array *array = array_new (NULL);
gboolean success = array_remove (array, 1);
g_assert_false (success);
array_unref (array);
}
static void
test_get (void)
{
Array *array = array_new (NULL);
const void *element = array_get (array, 1);
g_assert_null (element);
array_unref (array);
}
static void
test_set (void)
{
Array *array = array_new (NULL);
gboolean success = array_set (array, 1, NULL);
g_assert_false (success);
array_unref (array);
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/array/length", test_length);
g_test_add_func ("/array/remove_fast", test_remove_fast);
g_test_add_func ("/array/remove", test_remove);
g_test_add_func ("/array/get", test_get);
g_test_add_func ("/array/set", test_set);
return g_test_run ();
}
avg = executable('average',
'average.c',
link_with: lib,
include_directories: lib_inc,
dependencies: [glib_dep])
basic = executable('basic',
'basic.c',
link_with: lib,
include_directories: lib_inc,
dependencies: [glib_dep])
empty = executable('empty',
'empty.c',
link_with: lib,
include_directories: lib_inc,
dependencies: [glib_dep])
test('average', avg)
test('basic', basic)
test('empty', empty)
#include <stdio.h>
static int list[] = { 8, 11, 17, 22, 29, 38, 51, 58, 65, 67, 74, 81, 87, 90, 96, 97 };
#define N_ELEMENTS(arr) (sizeof(arr) / sizeof(arr[0]))
static void
binary_search (int key)
{
int passes = 0;
int *low = &list[0];
int *high = &list[N_ELEMENTS (list) - 1];
int *mid;
printf ("key = %d\n", key);
while (low <= high)
{
passes += 1;
printf ("\n----------Pass #%d----------\n", passes);
mid = low + (high - low) / 2;
printf ("low, mid, high = %d, %d, %d\n", *low, *mid, *high);
if (key < *mid)
{
high = mid - 1;
printf ("key < mid\n");
}
else if (key > *mid)
{
low = mid + 1;
printf ("key > mid\n");
}
else
{
printf ("key = %d\n", *mid);
printf ("%d passes\n", passes);
break;
}
}
}
int
main (int argc, char **argv)
{
int key;
if (argc != 2)
{
printf ("What is the number to search for? ");
scanf ("%d", &key);
}
else
sscanf (argv[1], "%d", &key);
binary_search (key);
return 0;
}
#include <stdio.h>
#include <tgmath.h>
int
main (void)
{
double complex z1 = I*I;
printf("j * j = %.1f + j%.1f\n", creal (z1), cimag (z1));
double complex z2 = pow (I, 2);
printf("j^2 = %.1f + j%.1f\n", creal (z2), cimag (z2));
double complex z3 = exp (I * M_PI);
printf("exp(j * pi) = %.1f + j%.1f\n", creal (z3), cimag (z3));
double complex z4 = 1 + 2*I;
double complex z5 = 1 - 2*I;
printf ("(1+j2)(1-j2) = %.1f + j%.1f\n", creal (z4*z5), cimag (z4*z5));
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define C 299792458
#define C2 (C)*(C)
int
main (int argc, char **argv)
{
double m;
if (argc != 2)
{
printf ("Input the mass in kg: ");
scanf ("%lf", &m);
}
else
{
if (sscanf (argv[1], "%lf", &m) != 1)
{
fprintf (stderr, "Couldn't parse number\n");
return EXIT_FAILURE;
}
}
double e = m * C2;
printf ("E = %#.3G J\n", e);
return 0;
}
#include <stdio.h>
int
main (void)
{
char array[80];
printf ("Enter string for fgets()\n");
fgets (array, sizeof(array), stdin);
printf ("fgets returned: %s\n", array);
printf ("Enter string for scanf()\n");
scanf ("%80[^\n]", array);
printf ("scanf() returned: %s\n", array);
return 0;
}
#include <inttypes.h>
#include <stdio.h>
#define N 45
static uint32_t
fib (int n)
{
if (n < 2)
return 1;
return fib (n - 2) + fib (n - 1);
}
int
main (void)
{
for (int i = 0; i < N; i++)
{
printf ("fib(%2d) = %" PRIu32 "\n", i, fib (i));
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment