0% found this document useful (0 votes)
40 views4 pages

7 - Chapter 7 - Arrays

Uploaded by

vunguyenhongson3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

7 - Chapter 7 - Arrays

Uploaded by

vunguyenhongson3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 7

1. Largest/Smallest Array Values


Write a program that lets the user enter 10 values into an array. The program should then display the
largest and smallest values stored in the array.

2. Rainfall Statistics
Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles.
The program should calculate and display the total rainfall for the year, the average monthly rainfall, and
the months with the highest and lowest amounts.

Input Validation: Do not accept negative numbers for monthly rainfall figures.

3. Larger Than 𝒏
In a program, write a function that accepts three arguments: an array, the size of the array, and a number
𝑛. Assume that the array contains integers. The function should display all of the numbers in the array
that are greater than the number 𝑛.

4. Monkey Business
A local zoo wants to keep track of how many pounds of food each of its three monkeys eats each day
during a typical week. Write a program that stores this information in a two-dimensional 3 × 5 array,
where each row represents a different monkey and each column represents a different day of the week.
The program should first have the user input the data for each monkey. Then it should create a report
that includes the following information:

• Average amount of food eaten per day by the whole family of monkeys.
• The least amount of food eaten during the week by any one monkey.
• The greatest amount of food eaten during the week by any one monkey.

Input Validation: Do not accept negative numbers for pounds of food eaten.

5. Lo Shu Magic Square


The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure 1. The Lo Shu Magic Square
has the following properties:

• The grid contains the numbers 1 through 9 exactly.


• The sum of each row, each column, and each diagonal all add up to the same number. This is
shown in Figure 2.

In a program you can simulate a magic square using a two-dimensional array. Write a function that accepts
a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square.
Test the function in a program.
4 9 2
3 5 7
8 1 6
Figure 1
15

4 9 2 15

3 5 7 15

8 1 6 15

15
15 15 15
Figure 2

6. Payroll
Write a program that uses the following arrays:

• empId: an array of seven long integers to hold employee identification numbers. The array should
be initialized with the following numbers:
5658845 4520125 7895122 8777541
8451277 1302850 7580489
• hours: an array of seven integers to hold the number of hours worked by each employee
• payRate: an array of seven doubles to hold each employee’s hourly pay rate
• wages: an array of seven doubles to hold each employee’s gross wages

The program should relate the data in each array through the subscripts. For example, the number in
element 0 of the hours array should be the number of hours worked by the employee whose identification
number is stored in element 0 of the empId array. That same employee’s pay rate should be stored in
element 0 of the payRate array.

The program should display each employee number and ask the user to enter that employee’s hours and
pay rate. It should then calculate the gross wages for that employee (hours times pay rate) and store them
in the wages array. After the data has been entered for all the employees, the program should display
each employee’s identification number and gross wages.

Input Validation: Do not accept negative values for hours or numbers less than 15.00 for pay rate.

7. Grade Book
A teacher has five students who have taken four tests. The teacher uses the following grading scale to
assign a letter grade to a student, based on the average of his or her four test scores.

Test Score Letter Grade


90-100 A
80-89 B
70-79 C
60-69 D
0-59 F

Write a program that uses an array of string objects to hold the five student names, an array of five
characters to hold the five students’ letter grades, and five arrays of four doubles to hold each student’s
set of test scores.

The program should allow the user to enter each student’s name and his or her four test scores. It should
then calculate and display each student’s average test score and a letter grade based on the average.

Input Validation: Do not accept test scores less than 0 or greater than 100.

8. Lottery Application
Write a program that simulates a lottery. The program should have an array of five integers named
lottery and should generate a random number in the range of 0 through 9 for each element in the
array. The user should enter five digits, which should be stored in an integer array named user. The
program is to compare the corresponding elements in the two arrays and keep a count of the digits that
match. For example, the following shows the lottery array and the user array with sample numbers
stored in each. There are two matching digits (elements 2 and 4).

lottery array:

7 4 9 1 3

user array:

4 2 9 7 3

The program should display the random numbers stored in the lottery array and the number of
matching digits. If all of the digits match, display a message proclaiming the user as a grand prize winner.

9. Name Search
You will find the following files in this Resources folder:

• GirlNames.txt—This file contains a list of the 200 most popular names given to girls born in
the United States from 2000 to 2009.
• BoyNames.txt—This file contains a list of the 200 most popular names given to boys born in the
United States from 2000 to 2009.

Write a program that reads the contents of the two files into two separate arrays or vectors. The user
should be able to enter a boy’s name, a girl’s name, or both, and the application should display messages
indicating whether the names were among the most popular.

10. 2D Array Operations


Write a program that creates a two-dimensional array initialized with test data. Use any data type you
wish. The program should have the following functions:

• getTotal. This function should accept a two-dimensional array as its argument and return the
total of all the values in the array.
• getAverage. This function should accept a two-dimensional array as its argument and return the
average of all the values in the array.
• getRowTotal. This function should accept a two-dimensional array as its first argument and an
integer as its second argument. The second argument should be the subscript of a row in the
array. The function should return the total of the values in the specified row.
• getColumnTotal. This function should accept a two-dimensional array as its first argument and
an integer as its second argument. The second argument should be the subscript of a column in
the array. The function should return the total of the values in the specified column.
• getHighestInRow. This function should accept a two-dimensional array as its first argument
and an integer as its second argument. The second argument should be the subscript of a row in
the array. The function should return the highest value in the specified row of the array.
• getLowestInRow. This function should accept a two-dimensional array as its first argument and
an integer as its second argument. The second argument should be the subscript of a row in the
array. The function should return the lowest value in the specified row of the array.

Demonstrate each of the functions in this program.

You might also like