summaryrefslogtreecommitdiff
path: root/03_code2
diff options
context:
space:
mode:
authorHaidong Ji2022-04-15 15:51:30 -0500
committerHaidong Ji2022-04-15 15:51:30 -0500
commit442a49ad5a48d417345959b903ae6a6d32d55759 (patch)
treec7127bb497e5e439018b1915e0136eec2c9cb124 /03_code2
Great C programming funHEADmaster
Excellent fundamentals and displine training, many tools and techniques exercises: gdb, emacs, valgrind, git
Diffstat (limited to '03_code2')
-rw-r--r--03_code2/README34
-rw-r--r--03_code2/code2.c51
-rw-r--r--03_code2/grade.txt23
-rwxr-xr-x03_code2/test.sh46
4 files changed, 154 insertions, 0 deletions
diff --git a/03_code2/README b/03_code2/README
new file mode 100644
index 0000000..704aefc
--- /dev/null
+++ b/03_code2/README
@@ -0,0 +1,34 @@
+Open the code2.c file you have in this directory.
+You will see two functions: printTriangle and main.
+
+As with the previous problem, the printTriangle function
+has an algorithm written as comments, but there is no code.
+You should translate this algorithm to code.
+
+In main, there are three print statements which print out a triangle
+with height 4, and the total number of stars in that triangle (returned
+by the printTriangle function).
+
+There is also a comment asking you to write a few more statements
+to do the same thing for a triangle of height 7.
+Add the code to do this.
+
+When you have done this, run
+
+./test.sh
+
+As before, this script will try to compile your code (which you will learn about
+ina n upcoming lesson), 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 Course 1 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/03_code2/code2.c b/03_code2/code2.c
new file mode 100644
index 0000000..25af8a0
--- /dev/null
+++ b/03_code2/code2.c
@@ -0,0 +1,51 @@
+
+int printTriangle(int size) {
+ //start with starCount being 0
+ int starCount = 0;
+
+ //count from 0 (inclusive) to size (exclusive), for each number i that you count
+ for (int i = 0; i < size; i++) {
+
+ //count from 0 (inclusive) to i (inclusive), for each number j that you count
+ for (int j = 0; j <= i; j++) {
+
+ //print a "*"
+ printf("*");
+
+ //increment starCount
+ starCount++;
+ }
+
+ //when you finish counting on j,
+
+ //print a newline ("\n")
+ printf("\n");
+ }
+
+ //when you finish counting on i,
+
+ //your answer is starCount
+ return starCount;
+
+}
+
+
+int main(void) {
+ int numStars;
+ printf("Here is a triangle with height 4\n");
+ numStars = printTriangle(4);
+ printf("That triangle had %d total stars\n", numStars);
+ //now print "Here is a triangle with height 7\n"
+ printf("Here is a triangle with height 7\n");
+
+ //then call printTriangle, passing in 7, and assign the result to numStars
+ numStars = printTriangle(7);
+
+ //finally, print "That triangle had %d total stars\n", such that the %d
+ //prints the value of numStars
+ printf("That triangle had %d total stars\n", numStars);
+
+
+ return 0;
+}
+
diff --git a/03_code2/grade.txt b/03_code2/grade.txt
new file mode 100644
index 0000000..0138468
--- /dev/null
+++ b/03_code2/grade.txt
@@ -0,0 +1,23 @@
+Grading at Sat 27 Nov 2021 02:21:26 AM UTC
+Checking code2.c for legal syntax
+Checking for int printTriangle (int size)
+Found on line 4, column 1
+Checking for int main(void)
+Found on line 35, 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 printTriangle(0) ... Correct
+Testing printTriangle(1) ... Correct
+Testing printTriangle(2) ... Correct
+Testing printTriangle(3) ... Correct
+Testing printTriangle(4) ... Correct
+Testing printTriangle(7) ... Correct
+Testing printTriangle(9) ... Correct
+Testing printTriangle(12) ... Correct
+Testing printTriangle(142) ... Correct
+Testing printTriangle(191) ... Correct
+Testing printTriangle(2037) ... Correct
+Testing printTriangle(2479) ... Correct
+
+Overall Grade: A
diff --git a/03_code2/test.sh b/03_code2/test.sh
new file mode 100755
index 0000000..649b8a6
--- /dev/null
+++ b/03_code2/test.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+cat > temp.c <<EOF
+#include <stdio.h>
+#include <stdlib.h>
+EOF
+cat code2.c >> temp.c
+gcc -Wall -Werror -pedantic -std=gnu99 temp.c -o code2 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 "-----------------"
+ ./code2
+ echo "-----------------"
+ echo "Here is what I would expect the answers to be:"
+ echo "----------------"
+ cat <<EOF
+Here is a triangle with height 4
+*
+**
+***
+****
+That triangle had 10 total stars
+Here is a triangle with height 7
+*
+**
+***
+****
+*****
+******
+*******
+That triangle had 28 total stars
+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 errors.txt temp.c code2
+