CS12, Lab 4

Lab Cycle of April 26--30

 

 

Topics:          

·       Boolean variables

·       Pointers and dereferencing

·       Pointer arithmetic and accessing arrays through pointers

·       Pointer parameters to functions

 

·       Pointers to functions.

 

 

In this lab you will be practicing different uses of pointers.

You will be using pointers to access array elements, using pointers to pass parameters into functions, and using pointers to functions to call different functions using the same call.  

 

To do: 

·       Copy the file array.cpp, which contains a program that searches through an array of integers and doubles the value in each cell.

·        Save the program in a new file, p_array.cpp. 

·        Rewrite the code using a pointer and pointer arithmetic instead of subscripting. You need to include the dice class in this project to see it run.  

 

·       Copy the file param.cpp, which contains a division function with four parameters: two numbers passed in as value parameters, and their quotient and remainder passed as reference parameters. The function returns "true" or "false" depending on whether the division was successfull.

·        Save the program in a new file, p_param.cpp. 

·        Rewrite the code using pointer variables and pointer parameters instead of reference parameters.  

 

Extra credit: 

·       Write a program that has two functions, each with two int variables. One function, min() returns the smaller of the two. The other function, max() returns the larger of the two.

·       Write another function, void extreme(), that has four parameters: two input arrays with the same number of cells, the number of cells in the arrays, and a pointer to one of the two functions above. The function should print out, for each cell i, the smaller or larger of cells i in the two input arrays (depending on which function was called). For example, if the two input arrays are [1,2,5,3] and [0,8,7,1], the size is 4, and the input function is min(), extreme will print out 0 2 5 1. For the same input arrays but input function being max(), the output will be 1 8 7 3.

·       Now write the main(), which fills two arrays of ten elements (user input or hardwired), then calls the function extreme() twice, once with min() as a parameter and once with max() as a parameter.