Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 irasnyd 1
/* arrays.c -- common functions for dealing with arrays */
2
 
3
#ifndef ARRAYS_C
4
#define ARRAYS_C
5
 
6
#include <stdio.h>  /* for printf() */
7
#include <stdlib.h> /* for exit() */
8
 
9
int* create_array (unsigned long size)
10
{
11
    int *array;
12
 
13
    if ( (array = (int*) malloc (size*sizeof(int))) == NULL)
14
    {
15
        printf ("Out of memory allocating array of size=%lu\n", size);
16
        printf ("Exiting...");
17
        exit (1);
18
    }
19
 
20
    return array;
21
}
22
 
23
int* free_array (int* array)
24
{
25
    free (array);
26
    array = NULL;
27
    return array;
28
}
29
 
30
/* copy the src[] to dst[] */
31
void copyarray (int src[], int dst[], unsigned long size)
32
{
33
    unsigned long i;
34
 
35
    for (i=0; i<size; i++)
36
        dst[i] = src[i];
37
}
38
 
39
/* copy src[src_left] to src[src_right] into dst[] */
40
void copyarray_partial (int src[], int dst[], unsigned long src_left, 
41
        unsigned long src_right)
42
{
43
    unsigned long i, j;
44
 
45
    for (i=src_left, j=0; i<=src_right; i++, j++)
46
        dst[j] = src[i];
47
}
48
 
49
/* print out the contents of the array
50
 * used for debugging mainly
51
 */
52
void printarray (int array[], unsigned long size)
53
{
54
    unsigned long i;
55
 
56
    printf("Printing %lu elements\n", size);
57
 
58
    for (i=0; i<size; i++)
59
        printf("%d ", array[i]);
60
 
61
    printf("\n");
62
}
63
 
64
#endif