In this lab exercise, you will write a program to demonstrate two concepts:

  • Using pointers as function parameters for arrays.
  • Dynamic memory allocation.

Programming Exercise

Write a program that contains a main function and three additional functions: populateIntegerArray, displayIntegerArray, and findMaximumInteger.

The main function must perform the following:

  • Ask the user to input a desired size for an array. (That is, ask the user to specify how many data values the array should hold.)
  • Dynamically allocate an array of int variables, with the size that the user requested.
  • Call a function named populateIntegerArray, which will prompt the user to input an integer value for each element of the array.
  • Call a function named displayIntegerArray, which will display on the screen:
    • the hexadecimal address of each array element,
    • the contents of the array element in decimal.
    • the contents of the array element in hexadecimal (this is an optional feature).
  • Call a function named findMaximumInteger, which will find and return the largest value in the array.
  • Display the value that was returned by the findMaximumInteger function.
  • De-allocate the array.

Function Prototypes

The functions indicated above must have the following function prototypes:

void populateIntegerArray(int *arrayPtr, int arraySize);
void displayIntegerArray(int *arrayPtr, int arraySize);
int findMaximumInteger(int *arrayPtr, int arraySize);

Function: populateIntegerArray

This function has two input parameters:

arrayPtr = address of beginning of array
arraySize = number of elements in the array.

The populateIntegerArray function must contain a loop that prompts the user to enter a value each element of the array.

Function: displayIntegerArray

This function has two input parameters:

arrayPtr = address of beginning of array
arraySize = number of elements in the array.

The displayIntegerArray function must contain a loop that displays the array contents, formatted as follows:

  • Display the hexadecimal address of each array element,
  • Display the contents of the array element in decimal.
  • Display the contents of the array element in hexadecimal (this is an optional feature).

(Observe the Sample Output section of this document as an example of the formatting.)

Function: findMaximumValue

This function has two input parameters:

arrayPtr = address of beginning of array
arraySize = number of elements in the array.

The findMaximumValue function must contain a loop that scans the array to identify the maximum value in the array.

The function returns that maximum value to the caller.

Displaying an integer as Hexadecimal

There are several ways to display an integer in hexadecimal format. Do some internet research to find an easy way to do this.

See if you can get your display to match the sample output on the next page exactly (except for the pointer value).

Sample Output

The following are some examples of correct program output, with the extra credit enhancement. (The exact value of your pointer will probably be different.)

(Note: in these examples, we have indicated which text the user types by showing it in a larger, bold font. In actuality, all text would appear in the same size font, with no bold characters.)

Example 1: array size = 5

Enter desired array size: 5
arrayPtr = 00000210D6EA1230
Enter value for array element 0: 12
Enter value for array element 1: -12
Enter value for array element 2: 65536
Enter value for array element 3: -65536
Enter value for array element 4: 4
00000210D6EA1230: arrayPtr[0] = 12 (Hex 0000000C)
00000210D6EA1234: arrayPtr[1] = -12 (Hex FFFFFFF4)
00000210D6EA1238: arrayPtr[2] = 65536 (Hex 00010000)
00000210D6EA123C: arrayPtr[3] = -65536 (Hex FFFF0000)
00000210D6EA1240: arrayPtr[4] = 4 (Hex 00000004)
Maximum integer in array is: 65536
DELETING array at arrayPtr = 00000210D6EA1230
Exit the program.

Example 2: array size = 20

Enter desired array size: 20
arrayPtr = 000001BA71BC95B0
Enter value for array element 0: 1024
Enter value for array element 1: -1024
Enter value for array element 2: 1
Enter value for array element 3: -1
Enter value for array element 4: 2
Enter value for array element 5: -2
Enter value for array element 6: 7
Enter value for array element 7: -7
Enter value for array element 8: 4096
Enter value for array element 9: -4096
Enter value for array element 10: 100
Enter value for array element 11: -100
Enter value for array element 12: 2000000000
Enter value for array element 13: -2000000000
Enter value for array element 14: 10
Enter value for array element 15: -10
Enter value for array element 16: 5
Enter value for array element 17: -5
Enter value for array element 18: 3
Enter value for array element 19: -3
000001BA71BC95B0: arrayPtr[0] = 1024 (Hex 00000400)
000001BA71BC95B4: arrayPtr[1] = -1024 (Hex FFFFFC00)
000001BA71BC95B8: arrayPtr[2] = 1 (Hex 00000001)
000001BA71BC95BC: arrayPtr[3] = -1 (Hex FFFFFFFF)
000001BA71BC95C0: arrayPtr[4] = 2 (Hex 00000002)
000001BA71BC95C4: arrayPtr[5] = -2 (Hex FFFFFFFE)
000001BA71BC95C8: arrayPtr[6] = 7 (Hex 00000007)
000001BA71BC95CC: arrayPtr[7] = -7 (Hex FFFFFFF9)
000001BA71BC95D0: arrayPtr[8] = 4096 (Hex 00001000)
000001BA71BC95D4: arrayPtr[9] = -4096 (Hex FFFFF000)
000001BA71BC95D8: arrayPtr[10] = 100 (Hex 00000064)
000001BA71BC95DC: arrayPtr[11] = -100 (Hex FFFFFF9C)
000001BA71BC95E0: arrayPtr[12] = 2000000000 (Hex 77359400)
000001BA71BC95E4: arrayPtr[13] = -2000000000 (Hex 88CA6C00)
000001BA71BC95E8: arrayPtr[14] = 10 (Hex 0000000A)
000001BA71BC95EC: arrayPtr[15] = -10 (Hex FFFFFFF6)
000001BA71BC95F0: arrayPtr[16] = 5 (Hex 00000005)
000001BA71BC95F4: arrayPtr[17] = -5 (Hex FFFFFFFB)
000001BA71BC95F8: arrayPtr[18] = 3 (Hex 00000003)
000001BA71BC95FC: arrayPtr[19] = -3 (Hex FFFFFFFD)
Maximum integer in array is: 2000000000
DELETING array at arrayPtr = 000001BA71BC95B0
Exit the program.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.