Programming Assignment 3: Working with Two-Dimensional Arrays in C, Assignments of Computer Science

Instructions for a programming assignment that involves using two-dimensional arrays in c. The assignment includes downloading a source file and data file, writing functions to find the player with the most hits and calculate batting averages, and displaying all statistics for all players. Students are expected to become familiar with using two-dimensional arrays to solve problems and learn how to correctly pass a two-dimensional array to a function.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-3t4
koofers-user-3t4 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Name:
Sec: 06/22/2006
Programming Assignment #3
Using 2-Dimensional Arrays
Two dimensional arrays can be thought of as tables of data. A familiar example of
a table of data is the spreadsheet, such as Microsoft Excel, where you arrange data
into rows and columns. To declare a 2-Dimensional array in C, we do the
following:
const int NUM_ROWS = 8;
const int NUM_COLUMNS = 5;
int table[NUM_ROWS][NUM_COLUMNS];
The above, as you can probably guess, creates an array called table, that can
hold/represent a table with 8 rows and 5 columns. Of course, by changing the
named constants NUM_ROWS and/or NUM_COLUMNS, you can change the
size of the table that is being created and represented. However, unlike a
spreadsheet, 2-dimensional arrays in C must always start at index 0 for both the
row index and the column index. By convention, the first index is used to index
the row number, and the second index the column number. This convention is
somewhat arbitrary, they could be reversed, but in most of mathematics and
programming you will see people using this ordering when working with 2-
dimensional tables, so it is a good idea to just memorize this ordering.
So, a visual representation of the 2-dimensional table variable, declared above,
would be:
table [0] [1] [2] [3] [4]
[0]
[1]
[2]
[3]
[4] 25
[5]
[6]
[7]
pf3
pf4

Partial preview of the text

Download Programming Assignment 3: Working with Two-Dimensional Arrays in C and more Assignments Computer Science in PDF only on Docsity!

Name: Sec: 06/22/

Programming Assignment

Using 2 Dimensional Arrays

Two dimensional arrays can be thought of as tables of data. A familiar example of a table of data is the spreadsheet, such as Microsoft Excel, where you arrange data into rows and columns. To declare a 2D imensional array in C, we do the following: const int NUM_ROWS = 8; const int NUM_COLUMNS = 5; int table[NUM_ROWS][NUM_COLUMNS]; The above, as you can probably guess, creates an array called table, that can hold/represent a table with 8 rows and 5 columns. Of course, by changing the named constants NUM_ROWS and/or NUM_COLUMNS, you can change the size of the table that is being created and represented. However, unlike a spreadsheet, 2di mensional arrays in C must always start at index 0 for both the row index and the column index. By convention, the first index is used to index the row number, and the second index the column number. This convention is somewhat arbitrary, they could be reversed, but in most of mathematics and programming you will see people using this ordering when working with 2 dimensional tables, so it is a good idea to just memorize this ordering. So, a visual representation of the 2di mensional table variable, declared above, would be: table [0] [1] [2] [3] [4] [0] [1] [2] [3] [4] 25 [5] [6] [7]

To assign a value into a 2di mensional table, we do the same as for a 1 dimensional array, except we specify 2 indexes, the row, column. So, to assign the value 25 to the cell shown in the table, which is in the row with index 4 and the column with index 1, we would do: table[4][1] = 25; In this program, you will use and manipulate data in a 2di mensional array.

Objectives

Become familiar with using 2di mensional array to solve problems. Also, learn how to correctly pass a 2di mensional array to a function.

Instructions

  1. Download the PlayBall.cpp source file from the class web site. Also get the playerstats.dat file, which contains some data you will use in the program.
  2. The PlayBall.cpp source file already contains code to open up and read the player statistics from the playerstats.dat data file. The input() function performs this task. Two arrays are created in the input function, a 1 dimensional array of the player names, and a 2di mensional array of the player stats. Each row of the 2di mensional stats array holds statistics for a particular baseball player. For example, the name of the player at index 5 of the players[] array might be “Jose Consenco”. The statistics for “Jose Consenco” will be found in the 2di mensional stats array in the row with index 5. The columns of the stats table hold the following information: stats [0] Hits [1] Walks [2] Outs [0] [1] [2] ... e.g. 1 row for each player on the team Therefore, if you wanted to find the number of hits that “Jose Consenco” has, and he is the player at index 5, you would look in row 5, column 0 like this: joseConsencoHits = stats[5][0];

Program 3 Finished

You have now completed Program 3. As with previous assignments, the final step you should perform when you complete an assignment is to upload your finished assignment to your eCollege account. Go ahead at this point and upload your PlayBall.cpp source file to eCollege. Once this is done you have successfully completed the programming assignment #3.