.. _lab2: Lab 2 - Simple C++ Functions ############################ For this lab, you will need to clone another repository using the invitation link below: * `COSC1337-002 `_ Writing a bigger program ************************ Now that we have seen how to put a simple program together that uses C++ functions, let's build a program that does the most basic job all students have to contend with, process the grades on a test and produce a summary of the results. The big wrinkle in this is that I want this to be a multi-file program. We want to do three simple things: * Figure out the best score of all scores produced * Figure out the worst score of all scores produced * Assign a letter grade for each score produced Here is our list of grades to process: .. code-block:: c int grades[15] = { 55, 87, 93, 77, 92, 88, 67, 81, 84, 73, 81, 92, 89, 100, 62 }; This is formally an ``array`` of numbers. We will look at arrays later, but they work much like lists in Python. The big difference here, is that when we ask C++ to hand an array of data to a function, we actually just hand the address of the first element of the array to the function. We need to tell the function how many elements are in the array and then use that number to loop over the array to make sure we use the array correctly. .. warning:: Messing this up is a huge source of bugs in programs! Here is an example to make sure we do things right: .. code-block:: c void show_grades(int data[], int num_values); int main(int argc, char * argv[]) { int grades[15] = { 55, 87, 93, 77, 92, 88, 67, 81, 84, 73, 81, 92, 89, 100, 62 }; show_grades(grades, 15); } Place this code in a file named ``grader.cpp`` Here is the function that will display the grades: .. code-block:: c #include void show_grades(int data[], int num_values) { for(int i=0 i g++ -c grader.cpp -o grader.o > g++ -c utilities.cpp -o utilities.o > g++ grader.o utilities.o -o grader.exe That "-c" tell the compiler not to try to build an application, just "compileonly". The last line runs the linker which puts together the real application. (On a Mac/Linux ssystem, leave off the ".exe" extension) Remember that you can edit the command lines you have previously entered. Just use the up arrow key to work through the set of compile steps. Then link the final code. Your lab project **************** You need to complete this lab by writing three functions in the utilities file. Modify the header file and list the prototypes for each function. You will call each function with the `grades` array as a parameter, and the number of values in that array in your main routine and make it do the following work: * Use the max value function to print out the max value found in the grade array. * Use the min value function to print out the min value found in the grade array * Use the letter grade function to print out the grade number and the associated letter grade for each score in the array. This will require a nested if-then else statement to figure this out. You get to pick names for these three funcitons, so pick something that names sense. Do not worry about making this output extremely pretty. All we are doing here is trying to get our first fairly involved C++ program put together. Place all of this code in a folder named ``LAB2`` in the new labs-username rrpository .. vim: set filetype=rst spell