Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

up to ex16

  • Loading branch information...
commit 2bd7550c668157cded811e65765a8dc59e0d9811 1 parent 834aebc
@wookiehangover authored
Showing with 240 additions and 1 deletion.
  1. +2 −1  Makefile
  2. +22 −0 ex12.c
  3. +48 −0 ex13.c
  4. +36 −0 ex14.c
  5. +62 −0 ex15.c
  6. +70 −0 ex16.c
View
3  Makefile
@@ -1,7 +1,8 @@
CFLAGS=-Wall -g
-SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10
+SOURCES=ex1 ex4 ex6 ex7 ex8 ex9 ex10 ex11 ex12 ex13 ex14 ex15 ex16
all: $(SOURCES)
clean:
rm -f $(SOURCES)
+ rm -rf *.dSYM
View
22 ex12.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ int i = 0;
+
+ if( argc == 1 ){
+ printf("You only have one arg.");
+ } else if(argc > 1 && argc < 4) {
+ printf("Here are your args:\n");
+
+ for(i = 0; i < argc; i++) {
+ printf("%s ", argv[i]);
+ }
+
+ printf("\n");
+ } else {
+ printf("You have too many examples. You suck. \n");
+ }
+
+ return 0;
+}
View
48 ex13.c
@@ -0,0 +1,48 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ if(argc != 2) {
+ printf("Error: you need one arg. \n");
+ return 1;
+ }
+
+ int i = 0;
+ for(i = 0; argv[1][i] != '\0'; i++) {
+ char letter = argv[1][i];
+
+ switch( letter ) {
+ case 'a':
+ case 'A':
+ printf("%d: 'A'\n", i);
+ break;
+ case 'e':
+ case 'E':
+ printf("%d: 'E'\n", i);
+ break;
+ case 'i':
+ case 'I':
+ printf("%d: 'I'\n", i);
+ break;
+ case 'o':
+ case 'O':
+ printf("%d: 'O'\n", i);
+ break;
+ case 'u':
+ case 'U':
+ printf("%d: 'U'\n", i);
+ break;
+ case 'y':
+ case 'Y':
+ if(i > 2) {
+ printf("%d: 'Y'\n", i);
+ }
+ break;
+
+ default:
+ printf("%d: %c is not a vowel.\n", i, letter);
+ }
+ }
+
+ return 0;
+}
View
36 ex14.c
@@ -0,0 +1,36 @@
+#include <stdio.h>
+#include <ctype.h>
+
+// forward declarations
+int can_print_it(char ch);
+void print_letters(char arg[]);
+
+void print_arguments(int argc, char *argv[])
+{
+ int i = 0;
+
+ for(i = 0; i < argc; i++) {
+ print_letters(argv[i]);
+ }
+
+}
+void print_letters(char arg[])
+{
+ int i = 0;
+
+ for(i = 0; arg[i] != '\0'; i++) {
+ char ch = arg[i];
+
+ if( isalpha(ch) || isblank(ch) ) {
+ printf("'%c' == %d ", ch, ch);
+ }
+ }
+
+ printf("\n");
+}
+
+int main(int argc, char *argv[])
+{
+ print_arguments( argc, argv);
+ return 0;
+}
View
62 ex15.c
@@ -0,0 +1,62 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ // create 2 arrays we care about
+ int ages[] = {23, 43, 12, 89, 2};
+ char *names[] = {
+ "Alan", "Frank",
+ "Mary", "John", "Lisa"
+ };
+
+ // safely get the size of the ages
+ int count = sizeof(ages) / sizeof(int);
+ int i = 0;
+
+ // first way using indexing
+ for(i=0; i < count; i++) {
+ printf("%s has %d years alive. \n",
+ names[i], ages[i]);
+ }
+
+ printf("---\n");
+
+ // setup the pointers to the start of the arrays
+ int *cur_age = ages;
+ char **cur_name = names;
+
+ // second way using pointers
+ for(i=0; i < count; i++) {
+ printf("%s is %d years old.\n",
+ *(cur_name+i), *(cur_age+i));
+ }
+
+ printf("---\n");
+
+ // third way, pointers are just arrays
+ for(i=0; i < count; i++) {
+ printf("%s is %d years old again.\n",
+ cur_name[i], cur_age[i]);
+ }
+
+ printf("---\n");
+
+ // fourth way with pointers in a stupid complex way
+ for(cur_name = names, cur_age = ages;
+ (cur_age - ages) < count;
+ cur_name++, cur_age++)
+ {
+ printf("%s lived %d years for far.\n",
+ *cur_name, *cur_age);
+ }
+
+ printf("---\n");
+
+ for(i=0; i < count; i++) {
+ printf("name: %p age: %p.\n",
+ &cur_name[i], &cur_age[i]);
+ }
+
+ return 0;
+}
+
View
70 ex16.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct Person {
+ char *name;
+ int age;
+ int height;
+ int weight;
+};
+
+struct Person *Person_create(char *name, int age, int height, int weight)
+{
+ struct Person *who = malloc(sizeof(struct Person));
+ assert(who != NULL);
+
+ who->name = strdup(name);
+ who->age = age;
+ who->weight = weight;
+ who->height = height;
+
+ return who;
+}
+
+void Person_destroy(struct Person *who)
+{
+ assert(who != NULL);
+
+ free(who->name);
+ free(who);
+}
+
+void Person_print(struct Person *who)
+{
+ printf("Name: %s\n", who->name);
+ printf("\tAge: %d\n", who->age);
+ printf("\tHeight: %d\n", who->height);
+ printf("\tWeight: %d\n", who->weight);
+}
+
+int main(int argc, char *argv[])
+{
+ // make two people structures
+ struct Person *joe = Person_create(
+ "Joe Alex", 32, 64, 140);
+
+ struct Person *frank = Person_create(
+ "Frank Blank", 20, 72, 180);
+
+ printf("Joe is at memory location %p:\n", joe);
+ Person_print(joe);
+
+ printf("Frank is at memory location %p:\n", frank);
+ Person_print(frank);
+
+ joe->age += 20;
+ joe->height -= 2;
+ joe->weight += 40;
+ Person_print(joe);
+
+ frank->age += 20;
+ frank->weight += 20;
+ Person_print(frank);
+
+ Person_destroy(joe);
+ Person_destroy(frank);
+
+ return 0;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.