Professional Documents
Culture Documents
Assignment 13
Assignment 13
Page 1|6
7.6(FILL IN THE BLANKS)
c) list of values.
d) p*q
e) fractions[6][3]
a) The vector member function at provides bounds checking and throws an exception if its
argument is an invalid subscript.
True.
The std::vector class provides the at member function, which provides bounds checking on the input
argument and throws an std::out_of_range exception if the argument is an invalid subscript.
b) the value of an element of a vector can be accessed or modified using curly brackets ({})
True.
Page 2|6
In C++, the value of an element of a vector can be accessed or modified using the subscript operator [] or
using curly brackets {}. This is because std::vector overloads both operators to provide access to its
elements.
False
In C++, to indicate that you want to reserve space for an integer array p with 50 locations, you would use
the std::vector class and call its reserve member function
d) A for statement must be used to initialize the element of a 25 element array to one.
False.
In C++, you can use various methods to initialize the elements of an array to a specific value, and a for
loop is not necessarily required.
e) Two nested for statements must be used to total the elements of a three dimensional array.
False
If you want to sum the elements of a three-dimensional array in C++, you need to use two nested for
loops to iterate through each element of the array. The outer for loop iterates over the first dimension of
the array, while the inner for loops iterate over the second and third dimensions of the array.
b) read the value from the keyboard for the element 2 of one dimentional integer array marks.
float c[200];
total += c[i];
Page 3|6
cout << "Total = " << total << endl;
e) copy array a into the first portion of array b Assume that both arrays contain doubles and that
arrays a and b have 17 and 41 elements, respectively.
b[i] = a[i];
k. Write a statement that input the values for the elements of t from the keyboard.
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << "Enter value for t[" << i << "][" << j << "]: ";
cin >> t[i][j];
}
}
l. Write a series of statements that determine and display the smallest value in array t.
int smallest = t[ 0 ][ 0 ];
Page 4|6
for ( int r = 1; r < 2; ++r ) {
for ( int c = 1; c < 3; ++c ){
if ( t[ r ][ c ] < smallest )
smallest = t[ r ][ c ];
cout << smallest;
}}
m. Write a statement that displays the elements of the 0 row of t
cout << t[ 0 ][ 0 ] << ’ ’ << t[ 0 ][ 1 ] << ’ ’ << t[ 0 ][ 2 ] << endl;
n. Write a statement that totals the elements of the column 2 of t.
int sum = 0;
for (int i = 0; i < 2; i++) {
sum += t[i][1];
}
o. 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.
cout << " 0 1 2"<<endl;
for ( int r = 0; r < 2; ++r )
{ cout << r << ’ ’;
for ( int c = 0; c < 3; ++c )
cout << t[ r ][ c ] << " ";
cout << ’\n’; }
Page 6|6