CS-211 Lab 8 -- Arrays, Pointers, and Structures

We have learned about arrays, we have learned about structures, and now we are learning about pointers. This lab deals with all three.

I have created some C code for you to start with. The C code I have created has a main function that expects three arguments - the number of slices, the number of rows, and the number of columns in order to create a three-dimensional array. The program creates that array, and fills in the values so that each cell has a unique value (well... almost each cell.) Then the program loops 10 times. In each iteration of the loop, the program randomly generates three indexes - a slice index, a row index, and a column index, and puts all three into an "ind3" structure designed to hold all three indexes. Then, the program invokes a getElement function. The prototype for the getElement function is provided for you, but you will need to write the code for getElement. The getElement function should return the value in the three-dimensional matrix at the index specified. Then, after printing out the index and the value, the program invokes the findIndex function with that value. You will also need to code the findIndex function. It's job is to find the first element of the array whose value matches the specified value. The program then prints out your result.

When everything is coded correctly, here is an example of the results:

  >mat3d 4 3 6
  200 found at m[2][0][0]
  220 found at m[2][2][0]
  121 found at m[1][2][1]
  203 found at m[2][0][3]
  4 found at m[0][0][4]
  12 found at m[0][1][2]
  212 found at m[2][1][2]
  121 found at m[1][2][1]
  320 found at m[3][2][0]
  21 found at m[0][2][1]

Getting Started

Make a lab08 directory, download the mat3d.c file and move it into your lab08 directory. You will need to add two functions at the end of this file, getElement and findIndex. Find the prototypes for these functions at the top of the file. You may not change these prototypes!

When you are coding the getElement function, you may not use array notation. (It is possible to write this function using array notation, but it's complicated because you are given a pointer to an integer - not a matrix - as an argument. I think it's much easier to write this function using pointer notation)

The most important thing to remember when writing the code for this function is how arrays are layed out in memory. See Lecture 10, slides 13 and 15 if you can't remember how arrays are layed out.

When you are coding the findIndex function, resist the temptation to divide the value into its hundreds digit, its tens digit, and its ones digit, and use those for slice, row, and column respectively. That works for easy cases, but not for some of the more sophisticated cases. In fact, the easiest way to code this function is to invoke the getElement function repeatedly until you find an index where the result of getElement matches the val parameter. Note that you are not given the number of slices in your parameter list. This function should assume there are an infinite number of slices, and keep looking at slices until it finds the value.

Testing your Program

Once your mat3d.c code is written, compile it and make sure there are no error or warning messages. Once the compile is complete, test your code. Make sure you try some difficult cases, like cases where the number of rows, columns, or slices is zero, or cases where the number of rows, columns, or slices is greater than 10.

Extra Credit

There is some code at the end of the main function that is commented out, mostly because there is a (fairly small) chance that this code will cause a segmentation violation. If you uncomment this code and run it, does you code work? Can you make it work? Uncomment this code for 2 points worth of extra credit to offset any deductions you might have gotten on this lab.

Go to MyCourses, and open the CS-211 page, and choose the "Content" hyperlink on the left list of options. You will see a "Lab 08 Submission" icon to upload your mat3d.c file. Make this submission before the due date of Friday, November 3, at 11:59 PM. You may submit as many times as you want before the due date, but only the latest submission will be graded.

Lab Grading

This lab is worth 10 points. You will get deductions as follows: