Lab Exercises in Parallel Arrays and Two-Dimensional Arrays, Lab Reports of Computer Science

Two lab exercises from a computer science course. The first exercise introduces parallel arrays and processing them. The second exercise focuses on manipulating data in a two-dimensional array, including initializing, printing, inputting, summing, and finding the largest element in a column. Students are required to write designs and programs based on the given objectives.

Typology: Lab Reports

Pre 2010

Uploaded on 08/17/2009

koofers-user-1ew
koofers-user-1ew 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Sec: 09/20/2005
Lab 2.1 Coding with Parallel Arrays
Parallel arrays are two or more arrays whose corresponding components hold related
information. If you need to keep track of multiple items that are related, you can create
more than one array where the information is related by the placement within the
arrays.
Objectives
In this lab, you process parallel arrays.
After completing this lab, you will be able to:
Process parallel arrays.
Coding with Parallel Arrays
In the following exercises, select the correct answer for each question. Then design
and write a program that uses parallel arrays.
Question Answer
(Circle the correct answer)
1. Parallel arrays can be different data types. T F
2. Parallel arrays can be different sizes. T F
3a. Create the design for a program that keeps track of the hits, walks, and outs
of a baseball team. Use parallel arrays to keep track of each player’s statistics.
The player number is the index of the array. You will be reading in data from
the file called PlayBall.txt, which contains data that includes the player
number, hits, walks and outs. The data file will contain multiple games. Not
every player will have stats in each game. The arrays should be initialized to
zero (0) and should be accumulators to allow for stats for each game of the
season.
Write your design in the following space. Your design should be a list of C++
comments without any code:
pf3
pf4
pf5

Partial preview of the text

Download Lab Exercises in Parallel Arrays and Two-Dimensional Arrays and more Lab Reports Computer Science in PDF only on Docsity!

Name:

Sec: 09/20/

Lab 2.1 Coding with Parallel Arrays

Parallel arrays are two or more arrays whose corresponding components hold related information. If you need to keep track of multiple items that are related, you can create more than one array where the information is related by the placement within the arrays.

Objectives

In this lab, you process parallel arrays. After completing this lab, you will be able to: Process parallel arrays.

Coding with Parallel Arrays

In the following exercises, select the correct answer for each question. Then design and write a program that uses parallel arrays.

Question Answer

(Circle the correct answer)

1. Parallel arrays can be different data types. T F

2. Parallel arrays can be different sizes. T F

3a. Create the design for a program that keeps track of the hits, walks, and outs

of a baseball team. Use parallel arrays to keep track of each player’s statistics.

The player number is the index of the array. You will be reading in data from

the file called PlayBall.txt , which contains data that includes the player

number, hits, walks and outs. The data file will contain multiple games. Not

every player will have stats in each game. The arrays should be initialized to

zero (0) and should be accumulators to allow for stats for each game of the

season.

Write your design in the following space. Your design should be a list of C++

comments without any code:

3b. Write a C++ program based on the design you created in Exercise 3a. You are given

a start on the program which opens up the PlayBall.txt file and reads in the data for

you. The file, named PlayBall.cpp , may be found on the class website.

3. Given:

int stats[2][3] = {4, 4, 4, 3, 12, 15}; int totalHits = 0; for (int row=0; row<2; row++) totalHits += stats[row][0];

What is the value of totalHits?

4. Given

int stats[2][3] = {4, 4, 4, 3, 12, 15}; int atBats[2] = {0}; for (int row=0; row<2; row++) { for (int column = 0; column < 3; column++) atBats[row] += stats[row][column]; cout << “Player “ << row + 1 << “was up to bat “ << atBats[row] << “times.” << endl; }

What is the output?

5a. Redesign your PlayBall.cpp to use a two-dimensional array instead of

parallel arrays. Add an additional column that keeps track of the number of

times a player was up to bat. When printing the report, add a totals line that

totals the number of hits, walks, and outs for the team. Find the player with

the greatest number of hits, the player with the greatest number of walks,

and the player with the greatest number of outs.

Modify your PlayBall.cpp program to use two-dimensional arrays and

name it PlayBall2.cpp.

Enter, compile, link and execute PlayBall2.cpp.

The following is a copy of the screen results that might appear after running

your program, depending on the data entered.