2-Dimensional Arrays in C: Assignment #3, Assignments of Computer Science

Instructions for completing a programming assignment in c that involves using 2-dimensional arrays. 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. The document also explains how to declare and assign values to 2-dimensional arrays in c.

Typology: Assignments

Pre 2010

Uploaded on 08/16/2009

koofers-user-l1c
koofers-user-l1c 🇺🇸

6 documents

1 / 3

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]
To assign a value into a 2-dimensional table, we do the same as for a 1-
dimensional array, except we specify 2 indexes, the row, column. So, to assign
pf3

Partial preview of the text

Download 2-Dimensional Arrays in C: Assignment #3 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 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] To assign a value into a 2-dimensional 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 2-dimensional array.

Objectives

Become familiar with using 2-dimensional array to solve problems. Also, learn how to correctly pass a 2-dimensional 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 2-dimensional array of the player stats. Each row of the 2-dimensional 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 2-dimensional 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];
  3. Write a function that discovers which player has the most number of hits on the team. The function should be called maxHits() and should take the stats 2-dimensional array and the number of players on the team as parameters. In this function, you need to search through the values in the