| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Exercises - Arrays

Page history last edited by Steve Sweeney 5 years, 11 months ago

Part A: 1-Dimensional Arrays

 

  1. Write a program that uses an array to store 10 student grades.  Ask the user to enter each grade.  When the 10th grade is entered, print out the list of grades. (Turing Solution) (Java Solution)

    Extensions:

    (a) Output the list of marks forwards and backwards. (Turing Solution)

    (b) Determine the average of the grades by traversing the array using a for-loop. (Turing Solution)

    (c) Add a second array (also 10 elements) of strings.  Have the user enter their course as well as their mark.  Print out courses and marks by traversing the array and output the average at the end of the list.

    (d) Modify the output of the array to use a procedure instead (call it "printArray()" or something similar).

     

  2. Write a program to keep track of many rolls of a single die (each roll should be random).  Use an array to keep track of each roll (1 to 6).  Allow the user to choose the number of rolls.  After the rolling is complete, display the results for each possible roll (1 to 6). (Turing Solution)

    Extensions:

    (a) When displaying the results for a single roll, also display the percentages for that roll (i.e., what percent of the rolls were 1, or 2, or 3, etc.)

    (b) Simulate two dice being rolled.  The user picks number of rolls.  Display percentages.

    (c)  When performing a large simulation, there can be dead time on the monitor (nothing happens to let the user know the program is still running).  Experiment with ways to show the user that the program is running (think of progress bars or other indicators you might have seen with programs such as installers). (Turing Solution)

     

  3. Write a program that asks the user for a name and address.   Each is to be stored in a separate array (one for names and one for addresses), with 5 elements in each array. 
    Extensions:
    (a) Once data is entered, print off the list.  Now let the user enter a name and show them the corresponding address (i.e., you will have to search the "name" array). (Turing Solution)

    (b)  After the user selects a name, give them the option to change the address and save the new address in the array.  Whenever an address is changed, output the entire list again.

    (c) Create a more useful "search" that lets the user enter any string (i.e., a single letters or group of letters), and then find all names with those letters.  Output the names and addresses for all matches.

     

  4. Write a program that fills an array of 100 elements with random integers between 1 and 100 (each number should only be represented once).  Output the arrays in a meaningful way (i.e., identify each element #), and so that it fits on the screen.  Focus on making your output algorithm as efficient as possible.

    (a)  Create a second array of the same size.  Copy the elements of the first array into the second array, but in reverse order (i.e., element#100 from the 2nd array should match element#1 from the 1st array).  Output both arrays.  Again, the entire output should fit on a single screen.

     

  5. Write a program that asks the user for 5 numbers (integer or real, it's up to you) and stores them in an array.  Write a function that will test if the array is in order (sorted from lowest to highest).  The function should return true or false, depending on the sorting of the array.  Inform the user of the result.

    Extensions:

    (a) If the array is not in order, output the array and somehow show the user where the ordering first failed.  For example, if the numbers were 1, 2, 10, 4, 5, you would print out the list and somehow make the number 10 stand out as the "problem".  You may need to change your function to a procedure to accomplish this.

     

  6. Write a function that will locate the minimum element of an array and output the index of that element using a meaningful message.  If there is more than one such value, give the index of the first minimum value.  Write a program that will let you test your function.

    (a) Add another function that will output the minimum value from all elements in the array.  Test it.

    (b) Add a function that will provide the index of the maximum value in the array.  Test it.

    (c) Add a function that will output the maximum value from all elements in the array.  Test it.

    (d) Write a procedure that will find the two largest values in an array (they could be the same value if there are repeated values).

     

  7. Write and test a subprogram that will rotate (or shift) an array by N spaces.  A positive number will shift the array down.  Values that go off the end of the array will "wrap" back to the beginning.  The user should be able to enter positive or negative values, and the program should output the array before and after the shift.

    For example, suppose you have an array with { 1, 2, 3, 4, 5 }.  A rotation of +1 would give { 5, 1, 2, 3, 4 } and +2 would give { 4, 5, 1, 2, 3 }.

     

Part B: 2-Dimensional Arrays

 

  1. Write a program that uses a 2-D array to track the rolls of two dice (6 rows and 6 columns for the array).  Ask the user how many rolls they want.  Record a running-total of your results in your 2-D array (e.g., if the roll is 1-1, add 1 to cell at row-1 and column-1; a roll of 3-6 would be stored at row-3, column-6; etc.).  Once the rolls are complete, output the results in a meaningful way (some kind of grid would be best, with labels for the rolls along the top and side).

    (a) Give the user the option to display results as raw data or percentages.  Change your output accordingly.

    (b) When the rolls are complete, traverse the 2-D array and create a running total of the total roll (i.e., from 2 to 12) in a 1-D array.  Output the results as raw data or percentages as specified by the user.

     

  2. Write a program that will fill a 10x10 boolean grid (use a 2-D array) with false values.  Then write a loop to randomly assign true to some of these spaces . When done, output the grid in some meaningful way (e.g., using 'T' and 'F' to show true or false in each space).

    (a) Experiment with other ways to use this idea to fill the screen with various random patterns.  For example, leave all "false" squares blank, and for "true" squares, output a character of your choice.

     

     

  3. Consider the following very difficult math problem:  Given a square with side length of 1, what is the average distance between any two randomly placed points (see the following link for an explanation of the problem and the mathematical solution).  You can also obtain an approximate solution to this problem using 2D arrays.

Comments (0)

You don't have permission to comment on this page.