Assignment Instructions

  1. Edit, compile, and run the following program on the UH UNIX shell:
    1. Use pointers to write a function WITH ONE INTEGER OR FLOATING POINT PARAMETER that is call-by-reference. In other words, the function changes the value of an integer variable that is declared and initialized in the main() function. Display the value of the variable four (4) times - after initializing the variable in main(), twice inside the function call, and after the function call in main(). What the value is changed to is up to you.
      ** NOTE: Write your own, unique call-by-reference function. Do NOT simply copy and paste my swap() function. Do NOT write a function that swaps the values of the variables. **
      In the function definition, for example, you could add, subtract, multiply, or divide the parameter and a number and reassign the result to the parameter. Or just reassign the parameter to a different number. Just don't do a swap.
    2. Declare and initialize an array of 5 integers. Make up any values that you wish.
    3. Declare and intialize an integer pointer with the address of the array.
    4. Display 5th element in the array in four different ways: using array subscript notation with the array name, using pointer/offset notation using array name, using array subscript notation with the pointer, and using pointer/offset notation with the pointer.
    5. Display the entire array in four different ways: using array subscript notation with the array name, using pointer/offset notation using array name, using array subscript notation with the pointer, and using pointer/offset notation with the pointer.
    6. Display the HEXADECIMAL VALUE (using %p) of the addresses of each element of the array.
    7. Email your makefile and C program to the instructor, or show it to the instructor in class.
  2. For the submission guidelines and the grading guidelines, see the syllabus.

Example I/O

The output is up to you, but it should display the following:
the value of a variable before,
during,
and after a call-by-reference function call, which changes the value of the variable

the 5th element in the four different ways
the entire array in the four different ways
the addresses of each element of the array

Below is example output:

Here is the value of the variable BEFORE a call-by-reference function call:
number = 3.141593
Making a call-by-reference function call...
Here is the value of the parameter DURING a call-by-reference function call, BEFORE changing the value:
number = 3.141593
Making changes now to the parameter...
Here is the value of the parameter DURING a call-by-reference function call, AFTER changing the value:
number = 6.283185
Here is the value of the variable AFTER a call-by-reference function call:
number = 6.283185

Here is the 5th element, using array subscript notation with the array name:
array[4] = 505
Here is the 5th element, using pointer/offset notation using array name:
*(array + 4) = 505
Here is the 5th element, using array subscript notation with the pointer:
pointer[4] = 505
Here is the 5th element, using pointer/offset notation with the pointer:
*(pointer + 4) = 505

Here is the whole array, using array subscript notation with the array name:
101, 202, 303, 404, 505, 
Here is the whole array, using pointer/offset notation using array name:
101, 202, 303, 404, 505, 
Here is the whole array, using array subscript notation with the pointer:
101, 202, 303, 404, 505, 
Here is the whole array, using pointer/offset notation with the pointer:
101, 202, 303, 404, 505, 

Here is the HEXADECIMAL VALUE (using %p) of the addresses of each element of the array:
&array[0] = 0xffbffa30
&array[1] = 0xffbffa34
&array[2] = 0xffbffa38
&array[3] = 0xffbffa3c
&array[4] = 0xffbffa40