From 442a49ad5a48d417345959b903ae6a6d32d55759 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Fri, 15 Apr 2022 15:51:30 -0500 Subject: Great C programming fun Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git --- 33_counts/Makefile | 12 ++++++++++ 33_counts/README | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 33_counts/counts.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 33_counts/counts.c~ | 31 ++++++++++++++++++++++++++ 33_counts/counts.h | 23 +++++++++++++++++++ 33_counts/counts.h~ | 20 +++++++++++++++++ 33_counts/counts.o | Bin 0 -> 57304 bytes 33_counts/counts_test | Bin 0 -> 54216 bytes 33_counts/counts_test.c | 18 +++++++++++++++ 33_counts/counts_test.o | Bin 0 -> 55720 bytes 33_counts/grade.txt | 19 ++++++++++++++++ 11 files changed, 237 insertions(+) create mode 100644 33_counts/Makefile create mode 100644 33_counts/README create mode 100644 33_counts/counts.c create mode 100644 33_counts/counts.c~ create mode 100644 33_counts/counts.h create mode 100644 33_counts/counts.h~ create mode 100644 33_counts/counts.o create mode 100755 33_counts/counts_test create mode 100644 33_counts/counts_test.c create mode 100644 33_counts/counts_test.o create mode 100644 33_counts/grade.txt (limited to '33_counts') diff --git a/33_counts/Makefile b/33_counts/Makefile new file mode 100644 index 0000000..700ebd4 --- /dev/null +++ b/33_counts/Makefile @@ -0,0 +1,12 @@ +CFLAGS=-Wall -Werror -std=gnu99 -pedantic -ggdb3 +OBJS=counts.o counts_test.o +PROGRAM=counts_test + +$(PROGRAM): $(OBJS) + gcc $(CFLAGS) -o $@ $(OBJS) + +%.o: %.c counts.h + gcc -c $(CFLAGS) $< + +clean: + rm -f $(OBJS) $(PROGRAM) *~ diff --git a/33_counts/README b/33_counts/README new file mode 100644 index 0000000..986af21 --- /dev/null +++ b/33_counts/README @@ -0,0 +1,58 @@ +For this problem, we will address the following tasks: + + - print the counts to a file + - free the memory for the counts + +We'll note that we are not going to write the part of this program where +we read the input file and compute the counts until the next problem. However, we will +still want to be able to test our code. We can do this, by having a main +function which constructs the counts from a hard coded set of data, skipping +the details of the actual program (this is an example of a test scaffold). + +Our test scaffold can benefit from some functionality that (if we think a bit ahead) +will be useful to abstract out into a couple functions, so we can re-use that code +in the next problem. (Abstracting all of this code out into function is also good because +it hides the implementation details: none of the code in the main function +we provide cares what the names/types of the fields in the counts_t structure +are, which you will make shortly). + +First, go to counts.h. Here, you will find two empty struct declarations. You will +need to fill these in. The first should reflect the information about one count. +That is, for some particular string, how many times have we seen it so far. +The second, should have an array of the first, as well as the size of that array. +You should also include a field in this struct to count unknown names. + +Next, you should go to counts.c, and write the four functions there. + +The first, createCounts should allocate memory for a counts_t structure, and initialize +it to represent that nothing has been counted yet. + +The next function, addCount, should increment the count for the corresponding name. Note +that name will be NULL in the case of something that is unknown, so your code must account +for this case. + +The third function, printCounts takes a counts_t and prints that information to +the FILE outFile. Recall from the description of the entire problem, that this +function should print in the format: + +Captain: 1 +Commander: 2 +Lt. Commander: 1 + : 1 + +These should appear in the order that the name is first added, with unknown always +appearing last. + +***If there are no unknown values, you should not print a line for unknown. That +is, you should NEVEr print + : 0 + + +Finally, you should write freeCounts, which should free all the memory associated with +a counts_t. + +We have provided a main in countsTestc which creates a counts_t (using your createCounts +function), adds some names to it (using your addCount function), prints the result +to stdout (using your printCounts) function, then frees the memory (using your freeCounts). + +Test and debug these functions before proceeding. diff --git a/33_counts/counts.c b/33_counts/counts.c new file mode 100644 index 0000000..c69f9aa --- /dev/null +++ b/33_counts/counts.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include "counts.h" +counts_t * createCounts(void) { + counts_t * c = malloc(sizeof(* c)); + c->countArray = NULL; + c->arraySize = 0; + c->count_of_unknowns = 0; + return c; +} + +void addCount(counts_t * c, const char * name) { + if (name == NULL) { + c->count_of_unknowns++; + return; + } + + for (int i = 0; i < c->arraySize; i++) { + if (strcmp(name, c->countArray[i].string) == 0) { + c->countArray[i].count++; + return; + } + } + + one_count_t * newCount = malloc(sizeof(one_count_t)); + newCount->string = malloc((strlen(name) + 1) * sizeof(char)); + strcpy(newCount->string, name); + newCount->count = 1; + c->countArray = realloc(c->countArray, (c->arraySize + 1) * sizeof(one_count_t)); + c->countArray[c->arraySize] = *newCount; + c->arraySize++; + free(newCount); + +} +void printCounts(counts_t * c, FILE * outFile) { + for (int i = 0; i < c->arraySize; i++) { + fprintf(outFile, "%s: %d\n", c->countArray[i].string, c->countArray[i].count); + } + + if (c->count_of_unknowns > 0) { + fprintf(outFile, "%s: %d\n", " ", c->count_of_unknowns); + } + + if(fclose(outFile) != 0) { + fprintf(stderr, "oh no, couldn't close the file!\n"); + } +} + +void freeCounts(counts_t * c) { + for (int i = 0; i < c->arraySize; i++) { + free(c->countArray[i].string); + } + free(c->countArray); + free(c); +} diff --git a/33_counts/counts.c~ b/33_counts/counts.c~ new file mode 100644 index 0000000..819726a --- /dev/null +++ b/33_counts/counts.c~ @@ -0,0 +1,31 @@ +#include +#include +#include +#include "counts.h" +counts_t * createCounts(void) { + counts_t c = malloc(sizeof(counts_t)); + c.countArray = NULL; + c.arraySize = 0; + c.count_of_unknowns = 0; +} + +void addCount(counts_t * c, const char * name) { + if (name == NULL) { + c.count_of_unknowns++; + return; + } + + for (int i = 0; i < c.arraySize; i++) { + if (strcmp(name, c.countArray[i].string) == 0) { + c.countArray[i].count++; + return; + } + } +} +void printCounts(counts_t * c, FILE * outFile) { + //WRITE ME +} + +void freeCounts(counts_t * c) { + //WRITE ME +} diff --git a/33_counts/counts.h b/33_counts/counts.h new file mode 100644 index 0000000..cf0783c --- /dev/null +++ b/33_counts/counts.h @@ -0,0 +1,23 @@ +#include +#ifndef __COUNTS_H__ +#define __COUNTS_H__ +struct _one_count_t { + char * string; + int count; +}; +typedef struct _one_count_t one_count_t; + +struct _counts_t { + one_count_t * countArray; + int arraySize; + int count_of_unknowns; +}; +typedef struct _counts_t counts_t; + +counts_t * createCounts(void); +void addCount(counts_t * c, const char * name); +void printCounts(counts_t * c, FILE * outFile); + +void freeCounts(counts_t * c); + +#endif diff --git a/33_counts/counts.h~ b/33_counts/counts.h~ new file mode 100644 index 0000000..18f28c4 --- /dev/null +++ b/33_counts/counts.h~ @@ -0,0 +1,20 @@ +#ifndef __COUNTS_H__ +#define __COUNTS_H__ +struct _one_count_t { + //DEFINE ME + +}; +typedef struct _one_count_t one_count_t; + +struct _counts_t { + //DEFINE ME +}; +typedef struct _counts_t counts_t; + +counts_t * createCounts(void); +void addCount(counts_t * c, const char * name); +void printCounts(counts_t * c, FILE * outFile); + +void freeCounts(counts_t * c); + +#endif diff --git a/33_counts/counts.o b/33_counts/counts.o new file mode 100644 index 0000000..00cf023 Binary files /dev/null and b/33_counts/counts.o differ diff --git a/33_counts/counts_test b/33_counts/counts_test new file mode 100755 index 0000000..fd87286 Binary files /dev/null and b/33_counts/counts_test differ diff --git a/33_counts/counts_test.c b/33_counts/counts_test.c new file mode 100644 index 0000000..905d918 --- /dev/null +++ b/33_counts/counts_test.c @@ -0,0 +1,18 @@ +#include +#include +#include +#include "counts.h" + +#define NUM_TESTS 12 +int main(void) { + char * testData[NUM_TESTS] = {"apple", "banana", NULL,"apple", + "frog","sword","bear",NULL, + "frog","apple", "zebra", "knight"}; + counts_t * testCounts= createCounts(); + for(int i =0; i < NUM_TESTS; i++) { + addCount(testCounts,testData[i]); + } + printCounts(testCounts, stdout); + freeCounts(testCounts); + return EXIT_SUCCESS; +} diff --git a/33_counts/counts_test.o b/33_counts/counts_test.o new file mode 100644 index 0000000..6da765e Binary files /dev/null and b/33_counts/counts_test.o differ diff --git a/33_counts/grade.txt b/33_counts/grade.txt new file mode 100644 index 0000000..844521f --- /dev/null +++ b/33_counts/grade.txt @@ -0,0 +1,19 @@ +Grading at Sat 25 Dec 2021 01:42:38 AM UTC +Attempting to compile: +################################################# +testcase1: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase2: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean +################################################# +testcase3: +Your output is correct + - Valgrind was clean (no errors, no memory leaks) +valgrind was clean + +Overall Grade: A -- cgit v1.2.3