Section 5 - Sample Programming Labs
Now that we understand a lot more about how to write C++ programs, some improved assignment examples can be given. Here are a couple model homework submissions to remove any remaining confusion about how the homework should look.
2B.5.1 Typical Lab Assignment Submission #1
FAKE HOMEWORK #1:
Write a program to determine and print out the ASCII codes for the letters 'L' and 'l', 'F' and 'f'. What can you deduce about the uppercase and lowercase ASCII codes?
Here is how you would solve it, and this is what your homework submission should look like:
/* * Source program for Fake Homework #1 for CS 2A * Written by Michael Samuel, 11/15/12 * */ #include <iostream> using namespace std; int main() { char upperLet, lowerLet; int upperVal, lowerVal; // first do the L/l values. place in both char and int and show: upperLet = 'L'; lowerLet = 'l'; upperVal = upperLet; lowerVal = lowerLet; cout << upperLet << " has ASCII value: " << upperVal << endl; cout << lowerLet << " has ASCII value: " << lowerVal << endl << endl; // next do the F/f values. place in both char and int and show: upperLet = 'F'; lowerLet = 'f'; upperVal = upperLet; lowerVal = lowerLet; cout << upperLet << " has ASCII value: " << upperVal << endl; cout << lowerLet << " has ASCII value: " << lowerVal << endl << endl; return (0); } /* ---------------- output pasted below ---------------- L has ASCII value: 76 l has ASCII value: 108 F has ASCII value: 70 f has ASCII value: 102 Press any key to continue . . . ---------------------- end of output ----------------- */ The conclusion I draw from this is that the lower case letters have ASCII codes which are exactly 32 larger than their upper case counterparts: i.e., 'a' = 'A' + 32 and 'z' = 'Z' + 32.
2B.5.2 Typical Lab Assignment Submission #2
FAKE HOMEWORK #2:
Compute the following three expressions for x = -3 and y = 10:

Here is how you would solve it, and this is what your homework submission should look like:
/* * Source program for Fake Homework #2 for CS 2A * Written by Nina Meyers, 1/1/2012 * */ #include <iostream> using namespace std; int main() { double x, y, answer; // set up input values for variables x and y: x = -3; y = 10; // first expression: answer = x * x * x + y * y; cout << x << "^3 + " << y << "^2 = " << answer << endl << endl; // second expression answer = (x - y) / (x + y); cout << "(" << x << "-" << y << ") / (" << x << "+" << y << ") = " << answer << endl << endl; // third expression answer = 2 + 4 + 6 + 8 + 10; cout << "2 + 4 + ... + " << y << " = " << answer << endl << endl; return 0; } /* ------------------- Sample Run -------------------- -3^3 + 10^2 = 73 (-3-10) / (-3+10) = -1.85714 2 + 4 + ... + 10 = 30 Press any key to continue . . . ---------------------- End Sample Run ---------------- */
Prevent Point Loss
- Do not create lots of variables when you can re-use the same variables. This is particularly true if the variables all represent the same kinds of things. For example, it would usually be better to have one variable, age, rather than age1, age2 and age3. However, don't overdo this principle - see next bullet. (1-2 point penalty)
- Do use distinct variables if they represent different sorts of things. For example, don't use the same variable for input as you use for output. (1 - 2 point penalty)
- Describe output in screen output statements. Undocumented, or under-documented values in your screen output are unacceptable. This documentation should be generated by your program, not added by hand to your output. (1 - 2 point penalty)
- Do not do computations in a screen output statement. In general, don't mix computation and input or output. (2 - 3 point penalty)