CpSc 1111 Lab 9 2-D Arrays Overview This week, you will gain some experience with 2-dimensional arrays, using loops to do the following: initialize a 2-D array with data from an input file print out the values in an array compute averages for each row of the 2-D array compute averages for each column of the 2-D array sort all the values of the 2-D array Background Information In last week s lab, you wrote a program that computed the inner product of two 1-dimensional arrays. This week, you will be storing data from a file (grades of 7 labs for 6 students) into a 2-dimensional array. 2-dimensional arrays are often used for tables of data. They can be visualized as a matrix consisting of rows and columns. Declaration of a 2-D array is similar to a 1-D array except that an additional number is provided in brackets representing the size of the second dimension. The first size represents the number of rows and the second size represents the number of columns. You also used loops in last week s lab to iterate through arrays to perform various tasks. You will use loops again this week to initialize the array, print the values, traverse through the array to do calculations and store values into new 1- dimensional arrays, and to sort the values of the array. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Sorting Arrays There are many different sorting algorithms that may be used to sort values in an array. You may recall the Selection Sort as one of the sorts from the slides that was covered in lecture. It determines the location of the minimum of the list and swaps it with the element at the index where it is supposed to be. 1
Lab Assignment For this week s lab assignment, you will write a program, called lab9.c. Reminder About Formatting and Comments The top of your file should have a header comment, which should contain: o Your name o Course and semester o Lab number o Brief description about what the program does o Any other helpful information that you think would be good to have. Variables should be declared at the top of the main function, and should have meaningful names. Always indent your code in a readable way. Some formatting examples may be found here: https://people.cs.clemson.edu/~chochri/assignments/formatting_examples.pdf Don t forget to use the Wall flag when compiling, for example: gcc Wall lab9.c In your program, perform the following steps. (NOTE: use for loops for all the loops in your program.) 1. Once you log in to your account, navigate to a directory where you will do this week s lab work. Then type the following command: cp /group/course/cpsc111/public_html/s18labs/lab9/input.txt. Don t forget the dot at the end this says to copy file called input.txt from that specified path into your current working directory. 2. Declare a 2-D array called grades. It should be 6 rows X 7 columns. Create a for loop to read in the values using scanf(). When you run your program, you will redirect the input from the file called input.txt so you won t need to prompt the user to enter values. 3. Create a for loop to print the values of the grades array. (compile and run) Your output should look like the following: 80 90 70 100 60 90 85 78 93 80 70 98 98 85 100 99 89 90 72 0 78 98 100 65 67 11 28 89 85 90 98 85 87 56 69 22 85 72 95 75 64 88 To get the values to line up as shown above, format the integer output with a size of 4 in the format string of the printf() statement. 2
4. Each row of the table is a student. Add an additional printf() statement to the for loop from step 3 so that your output looks like the following: (compile and run) 5. Use another for loop to compute the average for each student. Put the averages for each student (each row) into a new 1-D array called stuavg. Then write another loop to print the averages from this 1-D array out. (compile and run) Your output now should look like the following: (NOTE: the averages are floating point values (or doubles) and are printed to 2 decimal places.) 82.14 86.00 75.43 65.43 81.43 71.57 6. Add an additional printf() statement to the for loop from step 5 so that your output for the averages indicates which student, like the following: (compile and run) 3
7. Each grade in each column can be thought of as a lab grade. Use another for loop to compute the average for each lab (or column). Put those averages into a new 1-D array called labavg. Then write another loop to print the averages from this 1-D array out. (compile and run) Adding the additional printf() for each lab average indicating which lab, your output now should look like the following: Lab 1 average: 77.17 Lab 2 average: 92.83 Lab 3 average: 79.00 Lab 4 average: 84.50 Lab 5 average: 67.17 Lab 6 average: 56.00 Lab 7 average: 82.33 8. Use the Selection Sort shown on page 1 of the lab write-up to sort the values in the grades array. One suggested way of doing this is to copy the values from the 2-D array to a 1-D array called sortedgrades, and then sort that 1-D array. Then print the values in that sorted array. (compile and run) Your output for the entire program should now look like the following: Lab 1 average: 77.17 Lab 2 average: 92.83 Lab 3 average: 79.00 Lab 4 average: 84.50 Lab 5 average: 67.17 Lab 6 average: 56.00 Lab 7 average: 82.33 0 11 22 28 56 60 64 65 67 69 70 70 72 72 75 78 78 80 80 85 85 85 85 85 87 88 89 89 90 90 90 90 93 95 98 98 98 98 99 100 100 100 4
Turn In Work 1. Before turning in your assignment, make sure you have followed all of the instructions stated in this assignment and any additional instructions given by your lab instructor(s). Always test, test, and retest that your program compiles and runs successfully on our Unix machines before submitting it. 2. Show your TA that you completed the assignment. Then submit your lab9.c program using the handin page: http://handin.cs.clemson.edu. Don t forget to always check on the handin page that your submission worked. You can go to your bucket to see what is there. Grading Rubric For this lab, points will be based on the following: Functionality 75 should have the following functionality in loops: loop(s) to initialize grades array loop(s) to print grades array loop(s) to compute student averages and put into stuavg array loop to print stuavg values loop(s) to compute lab averages and put into labavg array loop to print labavg values loop(s) to copy 2-D grades array into 1-D sortedgrades array loop(s) to sort sortedgrades array loop to print sortedgrades array Code formatting 10 No warnings when compile 5 including forgetting return statement at end of main Uses a 2-D array 5 Uses scanf() to initialize 2-D array 5 redirecting input from the input.txt file NOTE: There could be other possible point deductions for things not listed, such as global variables, use of break not in switch statements, naming the file incorrectly, etc. 5