Suppose v is an array with 100 int elements. if 100 is assigned to v[100]. what happens?

  1. Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code.
  2. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code.
  3. Consider the declaration: int v[1]; What is the index of the last element of this array?
  4. Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the values 10, 20, …, 100, respectively.
  5. Declare an array named taxRates of 5 elements of type double and initialize the elements (starting with the first) to the values 0.10, 0.15, 0.21, 0.28, 0.31, respectively.
  6. Write a statement to declare and initialize an array of ints named denominations that contains exactly six elements. Your declaration statement should initialize the elements of the array to the following values: 1, 5, 10, 25, 50, 100. (The value 1 goes into the first element; the value 100 to the last.)
  7. Given the array a, write an expression that refers to the first element of the array.
  8. Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.

Answer

1. Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code.
Answer:
v[100] refers to 101th element of v. In C and C++, though the size of the array is 100 you can still access 101th element as arrays internally work as pointers that point to continuous memory locations. Array always point to the first element in the array. In languages like Java and C# we cannot the element out the array size it throws index out of bounds exception.
2. An array of 1000 integers is declared. What is the largest integer that can be used as an index to the array? Shows the code.
Answer: The largest integer that can be used as an index is 999. Since indexing starts from 0 the last index of array of 1000 elements is 999. The indexes will be [0] [1] … [998][999]
Code: int arr[1000]; //array of 1000 element
arr[999] is the code to access the largest integer that can be used as index. 3. Consider the declaration: int v[1]; What is the index of the last element of this array?
Answer: Index of the last element of v is 0.
As the size of the array v is 1, v has only one element whose index is 0. Hence index of the first and last element in v is 0.
4. Declare an array named a of 10 int elements and initialize the elements (starting with the first) to the values 10, 20, …, 100, respectively.
Answer:
int a[10]= {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; // both declaration and initialization can be done in single line or it can be done as below int a[10]; a[0] = 10; a[1] = 20; a[2] = 30; a[3] = 40; a[4] = 50; a[5] = 60; a[6] = 70; a[7] = 80; a[8] = 90; a[9] = 100;
5. Declare an array named taxRates of 5 elements of type double and initialize the elements (starting with the first) to the values 0.10, 0.15, 0.21, 0.28, 0.31, respectively.
Answer:
double taxRates[5] = {0.10, 0.15, 0.21, 0.28, 0.31} // both declaration and initialization can be done in single line or can be done as below double taxRates[5]; taxRates[0] = 0.10; taxRates[1] = 0.15; taxRates[2] = 0.21; taxRates[3] = 0.28; taxRates[4] = 0.31;
6. Write a statement to declare and initialize an array of ints named denominations that contains exactly six elements. Your declaration statement should initialize the elements of the array to the following values: 1, 5, 10, 25, 50, 100. (The value 1 goes into the first element; the value 100 to the last.)
Answer:
int denomination[6] = {1, 5, 10, 25, 50, 100}; 7. Given the array a, write an expression that refers to the first element of the array.
Answer: a[0] refers to the first element of the array.
8. Given an array a, declared to contain 34 elements, write an expression that refers to the last element of the array.
Answer: a[33] refers to the last element of the array.

Let me know if you have any concerns with the above solution.

Leave a Comment