Posts

Array : Dynamic

/* A dynamically-allocated 1-D array */ #include <stdio.h> int main (void) { double* array; /* declare a pointer only */ int i, size; /* ask user for size of array */ printf ("How large do you want your array? "); scanf ("%d", &size); /* allocate the array in the heap */ array = (double *) calloc (size, sizeof(double)); /* printing the array for verification surprise! a dynamic array is automatically initialized with zeros! */ for (i = 0; i < size; ++i) printf ("%6.2lf", array[i]); /* freeing the memory allocation */ free (array); return (0); }

Array : Search

/* THE BASIC SEARCH ALGORITHM FOR A 1-D ARRAY */ #include <stdio.h> int search ( int arraytosearch[], int valuetosearch, int size ) { int i, found; /* initialize found at -1, if value not found, stays at -1 */ found = -1; /* search until found or until end of array */ i = 0; while (found<0 && i<size) { if (arraytosearch[i] == valuetosearch) found = i; /* I have found it! */ else i = i + 1; } return (found); } int main (void) { int x[] = {12,67,56,60,88,34,123}; int value = 60; int pos, i; pos = search (x, value, 7); if (pos >= 0) printf ("%d was found at position %d.\n", value, pos); else printf ("%d was not found in the array.\n", value); return (0); } Legend: preprocessor directives | variable declarations | main program | helper functions | user-defined structures | comments 60 was found at position 3.

Array : Adding Two Arrays

/* Problem: Add the corresponding values from two arrays of the same size. */ #include <stdio.h> /* the function that adds the two arrays a1 and a2. it "returns" a3 back */ void addarrays ( int a1[], int a2[], int a3[], int n ) { int i; /* do the adding of every corresponding cells */ for (i=0; i<n; ++i) a3[i] = a1[i] + a2[i]; } int main (void) { int x[] = {1,2,3,4}, i; int y[] = {10,20,30,40}; int z[4]; /* call the function */ addarrays (x, y, z, 4); /* print a report */ for (i=0; i<4; ++i) printf ("%3d", x[i]); printf ("\n + \n"); for (i=0; i<4; ++i) printf ("%3d", y[i]); printf ("\n-------------\n"); for (i=0; i<4; ++i) printf ("%3d", z[i]); return (0); }

Array : Filling Partially

/* Problem: This program partially fills an array from a file until the end of file (EOF). We get the actual number of data read */ #include <stdio.h> int array_from_file ( double a[], int size ) { int i; FILE* in; in = fopen ("data_array.dat", "r"); i=0; /* the first cell */ /* filling the array cell by cell */ /* until it is full or until the EOF */ while (i < 100 && fscanf (in, "%lf", &a[i]) != EOF) { i=i+1; } fclose (in); /* the actual number of values in the array */ return (i); } int main (void) { double array[100]; int actual_size, i; actual_size = array_from_file (array, 100); for (i=0; i < actual_size; ++i) printf ("%3.1lf ", array[i]); printf ("\nThe array contains %d values ", actual_size);

Array : With Pointer

/* Problem: This programs fills an array with a value submitted by the user. */ #include <stdio.h> /* array parameter can be expressed as a pointer */ /* *list is the same thing as list[] */ void fill_array ( int *list, int n, int in_value ) { int i; for (i=0; i<n; ++i) list[i] = in_value; } int main (void) { int x[100]; int i; /* &x[0] is the address of the x[0] */ /* which is the same thing as x */ fill_array (&x[0], 100, 5); /* printing the array for verification */ for (i=0; i<100; ++i) printf ("%d ", x[i]); return (0); }

Array : Filling from a file

#include <stdio.h> int main (void) { int numbers[10], i; FILE *input; input = fopen("numbers.txt", "r"); /* reading file - filling array */ for (i=0; i<10; ++i) fscanf(input, "%d", &numbers[i]); /* printing the content of array */ printf("The numbers read are: "); for (i=0; i<10; ++i) printf("%4d", numbers[i]); printf ("\n"); fclose (input); return (0); }

Array : Meand & Standard Deviations

/* This program computes the meand and standard deviations of the values inside an array */ #include <stdio.h> #include <math.h> #define MAX 5 int main (void) { double mean, sd, sum, sumsq; double x[] = {10.0, 15.0, 20.0, 10.0, 30.0}; int i; sum = 0; sumsq = 0; /* computing the sum and sum of squares */ for (i=0; i<MAX; ++i) { sum = sum + x[i]; sumsq = sumsq + x[i] * x[i]; } /* computing mean and standard deviation */ mean = sum / MAX; sd = sqrt(sumsq / MAX - mean * mean); /* printing report */ printf ("The mean is %lf. \n", mean); printf ("The standard deviation is %lf. \n", sd); return (0); }