0% found this document useful (0 votes)
13 views

Array Exercises

C++ array exercises

Uploaded by

studsleka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Array Exercises

C++ array exercises

Uploaded by

studsleka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Array Exercises

One Dimensional Array

Exercise 1
Write array declarations for the following:

a. A list of 100 integer grades


for example:
const int SIZE = 100;
int grades[SIZE];

b. A list of 50 double-precision temperatures

c. A list of 30 characters, each representing a code

d. A list of 100 integer years

e. A list of 32 double-precision velocities

f. A list of 1000 double-precision distances

g. A list of 6 integer code numbers

Exercise 2
Write correct notation for the first, third, and seventh elements of
the following arrays:

a. int grades[20]
b. double prices[10]
c. double amounts[16]
d. int dist[15]
e. double velocity[25]
f. double time[100]

Exercise 3
a. Write input statements using cin that can be used to enter values
in the first,
third, and seventh elements of each array declared in Exercise 2.
b. Write a for loop that can be used to enter values for each array
declared in Exercise 2.

Exercise 4
a. Write output statements using cout that can be used to display
values from the
first, third, and seventh elements of each array declared in
Exercise 2.

Exercise 5
a. Write, compile, and run a C++ program to input the following
values into an array named prices: 10.95, 16.32, 12.15, 8.22, 15.98,
26.22, 13.54, 6.45, and 17.59. After the
data has been entered, have your program display the values.

1|Page
Exercise 6
a. Write, compile, and run a C++ program to input 10 integer numbers
into an array named fmax and determine the maximum value entered.
Your program should contain only one loop, and the maximum should be
determined as array element values are being input.

b. Repeat Exercise 6a, keeping track of both the maximum element in


the array and the index number for the maximum. After displaying the
numbers, display these two messages
(replacing the underlines with the correct values):
The maximum value is: ____
This is element number ___ in the list of numbers

c. Repeat Exercise 6b, but have your program locate the minimum
value of the data entered.

Exercise 7
Write, compile, and run a C++ program that creates an array of five
integer numbers and displays these numbers in reverse order.

Exercise 8
Write, compile, and run a C++ program to input the following integer
numbers into an array named grades: 89, 95, 72, 83, 99, 54, 86, 75,
92, 73, 79, 75, 82, and 73. As each number is input, add the numbers
to a total. After all numbers are input and the total is obtained,
calculate the average of the numbers, and use the average to
determine the deviation of each value from the average. Store each
deviation in an array named deviation. Each deviation is obtained as
the element value less the average of all the data. Have your
program display each deviation with its corresponding element from
the grades array.

Exercise 9
Write, compile, and run a C++ program that specifies three one-
dimensional arrays named price, amount, and total. Each array should
be capable of holding 10 elements. Using a for loop, input values
for the price and amount arrays. The entries in the total array
should be the product of the corresponding values in the price and
amount arrays (so total[i] = price[i] * amount[i]). After all the
data has been entered, display the following output, with the
corresponding value under each column heading:

total price amount


----- ----- ------

Exercise 10
Using the srand() and rand() C++ library functions, fill an array of
1000 floating-point numbers with random numbers that have been
scaled to the range 1 to 100. Then determine and display the number
of random numbers having values between 1 and 50 and the number
having values greater than 50.

2|Page
Two Dimensional Arrays

Exercise 1
Write specification statements for the following:

a. An array of integers with 6 rows and 10 columns

b. An array of integers with 2 rows and 5 columns

c. An array of characters with 7 rows and 12 columns

d. An array of characters with 15 rows and 7 columns

e. An array of double-precision numbers with 10 rows and 25 columns

f. An array of double-precision numbers with 16 rows and 8 columns

Exercise 2
Determine the output produced by the following program(without using
a compiler):

#include <iostream>
using namespace std;
int main()
{
int i, j, val[3][4] = {8,16,9,52,3,15,27,6,14,25,2,10};
for (i = 0; i < 3; i++)
for (j = 0; j < 4; j++)
cout << val[i][j] << “ ”;
return 0;
}

Exercise 3
a. Write, compile, and run a C++ program that adds the values of all
elements in the val array used in Exercise 2 and displays the total.

b. Modify the program written for Exercise 3a to display the total


of each row separately.

Exercise 4
Write, compile, and run a C++ program that adds equivalent elements
of the two-dimensional arrays named first and second. Both arrays
should have two rows and three columns. For example, element [1][2]
of the resulting array should be the sum of first[1][2] and
second[1][2]. A 3rd two-dimensional arrays called sum should be
declared to store the resulting sums. The first and second arrays
should be initialized as follows:

first second
16 18 23 24 52 77
54 91 11 16 19 59

3|Page
Exercise 5
a. Write, compile, and run a C++ program that finds and displays the
maximum value in a two-dimensional array of integers. The array
should be declared as a 4-by-5 array of integers and initialized
with the data 16, 22, 99, 4, 18, -258, 4, 101, 5, 98, 105, 6, 15, 2,
45, 33, 88, 72, 16, and 3.

b. Modify the program written in Exercise 5a so that it also


displays the maximum value’s row and column subscript values.

Exercise 6
a. A professor has constructed a 3-by-5 two-dimensional array of
grades. This array contains the test grades of students in the
professor’s advanced programming class. Write, compile, and run a
C++ program that reads 15 array values and then determines the total
number of grades in these ranges: less than 60, greater than or
equal to 60 and less than 70, greater than or equal to 70 and less
than 80, greater than or equal to 80 and less than 90, and greater
than or equal to 90.

b. Entering 15 grades each time you run the program written for
Exercise 6a is a time consuming task. What method can be used for
initializing the array during the testing phase?

c. Sort each row of marks created in Exercise 6a in ascending order


and display the result in a tabular format.

4|Page

You might also like