Skip to content

Commit

Permalink
Exercise 12
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlz committed May 25, 2012
1 parent 45e9ca1 commit b8be61b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
CFLAGS=-Wall -g
CC=gcc
NAME=ex11
NAME=ex12
RUNCMD=./out/$(NAME)

all: clean compile
Expand Down
12 changes: 9 additions & 3 deletions ex10.c
Expand Up @@ -22,18 +22,24 @@ int main(int argc, char *argv[])
int num_states = 4;
puts("Printing our own array of strings:");
for(i = 0; i < num_states; i++) {
printf("state %d: %s\n",i, states[i]);
char * state = states[i];
if('W' == *state) {
printf("Detected Washington. Exiting early (ex12 Extra Credit #3)\n");
break;
} else {
printf("state %d: %s\n",i, state);
}
}

////////////////////////////////////////////////////////////////////
// EXTRA CREDIT
printf("** EXTRA CREDIT\n");

// 1) Figure out what kind of code you can put in the parts of a for-loop

// 2) Look up how to use the comma character to separate multiple
// statements in parts of the for-loop.
// http://stackoverflow.com/q/276512/7507
puts("multiple initializers");
puts("Testing for statement with multiple initializers");
int j = 0;
for(i = 0, j = 100; i < num_states; i++, j += 5) {
printf("state %d: (%d) %s\n", i, j, states[i]);
Expand Down
7 changes: 7 additions & 0 deletions ex11.c
Expand Up @@ -6,6 +6,13 @@ int main(int argc, char *argv[])

int i = 0;
while(i < argc) {
char *arg = argv[i];
// Ex12 Extra Credit #3
if(*arg == 'Z') {
printf("Detected argument starting with 'Z'! ");
printf("Not printing any more command line arguments...\n");
break;
}
printf("arg %d: %s\n", i, argv[i]);
i++;
}
Expand Down
61 changes: 61 additions & 0 deletions ex12.c
@@ -0,0 +1,61 @@
#include <stdio.h>

int main(int argc, char *argv[])
{
int i = 0;

if(argc == 1) {
// As per Extra Credit item #4
printf("You didn't enter any command-line arguments. You suck.\n");

} else if(argc > 1 && argc < 4) {
printf("Here are your arguments:\n");

for(i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
} else if(argc > 4 && argc < 10) {
// Extra credit #2.
printf("That's a lot of arguments. I'll print them anyway.\n");
while(i < argc) {
printf("%s ", argv[i]);
i++;
}
printf("\n");
} else {
puts("You have too many arguments. You suck.\n");
}

// Extra credit #2
if(argc > 1) {
if(*argv[1] == 'Z') {
printf("You entered the secret character. Congratuations!\n");
}
}

//////////////////////////////////////////////////////////////////////////
printf("** EXTRA CREDIT\n");

// 1) Research the "boolean operators"
// Technically, they are &&, || and !, but since since C uses
// numeric values for conditional tests, technically any operator
// that returns a numeric value can be used in this manner.
// I.e. &, | and ^.
if(1 & 0) { printf("1 & 0 = \"true\"\n"); } else { printf("1 & 0 = \"false\"\n"); }
if(1 | 0) { printf("1 | 0 = \"true\"\n"); } else { printf("1 | 0 = \"false\"\n"); }
if(1 ^ 1) { printf("1 ^ 1 = \"true\"\n"); } else { printf("1 ^ 1 = \"false\"\n"); }
// Let's test if these operators are "short circuit" like in Java...
if(1 > 0 && printf("If you see this, then C does not \"short circuit\" conditionals.\n")) {
printf("short circuit test: true\n");
} else {
printf("short circuit test: false\n");
}

// 2) Add some more test cases (see above).
// 3) Go back to ex10 and ex11. Add if-statements to make loops exit early
// using break.
// 4) Fix the first test so the message accurately explains to the user
// that they didn't enter any arguments.
return 0;
}

0 comments on commit b8be61b

Please sign in to comment.