add work so far

master
Konstantine Tsafatinos 2023-11-13 13:18:50 -05:00
parent d44c9e9b98
commit 8799d7699f
27 changed files with 682 additions and 0 deletions

25
intro/args.c 100644
View File

@ -0,0 +1,25 @@
#include <stdio.h>
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;
}

25
intro/arrays.c 100644
View File

@ -0,0 +1,25 @@
#include <stdio.h>
/* 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);
}

View File

@ -0,0 +1,49 @@
#include <stdio.h>
#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;
}

12
intro/count_char.c 100644
View File

@ -0,0 +1,12 @@
#include <stdio.h>
/* count characters input; 1st version */
main()
{
long nc;
nc = 0;
while (getchar() != EOF)
++nc;
printf("%ld\n", nc);
}

View File

@ -0,0 +1,11 @@
#include <stdio.h>
/* count characters input; 2nd version */
main()
{
double nc;
for (nc = 0; getchar() != EOF; ++nc)
;
printf("%.0f\n", nc);
}

View File

@ -0,0 +1,13 @@
#include <stdio.h>
/* count lines in input */
main()
{
int c, nl;
nl = 0;
while((c = getchar()) != EOF)
if (c == '\n')
++nl;
printf("%d\n", nl);
}

View File

@ -0,0 +1,13 @@
#include <stdio.h>
/* 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);
}

View File

@ -0,0 +1,25 @@
#include <stdio.h>
#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);
}

16
intro/ex1_10.c 100644
View File

@ -0,0 +1,16 @@
#include <stdio.h>
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);
}

3
intro/ex1_11.txt 100644
View File

@ -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.

32
intro/ex1_12.c 100644
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#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') ;
}
}
}

36
intro/ex1_13.c 100644
View File

@ -0,0 +1,36 @@
#include <stdio.h>
#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");
}
}

52
intro/ex1_13b.c 100644
View File

@ -0,0 +1,52 @@
#include <stdio.h>
#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");
}

39
intro/ex1_14.c 100644
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#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");
}

30
intro/ex1_15.c 100644
View File

@ -0,0 +1,30 @@
#include <stdio.h>
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;
}

58
intro/ex1_16.c 100644
View File

@ -0,0 +1,58 @@
#include <stdio.h>
#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;
}

33
intro/ex1_17.c 100644
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#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;
}

37
intro/ex1_18.c 100644
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#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;
}

48
intro/ex1_19.c 100644
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#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);
}

18
intro/ex1_9.c 100644
View File

@ -0,0 +1,18 @@
#include <stdio.h>
/* 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;
}
}

24
intro/funcs.c 100644
View File

@ -0,0 +1,24 @@
#include <stdio.h>
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;
}

6
intro/hello.c 100644
View File

@ -0,0 +1,6 @@
#include <stdio.h>
main()
{
printf("hello, world\n");
}

13
intro/read_char.c 100644
View File

@ -0,0 +1,13 @@
#include <stdio.h>
/* copy input to output; 1st version */
main()
{
int c;
c = getchar();
while (c != EOF) {
putchar(c);
c = getchar(c);
}
}

11
intro/read_char2.c 100644
View File

@ -0,0 +1,11 @@
#include <stdio.h>
/* copy input to output; 2nd version */
main()
{
int c;
printf("%d", EOF);
while ((c = getchar()) != EOF)
putchar(c);
}

21
intro/temp.c 100644
View File

@ -0,0 +1,21 @@
#include <stdio.h>
/* 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;
}
}

21
intro/temp2.c 100644
View File

@ -0,0 +1,21 @@
#include <stdio.h>
/* 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;
}
}

11
intro/temp3.c 100644
View File

@ -0,0 +1,11 @@
#include <stdio.h>
/* 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));
}