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 --- 02_code1/README | 33 ++++++++++++++++++++++++ 02_code1/code1.c | 20 +++++++++++++++ 02_code1/grade.txt | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 02_code1/test.sh | 34 +++++++++++++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 02_code1/README create mode 100644 02_code1/code1.c create mode 100644 02_code1/grade.txt create mode 100755 02_code1/test.sh (limited to '02_code1') diff --git a/02_code1/README b/02_code1/README new file mode 100644 index 0000000..49bd449 --- /dev/null +++ b/02_code1/README @@ -0,0 +1,33 @@ +Open the code1.c file you have in this directory. +You will see two functions: max and main. + +In max, the algorithm is written as comments, but there is no code. +You should translate this algorithm to code. + +In main, there are three print statements which call max and print +its result. These would let you check if max is working right. + +There is also a comment asking you to write one more print statement +with a call to max. In that comment, it asks you to take the max +of two numbers (written as hex), and print them out. +Add a line of code to do this. + +When you have done this, run + +./test.sh + +This script will try to compile your code (which you will learn about in +the next chapter), as well as run it. If your code does not have legal +syntax, it will do its best to describe what is wrong. If there +is a problem, look at the code and try to see where you did not follow +the syntax rules from chapter 2. The line number and message it gives +may help you find the problem. If you cannot find the problem after a minute +or two, ask for help. + +If your code has legal syntax, test.sh will also run it, and show +you the output it produced, as well as the output we expected. + +If they are the same, you should commit, push, and grade. + +If they are not, you should see if you can fix the problem (and ask for help +if you cannot). diff --git a/02_code1/code1.c b/02_code1/code1.c new file mode 100644 index 0000000..192da3f --- /dev/null +++ b/02_code1/code1.c @@ -0,0 +1,20 @@ +int max (int num1, int num2) { + //check if num1 is greater than num2 + //if so, your answer is num1 + //otherwise, your answer is num2 + if (num1 > num2) { + return num1; + } + return num2; +} + +int main(void) { + printf("max(42, -69) is %d\n", max(42, -69)); + printf("max(33, 0) is %d\n", max(33, 0)); + printf("max(0x123456, 123456) is %d\n", max(0x123456, 123456)); + //compute the max of 0x451215AF and 0x913591AF and print it out as a decimal number + printf("max(0x451215AF, 0x913591AF) is %d\n", max(0x451215AF, 0x913591AF)); + + return 0; +} + diff --git a/02_code1/grade.txt b/02_code1/grade.txt new file mode 100644 index 0000000..172fbee --- /dev/null +++ b/02_code1/grade.txt @@ -0,0 +1,75 @@ +Grading at Sat 27 Nov 2021 02:14:17 AM UTC +Checking code1.c for legal syntax +Checking for int max (int num1, int num2) +Found on line 3, column 1 +Checking for int main(void) +Found on line 13, column 1 +Trying to run the code.. +Your file matched the expected output +Removing your main() and replacing it with our own to run more tests... +Testing max(-999, -2147483648) ... Correct +Testing max(-999, 123) ... Correct +Testing max(-999, 567) ... Correct +Testing max(-999, 891) ... Correct +Testing max(-999, 0) ... Correct +Testing max(-999, 1) ... Correct +Testing max(-999, -999) ... Correct +Testing max(-999, 123123123) ... Correct +Testing max(-87, -2147483648) ... Correct +Testing max(-87, 123) ... Correct +Testing max(-87, 567) ... Correct +Testing max(-87, 891) ... Correct +Testing max(-87, 0) ... Correct +Testing max(-87, 1) ... Correct +Testing max(-87, -999) ... Correct +Testing max(-87, 123123123) ... Correct +Testing max(0, -2147483648) ... Correct +Testing max(0, 123) ... Correct +Testing max(0, 567) ... Correct +Testing max(0, 891) ... Correct +Testing max(0, 0) ... Correct +Testing max(0, 1) ... Correct +Testing max(0, -999) ... Correct +Testing max(0, 123123123) ... Correct +Testing max(1, -2147483648) ... Correct +Testing max(1, 123) ... Correct +Testing max(1, 567) ... Correct +Testing max(1, 891) ... Correct +Testing max(1, 0) ... Correct +Testing max(1, 1) ... Correct +Testing max(1, -999) ... Correct +Testing max(1, 123123123) ... Correct +Testing max(240, -2147483648) ... Correct +Testing max(240, 123) ... Correct +Testing max(240, 567) ... Correct +Testing max(240, 891) ... Correct +Testing max(240, 0) ... Correct +Testing max(240, 1) ... Correct +Testing max(240, -999) ... Correct +Testing max(240, 123123123) ... Correct +Testing max(345, -2147483648) ... Correct +Testing max(345, 123) ... Correct +Testing max(345, 567) ... Correct +Testing max(345, 891) ... Correct +Testing max(345, 0) ... Correct +Testing max(345, 1) ... Correct +Testing max(345, -999) ... Correct +Testing max(345, 123123123) ... Correct +Testing max(999999, -2147483648) ... Correct +Testing max(999999, 123) ... Correct +Testing max(999999, 567) ... Correct +Testing max(999999, 891) ... Correct +Testing max(999999, 0) ... Correct +Testing max(999999, 1) ... Correct +Testing max(999999, -999) ... Correct +Testing max(999999, 123123123) ... Correct +Testing max(2147483647, -2147483648) ... Correct +Testing max(2147483647, 123) ... Correct +Testing max(2147483647, 567) ... Correct +Testing max(2147483647, 891) ... Correct +Testing max(2147483647, 0) ... Correct +Testing max(2147483647, 1) ... Correct +Testing max(2147483647, -999) ... Correct +Testing max(2147483647, 123123123) ... Correct + +Overall Grade: A diff --git a/02_code1/test.sh b/02_code1/test.sh new file mode 100755 index 0000000..eb21e2b --- /dev/null +++ b/02_code1/test.sh @@ -0,0 +1,34 @@ +#!/bin/bash +cat > temp.c < +#include +EOF +cat code1.c >> temp.c +gcc -Wall -Werror -pedantic -std=gnu99 temp.c -o code1 2>errors.txt +if [ "$?" = "0" ] + then + echo "Your code appears to have legal syntax!" + echo "Here is what I get when I run it..." + echo "-----------------" + ./code1 + echo "-----------------" + echo "Here is what I would expect the answers to be:" + echo "----------------" + cat <