Vous êtes sur la page 1sur 8

// Week13_SortModules_with_Files.

c
#include <stdio.h> #define MAX_MODULES 10 #define CODE_LENGTH 7 // maximum number of modules // length of module code typedef struct { char code[CODE_LENGTH+1]; int enrolment; } module_t; int scanModules(module_t []); void printModules(module_t [], int); void sortByEnrolment(module_t [], int); int main(void) { module_t modules[MAX_MODULES]; int num_modules; num_modules = scanModules(modules); sortByEnrolment(modules, num_modules); printModules(modules, num_modules); return 0; } // Read number of modules, and for each module, // the module codes and enrolment figures. // Return number of modules. int scanModules(module_t mod[]) { FILE *infile; int size, i; infile = fopen("modules.in", "r"); // open a file for reading fscanf(infile, "%d", &size); for (i=0; i<size; i++) fscanf(infile, "%s %d", mod[i].code, &mod[i].enrolment); fclose(infile); return size; } // Sort by number of students // This uses Selection Sort void printModules(module_t mod[], int size) { FILE *outfile; int i; outfile = fopen("modules.out", "w"); // open a file for writing fprintf(outfile, "Sorted by student enrolment:\n"); for (i=0; i<size; i++) fprintf(outfile, "%s\t%3d\n", mod[i].code, mod[i].enrolment); fclose(outfile); } // Sort by number of students in each module void sortByEnrolment(module_t mod[], int size) { int i, start, min_index;

module_t temp; for (start = 0; start < size-1; start++) { // find index of minimum element min_index = start; for (i = start+1; i < size; i++) if (mod[i].enrolment < mod[min_index].enrolment) min_index = i; // swap minimum element with element at start index temp = mod[start]; mod[start] = mod[min_index]; mod[min_index] = temp; } }

// Week13_CopyFile.c
#include <stdio.h> #include <stdlib.h> #define FNAME_LENGTH 30 void copyFile(char *, char *); int main(void) { char in_fname[FNAME_LENGTH + 1], out_fname[FNAME_LENGTH + 1]; printf("What is the input filename? "); scanf("%s", in_fname); printf("What is the output filename? "); scanf("%s", out_fname); // copy input file to output file copyFile(in_fname, out_fname); return 0; } // Copy text from sourcefile to destfile void copyFile(char *sourcefile, char *destfile) { FILE *sfp, *dfp; int ch; if ((sfp = fopen(sourcefile, "r")) == NULL) exit(1); // error - cannot open source file if ((dfp = fopen(destfile, "w")) == NULL) { fclose(sfp); exit(2); // error - cannot open destination file } while ((ch = fgetc(sfp)) != EOF) { fputc(ch, dfp); } fclose(sfp); fclose(dfp); }

// Week11_countValue.c
// Count the number of times a certain value // appear in an array. #include <stdio.h> #include <stdlib.h> #include <time.h> #define ARRAY_SIZE 15 void initArray(int [], int); void printArray(int [], int); int countValue_iter(int, int [], int); int countValue(int, int [], int); int main(void) { int list[ARRAY_SIZE], value; initArray(list, ARRAY_SIZE); printArray(list, ARRAY_SIZE); printf("Enter search value: "); scanf("%d", &value); printf("Iterative: Number of times the value %d occurs = %d\n", value, countValue_iter(value, list, ARRAY_SIZE)); printf("Recursive: Number of times the value %d occurs = %d\n", value, countValue(value, list, ARRAY_SIZE)); return 0; } // Initialise array elements with random values // in the range [-9, 9] void initArray(int arr[], int size) { int i; srand(time(NULL)); for (i=0; i<size; i++) arr[i] = rand()%19 - 9; } // Print values in array void printArray(int arr[], int size) { int i; for (i=0; i<size; i++) printf("%d ", arr[i]); printf("\n"); } // Count the number of times value appear in arr // Iterative version // Pre-cond: size >= 0 int countValue_iter(int value, int arr[], int size) { int count = 0, i; for (i=0; i<size; i++) if (value == arr[i]) count++; return count;

} // Count the number of times value appear in arr // Recursive version // Pre-cond: size >= 0 int countValue(int value, int arr[], int size) { if (size == 0) return 0; else return (value == arr[size-1]) + countValue(value, arr, size-1); }

// Week11_countValue_auxiliary.c
// Count the number of times a certain value // appear in an array. #include <stdio.h> #include <stdlib.h> #include <time.h> #define ARRAY_SIZE 15 void initArray(int [], int); void printArray(int [], int); int countValue_iter(int, int [], int); int countValue(int, int [], int); int countValue_recur(int, int [], int, int); int main(void) { int list[ARRAY_SIZE], value; initArray(list, ARRAY_SIZE); printArray(list, ARRAY_SIZE); printf("Enter search value: "); scanf("%d", &value); printf("Iterative: Number of times the value %d occurs = %d\n", value, countValue_iter(value, list, ARRAY_SIZE)); printf("Recursive: Number of times the value %d occurs = %d\n", value, countValue(value, list, ARRAY_SIZE)); return 0; } // Initialise array elements with random values // in the range [-9, 9] void initArray(int arr[], int size) { int i; srand(time(NULL)); for (i=0; i<size; i++) arr[i] = rand()%19 - 9; } // Print values in array void printArray(int arr[], int size) { int i;

for (i=0; i<size; i++) printf("%d ", arr[i]); printf("\n"); } // Count the number of times value appear in arr // Iterative version // Pre-cond: size >= 0 int countValue_iter(int value, int arr[], int size) { int count = 0, i; for (i=0; i<size; i++) if (value == arr[i]) count++; return count; } // Driver function for countValue_recur() // Pre-cond: size >= 0 int countValue(int value, int arr[], int size) { return countValue_recur(value, arr, 0, size); } // Count the number of times value appear in arr // Recursive version // Pre-cond: size >= 0 int countValue_recur(int value, int arr[], int start, int size) { if (start == size) return 0; else return (value == arr[start]) + countValue_recur(value, arr, start+1, size); }

// Week10_SortModules.c
#include <stdio.h> #include <string.h> #define MAX_MODULES 10 #define CODE_LENGTH 7 // maximum number of modules // length of module code

int scanModules(char [][CODE_LENGTH+1], int []); void printModules(char [][CODE_LENGTH+1], int [], int); void sortByEnrolment(char [][CODE_LENGTH+1], int [], int); int main(void) { char codes[MAX_MODULES][CODE_LENGTH+1]; // module codes int enrolments[MAX_MODULES]; // enrolment in each module int num_modules; num_modules = scanModules(codes, enrolments); sortByEnrolment(codes, enrolments, num_modules); printModules(codes, enrolments, num_modules); return 0; }

// Read number of modules, and for each module, the module codes and enrolment figures. // Return number of modules. int scanModules(char codes[][CODE_LENGTH+1], int enrolments[]) { int size, i; printf("Enter number of modules: "); scanf("%d", &size); printf("Enter module codes and student enrolment:\n"); for (i=0; i<size; i++) scanf("%s %d", codes[i], &enrolments[i]); return size; } // Print module codes and enrolment figures void printModules(char codes[][CODE_LENGTH+1], int enrolments[], int size) { int i; printf("Sorted by student enrolment:\n"); for (i=0; i<size; i++) printf("%s\t%3d\n", codes[i], enrolments[i]); } // Sort by enrolment in each module // This uses Selection Sort void sortByEnrolment(char codes[][CODE_LENGTH+1], int enrolments[], int size) { int i, start, min_index; char temp_code[CODE_LENGTH]; int temp_enrol; for (start = 0; start < size-1; start++) { // find index of minimum element min_index = start; for (i = start+1; i < size; i++) if (enrolments[i] < enrolments[min_index]) min_index = i; // swap minimum element with element at start index strcpy(temp_code, codes[start]); strcpy(codes[start], codes[min_index]); strcpy(codes[min_index], temp_code); temp_enrol = enrolments[start]; enrolments[start] = enrolments[min_index]; enrolments[min_index] = temp_enrol; } }

// Note: Next year, change the return type of scanPoints to int

// Week10_Point_Complete.c
// This program reads a list of points, sorts them by x-coordinates // and y-coordinates, and computes the total length of horizontal and // vertical lines if we trace the points in the sorted list. // Aaron Tan #include <stdio.h> #define MAX_POINTS 20 void scanPoints(int [], int [], int *); void printPoints(int [], int [], int); void sortPoints(int [], int [], int); int lessThan(int [], int [], int, int); int traceLines(int [], int [], int); int main(void) { int x[MAX_POINTS], y[MAX_POINTS]; // x- and y-coordinates of points int num_points; // number of points scanPoints(x, y, &num_points); // printf("Before sort:\n"); // printPoints(x, y, num_points); // for checking sortPoints(x, y, num_points); printf("After sort:\n"); printPoints(x, y, num_points); // for checking printf("Sum of lengths of vertical and horizontal lines = %d\n", traceLines(x, y, num_points)); return 0; } // Read data for points and assign to arrays x and y void scanPoints(int x[], int y[], int *size_ptr) { int i; printf("Enter number of points: "); scanf("%d", size_ptr); printf("Enter x- and y-coordinates of %d points:\n", *size_ptr); for (i=0; i<*size_ptr; i++) scanf("%d %d", &x[i], &y[i]); } // Print points void printPoints(int x[], int y[], int size) { int i; for (i=0; i<size; i++) printf("Point #%2d: (%d,%d)\n", i, x[i], y[i]); } // Sort the points in ascending order of x-coordinates and // then y-coordinates, using Selection Sort. void sortPoints(int x[], int y[], int size) { int i, start, min_index, temp;

for (start = 0; start < size-1; start++) { min_index = start; for (i = start+1; i < size; i++) // check if point at index i is "less than" point at min_index if ( lessThan(x, y, i, min_index) ) min_index = i; // swap minimum element with element at start index temp = x[start]; x[start] = x[min_index]; x[min_index] = temp; temp = y[start]; y[start] = y[min_index]; y[min_index] = temp; } } // Returns 1 if point at index p is "less than" point at index q; // otherwise returns 0. // Point at index p is "less than" point at index q if the former // has a smaller x-coordinate, or if their x-coordinates are the // same, then the former has a smaller y-coordinate. int lessThan(int x[], int y[], int p, int q) { return (x[p] < x[q]) || ((x[p] == x[q]) && (y[p] < y[q])); } // Trace lines on points in sorted array and compute the // sum of lengths of horizontal and vertical lines int traceLines(int x[], int y[], int size) { int i, sum; sum = 0; // sum of lengths of horizontal and vertical lines for (i=1; i<size; i++) if ( (x[i] == x[i-1]) || y[i] == y[i-1] ) sum += (x[i] - x[i-1]) + (y[i] - y[i-1]); return sum; }

Vous aimerez peut-être aussi