Struc@Array Question

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

7.

1 Introduction to Arrays 387

For example:

int arr[] = {2, 4, 6, 8};


for (int x : arr)
cout << x;
cout << endl;

This will output: 2468


When defining the variable that will iterate through the array we can
use the same modifiers that are available when defining a parameter for
a function. The example we used above for variable x is equivalent to
pass-by-value. If we change x inside the loop it doesn’t change the array.
We could define x as pass by reference using & and then changes to x
will be made to the array. We could also use const to indicate that the
variable can’t be changed. The example below increments every element
in the array and then outputs them. We used the auto datatype in the
output loop to automatically determine the type of element inside the
array.

int arr[] = {2, 4, 6, 8};


for (int& x : arr)
x++;
for (auto x : arr)
cout << x;
cout << endl;

This will output: 3579. The range-based for loop is especially convenient
when iterating over vectors, which are introduced in Chapter 8, and iterating
over containers, which are discussed in Chapter 18. ■

S el f -T est Exerc ises

  1. Describe the difference in the meaning of int a[5] and the meaning of
a[4]. What is the meaning of the [5] and [4] in each case?

  2. In the array declaration


double score[5];

state the following:

a. The array name


b. The base type
c. The declared size of the array
d. The range of values that an index for this array can have
e. One of the indexed variables (or elements) of this array
388 Chapter 7 / Arrays

  3. Identify any errors in the following array declarations.


a. int x[4] = { 8, 7, 6, 4, 3 };
b. int x[ ] = { 8, 7, 6, 4 };
c. const int SIZE = 4;
d. int x[SIZE];

  4. What is the output of the following code?


char symbol[3] = {'a', 'b', 'c'};

for (int index = 0; index < 3; index++)


cout << symbol[index];

  5. What is the output of the following code?


double a[3] = {1.1, 2.2, 3.3};
cout << a[0] << " " << a[1] << " " << a[2] << endl;
a[1] = a[2];
cout << a[0] << " " << a[1] << " " << a[2] << endl;

  6. What is the output of the following code?


int i, temp[10];

for (i = 0; i < 10; i++)


temp[i] = 2 * i;

for (i = 0; i < 10; i++)


cout << temp[i] << " ";
cout << endl;

for (i = 0; i < 10; i = i + 2)


cout << temp[i] << " ";

  7. What is wrong with the following piece of code?


int sample_array[10];

for (int index = 1; index <= 10; index++)


sample_array[index] = 3 * index;

  8. Suppose we expect the elements of the array a to be ordered so that


a[0] ≤ a[1] ≤ a[2] ≤ ...

However, to be safe we want our program to test the array and issue a warning
in case it turns out that some elements are out of order. The following code is
supposed to output such a warning, but it contains a bug. What is it?
double a[10];
<Some code to fill the array a goes here.>
7.2 Arrays in Functions 389

for (int index = 0; index < 10; index++)


if (a[index] > a[index + 1])
cout << "Array elements " << index << " and "
<< (index + 1) << " are out of order.";

  9. Write some C++ code that will fill an array a with 20 values of type int read
in from the keyboard. You need not write a full program, just the code to do
this, but do give the declarations for the array and for all variables.

10. 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
2 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]?

7.2 Arrays in Functions


You can use both array indexed variables and entire arrays as arguments to
functions. We first discuss array indexed variables as arguments to functions.

Indexed Variables as Function Arguments


An indexed variable can be an argument to a function in exactly the same
way that any variable can be an argument. For example, suppose a program
contains the following declarations:
int i, n, a[10];

If my_function takes one argument of type int, then the following is legal:
my_function(n);

Since an indexed variable of the array a is also a variable of type int, just like n,
the following is equally legal:
my_function(a[3]);

There is one subtlety that does apply to indexed variables used as


arguments. For example, consider the following function call:
my_function(a[i]);

If the value of i is 3, then the argument is a[3]. On the other hand, if the
value of i is 0, then this call is equivalent to the following:
my_function(a[0]);

You might also like