Tuesday, June 19, 2012

Homework 9: VECTORS

1. Declare a vector named  scores of twenty-five elements of type  int .

vector < int > scores ( 25 ) ;

2. Given the vector  a , write an expression that refers to the first   element of the vector. 

a [ 0 ]

3. Given an vector  a , declared to contain 34 elements, write an expression that refers to the last   element of the vector. Instructor's notes: Do not use a member function in your solution to this problem.

a [ 33 ]

4. Assume that the vector  arr has been declared. In addition, assume that  VECTOR_SIZE has been defined to be an integer that equals the number of elements in  arr . Write a statement that assigns the next to last  element of the vector to the variable  x ( x has already been declared). 

x = arr [ VECTOR_SIZE - 2 ] ;

5. Assume that the vector  monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.). Write a statement that writes to standard output the element corresponding to October. 

cout << monthSales [ 9 ] ;

6. Given that an vector of  int named  a has been declared, assign 3 to its first  element. 

a [ 0 ] = 3 ;

7. Assume that an vector of  int variables named  salarySteps that contains exactly five elements has been declared. Write a single statement to assign the value  30000 to the first  element of this vector. 

salarySteps [ 0 ] = 30000 ;

8. Assume that an vector of integers named  salarySteps that contains exactly five elements has been declared. Write a statement that assigns the value  160000 to the last  element of the vector  salarySteps . 

salarySteps [ 4 ] = 160000 ;

9.  Assume that an vector named  a , containing exactly five integers has been declared and initialized. Write a single statement that adds ten to the value stored in the first  element of the vector. 

a [ 0 ] += 10 ;

10. Given that an vector of  int named  a with 30 elements has been declared, assign 5 to its last  element. Instructor's notes: Do not use a member function to answer this question.

a [ 29 ] = 5 ;

11. Assume v is a vector that has been declared and initialized. Write an expression whose true if there are any values stored in v.

! v. empty ( )

12. Assume v is a vector that has been declared and initialized. Write a statement that removes ALL the values stored in v.

v . clear ( ) ;

13. Assume v is a vector of integers that has been declared and initialized. Write a statement that adds the value 42 to the vector.

v . push_back ( 42 ) ;

No comments:

Post a Comment