Question

Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index...

  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.
0 1
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Suppose v is an array with 100 int elements. If 100 is assigned to v[100], what happens? Show the code. An array of 1000 integers is declared. What is the largest integer that can be used as an index...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • Write a java code that Declares and initialize a two-dimensional int array named grades. It should...

    Write a java code that Declares and initialize a two-dimensional int array named grades. It should have 10 rows and 6 columns where it stores the values and then calculates the average of all the elements in the grades array that you have declared

  • Write a C program to do the following...in this EXACT order: a. Declare an integer array...

    Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...

  • Write code that declares an array of integer values that will represent the first five even...

    Write code that declares an array of integer values that will represent the first five even numbers: 2, 4, 6, 8, 10. Add code to initialize each element of the array with the even number values shown above. Add code to calculate the product of the array elements, and store the result in a variable named product. You must access the array elements to accomplish this. Do not write product = 2 * 4 * 6 * 8 * 10;...

  • C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point...

    C++ pointers and linked lists 1. Declare an integer pointer variable intPointer. Initialize it to point to an int variable named someInt. Assign the value 451 to someInt and output (cout) the variable someInt and output (cout) the value pointed to by intPointer. Write an assignment statement that indirectly stores 900 into the value pointed to by intPointer. Output (cout) the value pointed to by intPointer and output (cout) the variable someInt, 2. Declare a pointer variable charArrPointer and initialize...

  • QUESTION 1 Using the following declarations and a member of the Array class, int [ ]...

    QUESTION 1 Using the following declarations and a member of the Array class, int [ ] bArray = new int [10]; int location; Using a method in the Array class, write a statement that would change the order of the elements in the bArray array. The contents of the first cell should hold what was in the last cell. The second cell should hold what was in the next to last cell. __________________________ HINT: You must write the full statement...

  • Java Code please read comments and add the code to required lines....LINE 1 to LINE 5...

    Java Code please read comments and add the code to required lines....LINE 1 to LINE 5 ********************************************************************** * Program Summary: This program demonstrates these basic Java concepts: * - Creating an array based on user input * - Accessing and displaying elements of the array * * The program should declare an array to hold 10 integers. * The program should then ask the user for an integer. * The program should populate the array by assigning the user-input integer...

  • Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not...

    Assignment 06 – Ten Array Methods You must work in alone on this assignment. Do not use any Java language features we have not cover so far in this course. Assignment Objectives After completing this assignment the student should be able to:  Declare and instantiate arrays  Access array elements by index  Use loops and decisions to manipulate arrays and array elements  Write methods that manipulate arrays  Write methods that take array arguments  Write methods...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT