CSC 180 Notes for Tutorial #3 Monday 30 Sept Wednesday 2 October ============ Notes for your tutorial ========================================= SOME SMALL PROGRAMMING EXAMPLES ================================ - Some suggested problems to solve: I o Ask the user for their age, and make sure it's between 0 and 125. If not, keep asking until they enter a "reasonable" age. (The haven't seen compound conditions using && or ||, but feel free to tell them about these operators and use them in the program.) II o Given a list of 60 numeric marks, determine the number of A's, B's, C's, D's, and failures. (assume that the input marks are valid - but ask how they would change the program to do error checking) (They don't know about arrays yet, so you will need 5 counters.) III o Read in 30 marks on an assignment marked out of 100 (don't worry about error checking) and print out whether or not anyone got perfect. They know about Booleans, but haven't used them yet outside of conditions, so you'll have to introduce and explain that aspect. You may want to let them solve this without using booleans, and then improve the solution by switching over to booleans. Use this as an opportunity to explain what a "flag" is (many students use this as a variable name when it's not flagging anything). Also explain why "flag" is a poor name even when it does represent some sort of flag -- foundPerfect, eg, would be better. Here are sample solutions to the problems: =========================================== Problem I - when you derive the solution, don't start with constants for the min / max age, but see if you can get them to suggest the idea - try to get them to write the condition for a valid age. /* Program that asks the user for their age and makes sure it's between 0 and 125. If not, keep asking until they enter a "reasonable" age. */ #define MINIMUM_AGE 0 /* We use constants for the minimum and maximum */ #define MAXIMUM_AGE 125 /* age so that changes to the age range can be */ /* done easily. */ int main() { int usersAge; /* Prompt and read the users age. */ printf("Enter your age: "); scanf("%d", &usersAge); /* Check validity of age and repeatedly prompt and read an age */ /* until a valid age is entered. */ while ( (usersAge < MINIMUM_AGE) || (usersAge > MAXIMUM_AGE) ) { /* Scold the users for entering bad input! */ printf("The age: %d is not valid.\n", usersAge); printf("Your age should be between %d and %d\n", MINIMUM_AGE, MAXIMUM_AGE ); /* Prompt and read the users age. */ printf("Enter your age: "); scanf("%d", &usersAge); } printf("Your age is %d years.\n", usersAge); } Problem II - get them to derive the cascaded if, and also the idea of having more than one counter. You can get them to think about "arrays" of counters, but don't write the program that way. /* Program that reads a list of 60 numeric marks and determines the number of A's, B's, C's, D's, and failures. */ #define NUMBER_OF_MARKS 60 #define A 80 #define B 70 #define C 60 #define D 50 int main() { int mark; int numMarksProcessed = 0; int numAs = 0, numBs = 0, numCs = 0, numDs = 0, numFs = 0; while ( numMarksProcessed < NUMBER_OF_MARKS ) { /* read a mark and update the counters appropriately. */ scanf("%d", &mark); numMarksProcessed = numMarksProcessed + 1; /* They don't know about += or ++ yet. */ /* categorize the mark and increase the accumulator */ if ( mark >= A ) { numAs = numAs + 1; } else if ( mark >= B ) { numBs = numBs + 1; } else if ( mark >= C ) { numCs = numCs + 1; } else if ( mark >= D ) { numDs = numDs + 1; } else { numFs = numFs + 1; } } /* Print a report on the grade distribution. */ printf("The number of A's is : %d\n", numAs); printf("The number of B's is : %d\n", numBs); printf("The number of C's is : %d\n", numCs); printf("The number of D's is : %d\n", numDs); printf("The number of F's is : %d\n", numFs); } Problem III - try to get them to think about short circuiting the loop. /* Read in 30 marks on an assignment marked out of 100 (don't worry about error checking) and print out whether or not anyone got perfect. */ #define NUMBER_OF_MARKS 30 #define PERFECT_MARK 100 #define FALSE 0 /* It is a common practice to define constants */ #define TRUE 1 /* to represent the values for FALSE and TRUE. */ int main() { int mark; int numMarksProcessed = 0; int foundPerfect = FALSE; /* We initialize foundPerfect to FALSE since at the beginning of the program we have not yet found a perfect mark. */ while ( numMarksProcessed < NUMBER_OF_MARKS ) { /* read a mark and check for perfect */ scanf("%d", &mark); numMarksProcessed = numMarksProcessed + 1; /* They don't know about += or ++ yet. */ if ( mark == PERFECT_MARK ) { foundPerfect = TRUE; } /* You can add a "break" to the loop once a perfect mark is found. They don't know about break yet, but it will seem natural to do here, so feel free to tell them. Or you could add a AND !foundPerfect to the while condition. Feel free to talk about these choices. */ } /* Report on whether or not a perfect grade was given. */ if ( foundPerfect ){ printf( "Yes, someone did receive a perfect grade.\n"); } else { printf( "No, no one received a perfect grade.\n"); } /* Note we don't say foundPerfect == TRUE because foundPerfect's value will be either 0 (false) or 1 (true) */ }