Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment #5 - Array Practice Due : Friday March 15th, 11:59:00pm Objective To gain practice with arrays and common array algorithms, as well as the

Assignment #5 - Array Practice

Due: Friday March 15th, 11:59:00pm

Objective

To gain practice with arrays and common array algorithms, as well as the use of array parameters in functions.

Task

This assignment will consist of writing several functions that manipulate arrays or access data from arrays, as well as a test program that will allow interactive testing of the array functions.

Part 1: Functions

In this assignment, you are required to write at least the following THREE (3) functions in addition to main() that take in and utilize the array in your program. You may have more functions in your code if you choose. A sample CALL is given for each function. NONE of these 3 functions you write should use keyboard input (cin) or print output to the screen (cout). All of that happens elsewhere.

  • Shift

    Write a function called Shift that takes in three parameters:
    • an integer array
    • the current size of the array
    • the shift amount
    This function should shift or "rotate" the elements in an array by the shift value. Here, the shift should always occur moving towards the right. For instance: Sample call:
     // Suppose the array "list" is {2, 4, 6, 8, 10, 12} //note here size is 6 Shift(list,size,3); // "list" is now {8, 10, 12, 2, 4, 6} 
    This function does not return a value.
  • Delete

    Write a function called Delete that takes in three parameters:
    • an integer array
    • the current size of the array
    • the index of the item to delete
    This function should delete the value at the given index. The remaining items in the array will need to shift over to fill the "empty" slot. The last item in the array (now vacant) should be set to 0. If the given index is out of bounds for the array, abort the function without deleting anything. This function does not return a value. Note that this does not change the "current size" of our array. Sample call:
     // Suppose the array "list" is {2, 4, 6, 8, 10, 12} //size is 6 Delete(list, 6, 2); // delete the value at index 2. // "list" is now {2, 4, 8, 10, 12, 0} //size still 6 
  • Reverse

    Write a function called Reverse that takes in two parameters:
    • an integer array
    • the current size of the array
    This function should reverse the order of the array's contents. No returned value. Sample call:
     // Suppose the array "list" is {2, 4, 6, 8, 10, 12} Reverse(list, 6); // Reverse the array "list" // "list" is now {12, 10, 8, 6, 4, 2} 
    Want a challenge?: Try to do this WITHOUT creating an additional "helper" array...

Note that none of the three required functions above you'll write above do any keyboard input or screen output.

Part 2: Test your Functions and do other fun stuffs with your main() function

To help you test and get you started, I've provided you with a STARTER FILE which you SHOULD use to start. Fill in your code into this starter file. The starter file already contains the PrintArray function. You may call this provided function anywhere you need to print the array's contents.

Write a main() function that creates an array of size MAX (a global constant given in the starter file). Since this is a constant, it can be changed for testing different sizes of arrays easily. Use this constant whenever referring to the array's MAX size from main() (instead of using a hard-coded literal value), and use the MIN constant whenever you need that value throughout your code.

YOU MAY ASSUME that the first menu option chosen will be F (To fill the array with stuff!)

FIRST, upon beginning your program, ask the user to provide to you a size between MIN and MAX (the global const variables). This will be our array's starting "current size", aka, the amount of our array we are currently choosing to use. This current size can grow or shrink later when the appropriate menu selections are made. This current size can never go below MIN or exceed MAX. Force the user to provide a valid value here if they give an invalid size.

//example, user input underlined: How big would you like to make your array? Enter a value 5 --> 40: -43 Try again: 0 Try again: 58 Try again: 5 //good size, program continues. 

Then, the program should go into a menu loop, presenting the user with the following menu:

MENU: (or SELECT Q to QUIT) F - Fill Array: RANDOM values 1 - 100 R - Reverse Array Contents X - Shift Right C - Change Array Element D - Delete Array Element A - Print Average M - Print Max Value G - Grow Array Size by 5 S - Shrink Array Size by 5 P - Print Array --------------------------- Selection > 
  • Your loop should print the above menu, and read in the users response. You're welcome to print the menu contents in whatever order you choose if you don't like my ordering. You can print the menu once in the beginning, before every entry, or as often as you choose, so long as your program is user friendly.
  • F: should fill up the array with random values between 1-100. You may assume this is the first option the user will select upon running your program. (When testing, you'll want to populate your array first so you can test your functions that you create).
  • R: No extra input required. Just call your Reverse function appropriately, then print out the array
  • X: Shift Right. Ask the user for an amount to shift right by, and shift array contents appropriately. Note that this is essentially pushing the elements to the right, and wrapping around elements from the back to the front. A zero shift value should not affect the array's contents. A negative shift value is not allowed (print an error message and do not change array's contents).
  • C: Prompt the user for the new value to be inserted, then the index to in which to change. Replace the array at the appropriate index with the new value if the requested index is valid. If it is invalid, do not make any changes to the array and print an error message.
  • D: Prompt the user to type the index to be deleted. Pass this information to your Delete function appropriately, then print out the contents of the array.
  • A: Print average of array contents like so when this option is selected:
    Array Average: valuehere 
  • M: Print max of array contents like so when this option is selected:
    Array Max: valuehere 
  • G: should grow the current array size by 5 if this would not exceed the MAX array size. Do not grow the current array size if it would exceed this limit. Fill in the new 5 elements with the value 0 (even if there was previously valid data in those array slots).
  • S: menu choice should shrink the current array size by 5 if this would not go below the MIN array size. Do not shrink the current array size if it would pass this limit. You do not need to change any array data here.
  • P: should just invoke the already given function: PrintArray and should print the array based on the current size of your array in use.
  • Q: should exit the menu loop and allow the program to end. Make sure to print out the final array before quitting the program

Hint: A good way to implement a menu loop is a switch statement (for processing a menu selection) inside of a do-while loop (for repeating the menu selection process)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions