Code that generates errors/warnings is not trustworth: no warning is too small to ignore.
(Just as a heads-up, professors for many of the later csci courses will give an automatic fail to any program that generates a single warning message when it compiles, no matter how "good" the code is otherwise.)
One part of the grading of your labs will often involve an automated marking tool, that provides values to your programs in the order specified by the lab - if your program expects the data in a different order then it will fail the automated testing and lose marks accordingly.
"Please enter the circle radius as a real number, e.g. 5.25"
"Enter value"
"Out of range value entered: 32 (should be an integer in range 1-19)"
"Bad value"
"For the circle of radius 5, the computed circumference was 31.4"
"Result 31.4"
circleRadius
is immediately clearer
and more meaningful to the reader than R
or cR
.
// Dave Wessels // labex1 // // This program reads three polynomial coefficients // from the user and displays an equation in the // form aX^2 + bX + c
// variables to hold the three polynomial coefficients float a, b, c;
// prompt the user to enter the polynomial coeffients and // read them in, assume they are seperated by whitespace printf("Please enter the degree 2 coefficient,\n"); printf("followed by the degree 1, then the degree 0:\n"); scanf("%g\n", &a); scanf("%g\n", &b); scanf("%g\n", &c);
// Dave Wessels // A short program to compute the circumference of a circle when given its diameter #include <cstdio> // chosen accuracy level for Pi const double Pi = 3.1415; // calculate and return circumference based on radius of circle double calcCircumf(double rad); int main() { // obtain radius from user double radius; printf("Please enter the radius of a circle, e.g. 34.5\n"); int valsRead = scanf("%lg", &radius); // if valid radius compute and display circumference, // otherwise display appropriate error message if ((valsRead > 0) && (radius >= 0)) { double circ = calcCircumf(radius); printf("For a circle of radius %lg, the circumference is %lg\n", radius, circ); } else { // determine the specific nature of the problem and inform the user if (valsRead == 0) { printf("Sorry, but that was not a number\n"); } else { printf("Sorry, negative values (%lg) cannot be used\n", radius); } } return 0; } // calculate and return circumference based on radius of circle double calcCircumf(double rad) { // using Pi*diameter return Pi*(2*rad); } |
For example, userInitial would be a much more meaningful identifier than i, and fuelLevel would be a much more meaningful identifier than data.
For example, instead of the hard-coded value 3.1415 in the statement:
circumf = 3.1415 * radius;
we would introduce a named constant, e.g.
const float Pi = 3.1415; ....... circumf = Pi * radius;Similarly, if we have meanings associated with specific characters in the program they should be represented with constants.
For example, if we have the user enter Q to quit or C to continue, we might use:
const char QuitCmd = 'Q'; const char ContCmd = 'C'; ... printf("Enter %c to quit or %c to continue\n", QuitCmd, ContCmd);
void doSomething(int arr[], int size) { int someLocalArr[size]; // size is NOT a constant ...(This won't apply to arrays that are dynamically-allocated using the "new", "malloc", or "calloc" operators, as discussed at the very bottom of this page.)
For example, rather than having a function that is 100 lines long, break it into 3-5 smaller functions and call each in turn (passing data between them through the use of parameters).
The full implementation of the function should be accompanied by a more detailed set of comments.
void myfunction(int someparameter) { ..... all this is indented 3 spaces ..... }
if (condition) { // code and comments within the block // is indented 3 additional spaces } else if (condition) { // ... } else { // ... }
do { // ... // ... } while (condition);
while (condition) { // ... // ... }
for (initialization; condition; increment) { // ... // ... }
// comment describing struct purpose struct StructName { fieldType fieldName; // comment // ... etc ... };