Objective: To show our understanding of arrays, specifically how to declare, initialize and manipulate the elements of an array. To further our understanding of functions, loops, conditional statements, data types and variables.

Assignment: Write a program that uses an array to store a series of integers. The program should prompt the user to enter elements into the array. After the array has been filled, it should display the initial contents of the array. It should then perform operations to manipulate the elements of the array through specific functions. Note that if the array is modified in any way, the program should re-display the contents of the array.

Procedure:

1. Declare an array of integers of some pre-defined maximum size. You are free to decide the size of the array, but all the elements of the array should be initialized to zero.

2. Implement the following operations on the array through the specified functions:

  • input() This function prompts the user to populate the array with data entered from the keyboard. As this is an array of integers, the data should all be numeric integers.
  • display() This function outputs the contents of the array one element at a time. The array should be output using the following format:
Postion Value
------- -----
1 10
2 3
3 5
.
.
.
n 7
  • Note: position, represents the logical index of the array and value represents the numeric integer stored in the corresponding physical location. In other words position 1 corresponds to the value at array[0].
  • statistics() This function computes the minimum, maximum, sum, and average of all the elements in the array. Note that this function simply computes the information, it does not display it, that should happen in the body of the main function.
  • count() This function accepts a number and determines how many times, if at all, the number appears as an element of the array. The function should return this value.
  • search() This function searches the array for a specific element and returns the position in the array of the first instance of that element. If the specified element does not appear in the array, the function should return a -1.
  • replace() This function replaces all instances of a specific element with a new value. The function should receive the value to replace and the new value to replace it with. The function should return the number of times it replaced the value.
  • modify() This function modifies the element in a specific position of the array. It should receive as arguments both the postion of the array (i.e. 1 to n) to modify and the new value to assign to that position. Note that the position must be within 1 and the maximum size of the array but the position modifed is the corresponding physical location (i.e. array[0] to array[n-1]). The function should return a boolean variable of true or false respectively indicating whether the modification took place. In other words if the position is not within the bounds of the array, the function should return false.
  • clear() This function either clears a single element in the array or every element of the array. The function should determine which to do based on the argument passed to it. If a -1 is passed, the function should clear the entire array, otherwise, it should clear the element at the specified position, assuming off course that the specified position is within the bounds of the array. This function should return a boolean variable of true or false respectively indication whether or not it was able to accomplish its' task.

3. Write the code to control the execution of our program and to test each operation of the array within the body of the main() function.

Note: With the exception of the input() and display() functionis, each of the remaining functions should be independent of all cin and cout statements. In other words, any and all information required by the function should be passed in through parameters. Likewise, information from the function that the caller needs should be returned back either through the return statement or through reference parameters. All information required should be input by the user prior to the function being invoked and all returned information should be output after the function returns control to the caller. In addition, the user should not know anything about the zero based characteristic of arrays. Therefore, if the user needs to specify a location in the array, they should enter from the range 1 to the maximum size of the array and not 0 to max size - 1.

Following is one possible sample run, but feel free to be creative:

Welcome to the world of Arrays!!!!!

Enter element 1: 5
Enter element 2: 7
Enter element 3: 9
Enter element 4: 11
Enter element 5: 5

You have entered:

Postion Value
------- -----
1 5
2 7
3 9
4 11
5 5

The sum of all the numbers entered is: 37
The average of all the numbers entered is: 7.4
The largest number entered is: 11
The smallest number entered is: 5

What number would you like to count? 5
The number 5 appears 2 times.

What number would you like to search for? 5
The number 5 is in position 1.

What element would you like to replace? 5
Enter the new value: 17
The element 5 was replaced 2 times.

Postion Value
------- -----
1 17
2 7
3 9
4 11
5 17

What position would you like to modify? 7
That is an invalid position, please enter a position between 1 and 5: 9
That is an invalid position, please enter a position between 1 and 5: 3
Enter the new value: 13

The contents of your array are now:

Position Value
------- -----
1 17
2 7
3 13
4 11
5 17

What position would you like to clear? Enter the position or -1 to clear entire array: 9
That is an invalid position, please enter a position between 1 and 5, or -1 to clear entire array: -1

The contents of your array are now:

Position Value
------- -----
1 0
2 0
3 0
4 0
5 0
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.