From 8799d7699fb4eb967601ef92c0dc0ae7c6d974ba Mon Sep 17 00:00:00 2001 From: Konstantine Tsafatinos Date: Mon, 13 Nov 2023 13:18:50 -0500 Subject: [PATCH] add work so far --- intro/args.c | 25 +++++++++++++++++++ intro/arrays.c | 25 +++++++++++++++++++ intro/char_arrays.c | 49 +++++++++++++++++++++++++++++++++++++ intro/count_char.c | 12 +++++++++ intro/count_char2.c | 11 +++++++++ intro/count_lines.c | 13 ++++++++++ intro/count_lines2.c | 13 ++++++++++ intro/count_words.c | 25 +++++++++++++++++++ intro/ex1_10.c | 16 ++++++++++++ intro/ex1_11.txt | 3 +++ intro/ex1_12.c | 32 ++++++++++++++++++++++++ intro/ex1_13.c | 36 +++++++++++++++++++++++++++ intro/ex1_13b.c | 52 +++++++++++++++++++++++++++++++++++++++ intro/ex1_14.c | 39 +++++++++++++++++++++++++++++ intro/ex1_15.c | 30 +++++++++++++++++++++++ intro/ex1_16.c | 58 ++++++++++++++++++++++++++++++++++++++++++++ intro/ex1_17.c | 33 +++++++++++++++++++++++++ intro/ex1_18.c | 37 ++++++++++++++++++++++++++++ intro/ex1_19.c | 48 ++++++++++++++++++++++++++++++++++++ intro/ex1_9.c | 18 ++++++++++++++ intro/funcs.c | 24 ++++++++++++++++++ intro/hello.c | 6 +++++ intro/read_char.c | 13 ++++++++++ intro/read_char2.c | 11 +++++++++ intro/temp.c | 21 ++++++++++++++++ intro/temp2.c | 21 ++++++++++++++++ intro/temp3.c | 11 +++++++++ 27 files changed, 682 insertions(+) create mode 100644 intro/args.c create mode 100644 intro/arrays.c create mode 100644 intro/char_arrays.c create mode 100644 intro/count_char.c create mode 100644 intro/count_char2.c create mode 100644 intro/count_lines.c create mode 100644 intro/count_lines2.c create mode 100644 intro/count_words.c create mode 100644 intro/ex1_10.c create mode 100644 intro/ex1_11.txt create mode 100644 intro/ex1_12.c create mode 100644 intro/ex1_13.c create mode 100644 intro/ex1_13b.c create mode 100644 intro/ex1_14.c create mode 100644 intro/ex1_15.c create mode 100644 intro/ex1_16.c create mode 100644 intro/ex1_17.c create mode 100644 intro/ex1_18.c create mode 100644 intro/ex1_19.c create mode 100644 intro/ex1_9.c create mode 100644 intro/funcs.c create mode 100644 intro/hello.c create mode 100644 intro/read_char.c create mode 100644 intro/read_char2.c create mode 100644 intro/temp.c create mode 100644 intro/temp2.c create mode 100644 intro/temp3.c diff --git a/intro/args.c b/intro/args.c new file mode 100644 index 0000000..c2f6b6a --- /dev/null +++ b/intro/args.c @@ -0,0 +1,25 @@ +#include + +int power(int m, int n); + +/* test power function */ +main() +{ + int i; + + for (i =0; i < 10; ++i) + printf("%d %d %d\n", i, power(2,i), power(-3,i)); + return 0; +} + +/* power: raise base to n-th power; n >= 0; version 2 */ +/* arguments passed into funcs are copies of the value and + are not altering the passed in var; "by value" */ +int power(int base, int n) +{ + int p; + + for (p = 1; n > 0; --n) + p = p * base; + return p; +} diff --git a/intro/arrays.c b/intro/arrays.c new file mode 100644 index 0000000..f84ca08 --- /dev/null +++ b/intro/arrays.c @@ -0,0 +1,25 @@ +#include + +/* count digits, white spaces, others */ +main() +{ + int c, i, nwhite, nother; + int ndigit[10]; + + nwhite = nother = 0; + for (i = 0; i < 10; ++i) + ndigit[i] = 0; + + while ((c = getchar()) != EOF) + if (c >= '0' && c <= '9') + ++ndigit[c-'0']; + else if (c == ' ' || c == '\n' || c == '\t') + ++nwhite; + else + ++nother; + + printf("digits ="); + for (i = 0; i < 10; ++i) + printf(" %d", ndigit[i]); + printf(", white space = %d, other = %d\n", nwhite, nother); +} diff --git a/intro/char_arrays.c b/intro/char_arrays.c new file mode 100644 index 0000000..77445c5 --- /dev/null +++ b/intro/char_arrays.c @@ -0,0 +1,49 @@ +#include +#define MAXLINE 1000 /* maximum input line size */ + +int get_line(char line[], int maxline); +void copy(char to[], char from[]); + +/* print longest input line */ +main() +{ + int len; + int max; + char line[MAXLINE]; + char longest[MAXLINE]; + + max = 0; + while ((len = get_line(line, MAXLINE)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) /* there was a line */ + printf("%s", longest); + return 0; +} + +/* getline: read a line into s, return lenght */ +int get_line(char s[], int lim) +{ + int c, i; + + for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) + s[i] = c; + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + return i; +} + +/* copy: cope 'from' in 'to'; assume to is big enough */ +void copy(char to[], char from[]) +{ + int i; + + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} diff --git a/intro/count_char.c b/intro/count_char.c new file mode 100644 index 0000000..6ead4f7 --- /dev/null +++ b/intro/count_char.c @@ -0,0 +1,12 @@ +#include + +/* count characters input; 1st version */ +main() +{ + long nc; + + nc = 0; + while (getchar() != EOF) + ++nc; + printf("%ld\n", nc); +} diff --git a/intro/count_char2.c b/intro/count_char2.c new file mode 100644 index 0000000..6cdcc4d --- /dev/null +++ b/intro/count_char2.c @@ -0,0 +1,11 @@ +#include + +/* count characters input; 2nd version */ +main() +{ + double nc; + + for (nc = 0; getchar() != EOF; ++nc) + ; + printf("%.0f\n", nc); +} diff --git a/intro/count_lines.c b/intro/count_lines.c new file mode 100644 index 0000000..ae542dc --- /dev/null +++ b/intro/count_lines.c @@ -0,0 +1,13 @@ +#include + +/* count lines in input */ +main() +{ + int c, nl; + + nl = 0; + while((c = getchar()) != EOF) + if (c == '\n') + ++nl; + printf("%d\n", nl); +} diff --git a/intro/count_lines2.c b/intro/count_lines2.c new file mode 100644 index 0000000..1cc0dd0 --- /dev/null +++ b/intro/count_lines2.c @@ -0,0 +1,13 @@ +#include + +/* count lines in input */ +main() +{ + int c, nl; + + nl = 0; + while((c = getchar()) != EOF) + if (c == '\n' || c == ' ' || c == '\t') + ++nl; + printf("%d\n", nl); +} diff --git a/intro/count_words.c b/intro/count_words.c new file mode 100644 index 0000000..2c2788d --- /dev/null +++ b/intro/count_words.c @@ -0,0 +1,25 @@ +#include + +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* count lines, words and characters in input */ +main() +{ + int c, nl, nw, nc, state; + + state = OUT; + nl = nw = nc = 0; + while((c = getchar()) != EOF) { + ++nc; + if (c == '\n') + ++nl; + if (c == ' ' || c == '\n' || c == '\t') + state = OUT; + else if (state == OUT) { + state = IN; + ++nw; + } + } + printf("%d %d %d\n", nl, nw, nc); +} diff --git a/intro/ex1_10.c b/intro/ex1_10.c new file mode 100644 index 0000000..92d073c --- /dev/null +++ b/intro/ex1_10.c @@ -0,0 +1,16 @@ +#include + +main() +{ + int c; + + while((c = getchar()) != EOF) + if (c == '\t') + printf("\\t"); + else if (c == '\b') + printf("\\b"); + else if (c == '\\') + printf("\\\\"); + else + putchar(c); +} diff --git a/intro/ex1_11.txt b/intro/ex1_11.txt new file mode 100644 index 0000000..fff36e6 --- /dev/null +++ b/intro/ex1_11.txt @@ -0,0 +1,3 @@ +You could test the word count program by passing in all types of text. + +Different characters, i.e special one, or ones outside of ANSI would likely cause bugs to show up. diff --git a/intro/ex1_12.c b/intro/ex1_12.c new file mode 100644 index 0000000..4692297 --- /dev/null +++ b/intro/ex1_12.c @@ -0,0 +1,32 @@ +#include + +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* count lines, words and characters in input */ +main() +{ + int c, state; + + state = OUT; + while((c = getchar()) != EOF) { + // if (c == ' ' || c == '\t' || c == '\n') + // state = OUT; + // else if (state == OUT) { + // putchar('\n'); + // state = IN; + // putchar(c); + // } + // else + // putchar(c); + if (c != ' ' && c != '\t' && c != '\n') { + if (state == OUT) + state = IN ; + putchar(c) ; + } + else if (state == IN) { + state = OUT ; + putchar('\n') ; + } + } +} diff --git a/intro/ex1_13.c b/intro/ex1_13.c new file mode 100644 index 0000000..b03e028 --- /dev/null +++ b/intro/ex1_13.c @@ -0,0 +1,36 @@ +#include + +#define MAXWORDLENGTH 10 +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* write a histogram of length of words in input */ +main() +{ + int c, nc, i, j, state; + int lengths_arr[MAXWORDLENGTH]; + + for (i = 0; i < MAXWORDLENGTH; ++i) + lengths_arr[i] = 0; + + state = OUT; + while((c = getchar()) != EOF) { + if (c != ' ' && c != '\t' && c != '\n') { + if (state == OUT) + state = IN ; + ++nc; + } + else if (state == IN) { + state = OUT ; + if (nc <= MAXWORDLENGTH) + ++lengths_arr[nc-1]; + nc = 0; + } + } + for (i = 0; i < MAXWORDLENGTH; ++i) { + printf(" %2d|", i+1); + for (j = 0; j < lengths_arr[i]; ++j) + printf("="); + printf("\n"); + } +} diff --git a/intro/ex1_13b.c b/intro/ex1_13b.c new file mode 100644 index 0000000..df7b110 --- /dev/null +++ b/intro/ex1_13b.c @@ -0,0 +1,52 @@ +#include + +#define MAXWORDLENGTH 10 +#define IN 1 /* inside a word */ +#define OUT 0 /* outside a word */ + +/* write a histogram of length of words in input */ +main() +{ + int c, nc, i, j, state; + int lengths_arr[MAXWORDLENGTH]; + int max_freq = 0; + + for (i = 0; i < MAXWORDLENGTH; ++i) + lengths_arr[i] = 0; + + state = OUT; + while((c = getchar()) != EOF) { + if (c != ' ' && c != '\t' && c != '\n') { + if (state == OUT) + state = IN ; + ++nc; + } + else if (state == IN) { + state = OUT ; + if (nc <= MAXWORDLENGTH) + ++lengths_arr[nc-1]; + nc = 0; + } + } + + for (i = 0; i < MAXWORDLENGTH; ++i) + if (lengths_arr[i] > max_freq) + max_freq = lengths_arr[i]; + + for (i = max_freq; i > 0 ; --i) { + for (j = 0; j < MAXWORDLENGTH; ++j) + if (i <= lengths_arr[j]) + printf(" *"); + else + printf(" "); + printf("\n"); + } + + for (i = 0; i < MAXWORDLENGTH; ++i) + printf("---"); + printf("\n"); + + for (i = 0; i < MAXWORDLENGTH; ++i) + printf(" %2d", i+1); + printf("\n"); +} diff --git a/intro/ex1_14.c b/intro/ex1_14.c new file mode 100644 index 0000000..5a9dfeb --- /dev/null +++ b/intro/ex1_14.c @@ -0,0 +1,39 @@ +#include + +#define MAXNUMCHARS 94 /* values between 33 and 126 */ + +/* write a histogram of length of words in input */ +main() +{ + int c, i, j; + int char_freqs[MAXNUMCHARS]; + int max_freq = 0; + + for (i = 0; i < MAXNUMCHARS; ++i) + char_freqs[i] = 0; + + while((c = getchar()) != EOF) { + if (33 <= c && c <= 126) { + ++char_freqs[c-33]; + if (char_freqs[c-33] > max_freq) + max_freq = char_freqs[c-33]; + } + } + + for (i = max_freq; i > 0 ; --i) { + for (j = 0; j < MAXNUMCHARS; ++j) + if (i <= char_freqs[j]) + printf("*"); + else + printf(" "); + printf("\n"); + } + + for (i = 0; i < MAXNUMCHARS; ++i) + printf("-"); + printf("\n"); + + for (i = 0; i < MAXNUMCHARS; ++i) + printf("%c", i+33); + printf("\n"); +} diff --git a/intro/ex1_15.c b/intro/ex1_15.c new file mode 100644 index 0000000..7a23be4 --- /dev/null +++ b/intro/ex1_15.c @@ -0,0 +1,30 @@ +#include + +int convertctof(int celsius); +/* print Celsius-Fahrenheit table + for celsius = 0, 20, ..., 300; functafied */ +main() +{ + float fahr, celsius; + int lower, upper, step; + + lower = 0; /* lower limit of temperature table */ + upper = 300; /* upper limit */ + step = 20; /* step size */ + + celsius = lower; + printf("Celsius-Fahrenheit Table\n"); + while (celsius <= upper) { + fahr = convertctof(celsius); + printf("%3.0f %6.2f\n", celsius, fahr); + celsius = celsius + step; + } +} + +int convertctof(int cel) +{ + int fahr; + + fahr = cel * (9.0/5.0) + 32.0; + return fahr; +} diff --git a/intro/ex1_16.c b/intro/ex1_16.c new file mode 100644 index 0000000..d6b5415 --- /dev/null +++ b/intro/ex1_16.c @@ -0,0 +1,58 @@ +#include +#define MAXLINE 1000 /* maximum input line size */ + +int get_line(char line[], int maxline); +void copy(char to[], char from[]); + +/* print longest input line */ +main() +{ + int len; + int max; + char line[MAXLINE]; + char longest[MAXLINE]; + + max = 0; + while ((len = get_line(line, MAXLINE)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) /* there was a line */ + if (max > MAXLINE) { + printf("\n\nStorage limit exceeded by : %d", max-MAXLINE); + printf("\nString length : %d", max); + printf("\n%s", longest); + } + else + printf("%s", longest); + return 0; +} + +/* getline: read a line into s, return lenght */ +int get_line(char s[], int lim) +{ + int c, i; + + for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) + if (i < lim-1) + s[i] = c; + if (i < lim-1) { + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + } + return i; +} + +/* copy: cope 'from' in 'to'; assume to is big enough */ +void copy(char to[], char from[]) +{ + int i; + + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} diff --git a/intro/ex1_17.c b/intro/ex1_17.c new file mode 100644 index 0000000..d7a8291 --- /dev/null +++ b/intro/ex1_17.c @@ -0,0 +1,33 @@ +#include +#define MAXLINE 1000 /* maximum input line size */ + +int get_line(char line[], int maxline); + +/* print longest input line */ +main() +{ + int len; + char line[MAXLINE]; + + while ((len = get_line(line, MAXLINE)) > 0) + if (len > 80) + printf("%s\n", line); + else + printf("line < 80 \n"); + return 0; +} + +/* getline: read a line into s, return lenght */ +int get_line(char s[], int lim) +{ + int c, i; + + for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) + s[i] = c; + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + return i; +} diff --git a/intro/ex1_18.c b/intro/ex1_18.c new file mode 100644 index 0000000..bcda583 --- /dev/null +++ b/intro/ex1_18.c @@ -0,0 +1,37 @@ +#include +#define MAXLINE 1000 /* maximum input line size */ + +int get_line(char line[], int maxline); + +/* print longest input line */ +main() +{ + int len; + char line[MAXLINE]; + + while ((len = get_line(line, MAXLINE)) > 0) + if (len > 1 && line[0] != ' ' && line[0] != '\t') + printf("%s\n", line); + return 0; +} + +/* getline: read a line into s, return lenght */ +int get_line(char s[], int lim) +{ + int c, i; + + for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) + if (i == 0) + s[i] = c; + else if ((c == ' ' || c == '\t') && (s[i-1] == ' ' || s[i-1] == '\t')) + --i; + else if (s[i-1] != ' ' || s[i-1] != '\t') + s[i] = c; + + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + return i; +} diff --git a/intro/ex1_19.c b/intro/ex1_19.c new file mode 100644 index 0000000..25fee4f --- /dev/null +++ b/intro/ex1_19.c @@ -0,0 +1,48 @@ +#include +#define MAXLINE 1000 /* maximum input line size */ + +int get_line(char line[], int maxline); +void myreverse(char to[], char from[], int len); + +/* print longest input line */ +main() +{ + int len; + char line[MAXLINE]; + char reverse_line[MAXLINE]; + + while ((len = get_line(line, MAXLINE)) > 0) { + myreverse(reverse_line, line, len); + printf("%s", reverse_line); + } + return 0; +} + +/* getline: read a line into s, return length */ +int get_line(char s[], int lim) +{ + int c, i; + + for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) + s[i] = c; + if (c == '\n') { + s[i] = c; + ++i; + } + s[i] = '\0'; + return i; +} + +/* reverse: reverse a char string */ +void myreverse(char to[], char from[], int len) +{ + int i; + to[len] = '\0'; + to[len-1] = '\n'; + + for (i = 0; i < len-1; ++i) + to[len-2-i] = from[i]; + + // printf("%s", from); + // printf("%s", to); +} diff --git a/intro/ex1_9.c b/intro/ex1_9.c new file mode 100644 index 0000000..4eb39e0 --- /dev/null +++ b/intro/ex1_9.c @@ -0,0 +1,18 @@ +#include + +/* count lines in input */ +main() +{ + int c, nb; + + nb = 0; + while((c = getchar()) != EOF) + if (c != ' ') { + putchar(c); + nb = 0; + } + else if ((c == ' ') && (nb == 0)) { + putchar(c); + ++nb; + } +} diff --git a/intro/funcs.c b/intro/funcs.c new file mode 100644 index 0000000..37caaeb --- /dev/null +++ b/intro/funcs.c @@ -0,0 +1,24 @@ +#include + +int power(int m, int n); + +/* test power function */ +main() +{ + int i; + + for (i =0; i < 10; ++i) + printf("%d %d %d\n", i, power(2,i), power(-3,i)); + return 0; +} + +/* power: raise base to n-th power; n >= 0 */ +int power(int base, int n) +{ + int i, p; + + p = 1; + for (i = 1; i <= n; ++i) + p = p * base; + return p; +} diff --git a/intro/hello.c b/intro/hello.c new file mode 100644 index 0000000..fed1711 --- /dev/null +++ b/intro/hello.c @@ -0,0 +1,6 @@ +#include + +main() +{ + printf("hello, world\n"); +} diff --git a/intro/read_char.c b/intro/read_char.c new file mode 100644 index 0000000..09012c1 --- /dev/null +++ b/intro/read_char.c @@ -0,0 +1,13 @@ +#include + +/* copy input to output; 1st version */ +main() +{ + int c; + + c = getchar(); + while (c != EOF) { + putchar(c); + c = getchar(c); + } +} diff --git a/intro/read_char2.c b/intro/read_char2.c new file mode 100644 index 0000000..b4a22bf --- /dev/null +++ b/intro/read_char2.c @@ -0,0 +1,11 @@ +#include + +/* copy input to output; 2nd version */ +main() +{ + int c; + + printf("%d", EOF); + while ((c = getchar()) != EOF) + putchar(c); +} diff --git a/intro/temp.c b/intro/temp.c new file mode 100644 index 0000000..112b020 --- /dev/null +++ b/intro/temp.c @@ -0,0 +1,21 @@ +#include + +/* print Fahrenheit-Celsius table + for fahr = 0, 20, ..., 300 */ +main() +{ + float fahr, celsius; + int lower, upper, step; + + lower = 0; /* lower limit of temperature table */ + upper = 300; /* upper limit */ + step = 20; /* step size */ + + fahr = lower; + printf("Fahrenheit-Celsius Table\n"); + while (fahr <= upper) { + celsius = (5.0/9.0) * (fahr-32.0); + printf("%3.0f %6.2f\n", fahr, celsius); + fahr = fahr + step; + } +} diff --git a/intro/temp2.c b/intro/temp2.c new file mode 100644 index 0000000..c51bf0c --- /dev/null +++ b/intro/temp2.c @@ -0,0 +1,21 @@ +#include + +/* print Celsius-Fahrenheit table + for celsius = 0, 20, ..., 300 */ +main() +{ + float fahr, celsius; + int lower, upper, step; + + lower = 0; /* lower limit of temperature table */ + upper = 300; /* upper limit */ + step = 20; /* step size */ + + celsius = lower; + printf("Celsius-Fahrenheit Table\n"); + while (celsius <= upper) { + fahr = celsius * (9.0/5.0) + 32.0; + printf("%3.0f %6.2f\n", celsius, fahr); + celsius = celsius + step; + } +} diff --git a/intro/temp3.c b/intro/temp3.c new file mode 100644 index 0000000..f1ffdd9 --- /dev/null +++ b/intro/temp3.c @@ -0,0 +1,11 @@ +#include + +/* print Fahrenheit-Celsius table */ +main() +{ + int fahr; + + // for (fahr = 0; fahr <= 300; fahr = fahr + 20) + for (fahr = 300; fahr >= 0; fahr = fahr - 20) + printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); +}