Chapter 4 Exercices
1
1) Write C++ statements to accomplish each of the following:
a)
Display the value of the seventh element of character array f.
b)
Input a value into element 4 of single floatingpoint array b.
c)
Initialize each of the 5 elements of single integer array g to 8.
d)
Total and print the elements of floatingpoint array c of 100 elements.
e)
Copy array “a” into the first portion of array “b”.
Assume that the two
arrays have the following declaration:
double
a[11], b[34];
f)
Determine and print the smallest and largest values contained in 99
element floatingpoint array w.
Answer
:
a)
cout << f[6] << '\n';
b)
cin >> b[3];
c)
int
g[5]={8,8,8,8,8};
or
for
(
int
j = 0; j < 5; ++j)
g[j] = 8;
d)
float
total = 0;
for
(
int
k = 0; k < 100; ++k) {
total += c[k];
cout << c[k] << '\n'
}
cout << total << '\n'
e)
for
(
int
i = 0; i < 11; ++i)
b[i] = a[i];
f)
//assume all variables are declared and initialized
for
(
int
j = 0; j < 99; ++j)
if
(w[j] < smallest)
smallest = w[j];
else if
(w[j] > largest)
largest = w[j];
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Chapter 4 Exercices
2
2) Consider a 2by3 integer array “t”.
a)
Write a declaration for “t”.
b)
How many rows does “t” have?
c)
How many columns does “t” have?
d)
How many elements does “t” have?
e)
Write the names of all elements in the second row of “t”.
f)
Write the names of all the elements in the third column of “t”.
g)
Write a single statement that sets the element of “t” in row 1 and column
2 to zero.
h)
Write a series of statements that initialize each element of “t” to zero. Do
not use a loop.
i)
Write a nested "
for
" structure that initializes each element of the
double array "t" to zero.
j)
Write a statement that inputs the values of the elements of “t” from the
terminal.
k)
Write a series of statements that determine and print the smallest value
in array “t”.
l)
Write a statement that displays the elements of the first row of “t”.
m) Write a statement that totals the elements of the fourth column of “t”.
n) Write a series of statements that prints the array t in neat, tabular
format.
List the column subscripts as headings across the top and list
the row subscripts at the left of each row.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '10
 DR.M.G
 Array, Row, The Elements

Click to edit the document details