summaryrefslogtreecommitdiff
path: root/02_code1
diff options
context:
space:
mode:
authorHaidong Ji2022-04-15 15:51:30 -0500
committerHaidong Ji2022-04-15 15:51:30 -0500
commit442a49ad5a48d417345959b903ae6a6d32d55759 (patch)
treec7127bb497e5e439018b1915e0136eec2c9cb124 /02_code1
Great C programming funHEADmaster
Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git
Diffstat (limited to '02_code1')
-rw-r--r--02_code1/README33
-rw-r--r--02_code1/code1.c20
-rw-r--r--02_code1/grade.txt75
-rwxr-xr-x02_code1/test.sh34
4 files changed, 162 insertions, 0 deletions
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 <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+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 <<EOF
+max(42,-69) is 42
+max(33,0) is 33
+max(0x123456,123456) is 1193046
+max(0x451215AF, 0x913591AF) is 1158813103
+EOF
+ echo "---------------"
+ else
+ echo "Oh no, the syntax of your code is not correct!"
+ mesg=`grep error errors.txt | head -1`
+ ln=`echo "$mesg" | cut -f2 -d":"`
+ let ln=${ln}-2
+ echo "I discovered the problem on line $ln "
+ echo "(though the problem may be earlier and I just got confused)"
+ echo "Here is my best attempt to describe what is wrong:"
+ echo "$mesg" | cut -f5 -d":"
+ fi
+rm -f temp.c errors.txt code1