SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Arrays
Exercise:
Self-test Exercises
Question:10 | ISBN:9780321531346 | Edition: 7

Question

Suppose you have the following array declaration in your program:

int your_array[7];

Also, suppose that in your implementation of C++, variables of type int use two bytes of memory. When you run your program, how much memory will this array consume? Suppose that when you run your program, the system assigns the memory address 1000 to the indexed variable your_array[0].

What will be the address of the indexed variable your_array[3]?

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

In C++, the memory consumed by an array is determined by the size of each element multiplied by the total number of elements in the array. In this case, the array your_array is declared as int your_array[7], and assuming that variables of type int use two bytes of memory, the total memory consumed by the array would be:

       Total memory = size of each element * total number of elements

       Total memory = 2 bytes * 7 elements

        Total memory = 14 bytes

Therefore, the array your_array will consume 14 bytes of memory.

 

Given that the system assigns the memory address 1000 to the indexed variable your_array[0], then calculate the address of the indexed variable your_array[3] by taking into account the size of each element.

To calculate the address of your_array[3], we can use the following formula:

     Address of element N = Address of the first element + (N * size of each element)

     Address of your_array[3] = 1000 + (3 * 2)

     Address of your_array[3] = 1000 + 6

     Address of your_array[3] = 1006

 

Hence, the address of the indexed variable your_array[3] would be 1006.

0 0

Discussions

Post the discussion to improve the above solution.