• No results found

CSE 1321L: Programming and Problem Solving I Lab. Assignment points. 1D & 2D Arrays

N/A
N/A
Protected

Academic year: 2021

Share "CSE 1321L: Programming and Problem Solving I Lab. Assignment points. 1D & 2D Arrays"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

CSE 1321L: Programming and Problem Solving I Lab Assignment 5 – 100 points

1D & 2D Arrays

What students will learn:

1) Declaring arrays 2) Initializing arrays

3) Performing common operations on arrays

Overview: Arrays are an incredibly powerful thing in computing. Almost every audio file, video file, and image you’ve ever seen on a computer is stored in an array. An array is simply a data structure that holds a lot of things of the same type (i.e., it is homogeneous). For example, they could hold 50 integers, 100 Booleans, or a million floats. For most languages, when you see brackets [ ], you know you’re working with an array.

Assignment 5 A: Multiple Frequencies. In the last assignment, we calculated the frequency of a coin flip. This required us to have two separate variables, which we used to record the number of heads and tails. Now that we know about arrays, we can track the frequency of all numbers in a randomly generated sequence.

For this program, you will ask the user to provide a range of values (from 1 to that number, inclusive) and how long of a number sequence you want to generate using that number range.

You will then generate and save the sequence in an array. After that, you will count the number of times each number occurs in the sequence, and print the frequency of each number.

Hints: You can use multiple arrays for this assignment. One array should hold the number sequence, and another could keep track of the frequencies of each number.

Sample Output #1:

What’s the highest number you want to generate?: 5

How long of a number sequence do you want to generate?: 10 Okay, we’ll generate 10 number(s) ranging from 1 to 5!

1, 3, 1, 3, 3, 2, 4, 5, 1, 1, Frequency:

1 occurs 40.00% of the time 2 occurs 10.00% of the time 3 occurs 30.00% of the time 4 occurs 10.00% of the time 5 occurs 10.00% of the time

Sample Output # 2 :

What’s the highest number you want to generate?: 3

How long of a number sequence do you want to generate?: 7 Okay, we’ll generate 7 number(s) ranging from 1 to 3!

1, 1, 3, 1, 3, 1, 3 Frequency:

1 occurs 57.14% of the time 2 occurs 0.00% of the time 3 occurs 42.85% of the time

(2)

Assignment 5 B: Keeping Score. Now that we know about arrays, we can keep track of when events occur during our program. To prove this, let’s create a game of Rock-Paper-Scissors.

For those not familiar with this game, the basic premise is that each player says “Rock-Paper- Scissors!” and then makes their hand into the shape of one of the three objects. Winners are determined based on the following rules:

Rock beats Scissors Scissors beats Paper

Paper beats Rock

At the start of the program, it will ask the player how many rounds of Rock-Paper- Scissors they want to play. After this, the game will loop for that many number of times.

Each loop, it will ask the player what item they want to use – Rock, Paper, or Scissors.

The computer will randomly generate its own item, and a winner will be determined.

The game will then save the result as an element of an array, and the next round will begin.

Once all the rounds have been played, the program will say “Game Over” and display a list of who won each round, in order.

Hints: While the random functions we’ve discussed thus far can’t generate words, we can associate numbers with them instead. For example, we might treat the computer randomly generating 0 as if it threw Rock. We could then use that number to access a predefined String array with “Rock”, “Paper”, and “Scissors” stored inside it.

Sample Output #1:

How many games do you want to play?: 3 Round 1: What do you want to throw?: Rock Computer threw SCISSORS!

Round 2: What do you want to throw?: Paper Computer threw PAPER!

Round 3: What do you want to throw?: Rock Computer threw PAPER!

Game Over...

Here’s the recap:

Player won Round 1 Tied on Round 2 Computer won Round 3 Sample Output # 2 :

How many games do you want to play?: 2

Round 1: What do you want to throw?: Scissors Computer threw ROCK!

Round 2: What do you want to throw?: Paper Computer threw SCISSORS!

Game Over...

Here’s the recap:

Computer won Round 1 Computer won Round 2

(3)

Assignment 5 C: Level Map Creator. There are a variety of ways that game developers store their level layouts. One simple method is to associate level elements with certain symbols, and then storing them in a 2D grid inside a text file. We will use our knowledge of 2D arrays to create a very simple Level Map Creator tool.

The program should prompt the user to enter a width and height for the level. Then it should initialize a 2D array and fill every element with the “*” symbol. Afterwards, the user should be given the following options via a menu:

1. Clear Level Re-initialize the 2D array

and fill every element with the “*” symbol.

2. Add Platform Prompt the user to enter a starting point and length for the horizontal platform. Replace those elements in the 2D array with the “=” symbol. If the length is longer than the number of columns (or out of bounds), notify the user that this is not possible.

3. Add Item Prompt the user to enter a column and row index. Replace that element in the 2D array with the “O” symbol. If the column and row index in outside the bounds of the array, notify the user that this is not possible.

4. Quit: End the program

After completing the task, print the modified 2D array, If anything other than Quit is selected, display the menu again. Otherwise, tell the player “Good bye!” and stop.

Hints: Since we’re using a 2D array to represent the level map, we’ll use its indexes for our level coordinates. 0,0 will be the top-left corner of the map, and will correspond to array[0][0].

Sample Output # 1 : [FYE Level Map Creator]

Enter a level map width: 20 Enter a level map height: 6

********************

********************

********************

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 2 [Add Platform]

Enter X Coordinate: 1 Enter Y Coordinate: 1 Enter Length: 5

(4)

********************

*=====**************

********************

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 3 [Add Item]

Enter X Coordinate: 30 Enter Y Coordinate: 30

This is not a valid location!

********************

*=====**************

********************

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 3 [Add Item]

Enter X Coordinate: 3 Enter Y Coordinate: 0

***O****************

*=====**************

********************

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 2 [Add Platform]

Enter X Coordinate: 3 Enter Y Coordinate: 1 Enter Length: 29

This platform won’t fit in the level!

***O****************

*=====**************

********************

(5)

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 1 [Clear Level]

********************

********************

********************

********************

********************

********************

Options

1. Clear Level 2. Add Platform 3. Add Items 4. Quit

Enter a choice: 4

********************

********************

********************

********************

********************

********************

Goodbye!

Submission:

1. You will submit 3 separate files

2. File names and class names must be correct.

3. Upload all files (simultaneously) to the assignment submission folder in Gradescope.

References

Related documents