Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Monday, 20 February 2012

Copy One Dimensional Arrays in C++

If you have two integer variables, you can easily copy the value of one to the other with this code:

int1 = int2;

If you try the same thing with arrays,

mArray1 = mArray2;

the code will not compile. You will get a compile error message.

Each element of the array has to be assigned from one variable to the other.

Sample Code:

The sample code will create array variables, A and B, of integer type. Values are assigned to each element of A then copied to B.

The sample code was created in MS Visual C++ as a Windows Form Application. It contains a form (Form1) that displays when the program runs. The sample code, contained in the Form Load Event, executes and adds text to a RichTextBox named rtOut.

// Declare one-dimension arrays.
int A[10], B[10];
rtOut->Text = "Declare two one-dimension arrays: ";
rtOut->Text += "int A[10], B[10]; \n\n";

// Declare counter.
int i = 0;

// Load array with multiples of 10s.
rtOut->Text += "Load array A with multiples of 10s. \n\n";
for (i; i < 10; i++) A[i] = i * 10;

// Copy value of A to B.
// Can't use: A = B;
rtOut->Text += "Copy value of A to B, element by element with B[i] = A[i]. \n\n";
// Counter i reset to 0.
for (i = 0; i < 10; i++) B[i] = A[i];

// Show content.
// Counter i reset to 0.
for (i = 0; i < 10; i++)
{
rtOut->Text += "The A[" + i + "] value is " + A[i] + ". The B value is " + B[i] + "\n";
}

Output:

Declare two one-dimension arrays: int A[10], B[10];

Load array A with multiples of 10s.

Copy value of A to B, element by element with B[i] = A[i].

The A[0] value is 0. The value of B at index 0 is 0
The A[1] value is 10. The value of B at index 1 is 10
The A[2] value is 20. The value of B at index 2 is 20
The A[3] value is 30. The value of B at index 3 is 30
The A[4] value is 40. The value of B at index 4 is 40
The A[5] value is 50. The value of B at index 5 is 50
The A[6] value is 60. The value of B at index 6 is 60
The A[7] value is 70. The value of B at index 7 is 70
The A[8] value is 80. The value of B at index 8 is 80
The A[9] value is 90. The value of B at index 9 is 90



The Basics of a One-dimensional Array in C++

Here is a discussion on creating and using a one-dimensional array in C++.

An array is a variable of one data type with multiple values. In C++, each array holds values of the same date type (i.e., all integers or all floating point numbers). It’s a computer version of a matrix from mathematics. A one-dimensional array is similar to a matrix with one row or one column.

An array is created by specifying the data type (e.g., char, int) followed by a user-defined variable name and the maximum number of elements in square brackets.

type name[size];

For example, this declaration

int myArray[10];

creates an array called myArray with 10 elements of type integer.

Each element of the array is accessed by an index. The base index is 0 and increases by 1 to the size of the array less one (size – 1). The indices of an array with 10 elements are 0 to 9.

You access an element of an array by specifying its name followed by the specific index number in square brackets. There can be spaces between the name of the array and the square brackets, but convention is to not have a space.

variable_name[index]  // No space.
variable_name [index] // Space in between.

For example,

myArray[0] = 10;

stores 10 in the first element of the array.

Unlike a stack, an array can be accessed in any order.

The data of an array is stored sequentially in memory and can be accessed directly. The amount of memory used varies with the data type.

Sample Code.

The sample code was created in MS Visual C++ as a Windows Form Application. It contains a form (Form1) that displays when the program runs. The sample code, contained in the Form Load Event, executes and adds text to a RichTextBox named rtOut.

// Declare a one-dimension array of integers.
int sample[10];
rtOut->Text = "Declare one-dimension array: ";
rtOut->Text += "int sample[10]; \n\n";

// Counter.
int i = 0;

// Load array with 0 to 9.
// The value stored in the array is its index.
for (i; i < 10; i++) sample[i] = i;

// Show content.
// Note the counter i is reset to 0.
for (i = 0; i < 10; i++)
{
rtOut->Text += "The value of sample[" + i + "] is " + sample[i] + ".\n";
}

Output:

Declare one-dimension array: int sample[10];

The value of sample[0] is 0.
The value of sample[1] is 1.
The value of sample[2] is 2.
The value of sample[3] is 3.
The value of sample[4] is 4.
The value of sample[5] is 5.
The value of sample[6] is 6.
The value of sample[7] is 7.
The value of sample[8] is 8.
The value of sample[9] is 9.