Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
299 views

C++ - Array PDF

1) An array is a variable that stores multiple values of the same data type in consecutive memory locations. 2) To declare an array, specify the data type followed by the array name and size in square brackets, such as int testScores[5]. 3) The size must be a constant value determined at compile time, such as a literal value or constant, not a variable. This reserves the correct amount of memory for the array.

Uploaded by

Benedict David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
299 views

C++ - Array PDF

1) An array is a variable that stores multiple values of the same data type in consecutive memory locations. 2) To declare an array, specify the data type followed by the array name and size in square brackets, such as int testScores[5]. 3) The size must be a constant value determined at compile time, such as a literal value or constant, not a variable. This reserves the correct amount of memory for the array.

Uploaded by

Benedict David
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2/9/2011

INTRODUCTION
• An array enables the programmer to use a
single variable to store many values.
• The values are stored at consecutive indexes,
starting with zero and then incrementing by
one for each additional element of the array.
• Using 1 array variable to store many values
has many advantages over having to declare
separate variables that can hold only 1 value
each.

INTRODUCTION DECLARING AN ARRAY


– In addition to being a lot less code to write, it is far • An array is a variable.
easier to keep track of 1 variable than many – Like other variables, an array must be declared before
variables. it can be used.
– Furthermore and more important, programmer • The syntax for declaring an array is almost
can use a loop to access each consecutive element identical to the syntax for declaring integers,
in an array, whereas this is not possible with three characters, or other variables.
int testScore; → integer variable
separate variables. int testScore[3]; → integer array variable
float GPA[5];
char grades[7];
string names[6];

1
2/9/2011

DECLARING AN ARRAY DECLARING AN ARRAY


• While an array may be one of several data • The only difference between declaring a variable
types, all the values in a particular array must that holds a single value and an array is that,
be of the same data type. when declaring an array, the variable name is
– Cannot have an array in which some elements are followed by a number within square brackets.
floats, others are strings, still others are integers, – That number is the array’s size indicator.
and so on. Note: There is one exception to the necessity of having a size declarator. The
• The declaration of both a single variable and square brackets may be empty if you initialize the array when you declare it.

an array of variables begins with the data type • The purpose of the size declarator is to tell the
followed by a variable name and ending with a computer how much memory to reserve.
semicolon.

DECLARING AN ARRAY DECLARING AN ARRAY


– The size declarator, combined with the data type • Constants
of the array, determines how much memory to – Each of the size declarators used was a literal.
reserve.
Tip: Give careful consideration to the number of elements in an array before
– A literal is a value that is written exactly as it is
you declare the array since you can’t resize an array in the middle of a meant to be interpreted.
program in the event the array is too small or unnecessarily large. #include <iostream.h>
int main()
{
int testScore[3];
return 0;
}

2
2/9/2011

DECLARING AN ARRAY DECLARING AN ARRAY


– The size declarator may not be a variable. Note: It is possible to declare the size of an array with a variable if you use a
different array declaration technique, dynamic memory allocation.
#include <iostream.h>
int main()
– A constant is a name that represents the same
{ value throughout a program.
int numTests; • A constant is the converse of a variable, while a variable
cout << “Enter the number of test scores:”; is a name that may represent different values during
cin >> numTests; the execution of a program. However, the value of a
int testScore[numTests}; constant cannot change during the execution of a
return 0; program.
} Note: While neither a literal nor a constant changes its value during the
• The result is a compiler error. The compiler will flag the execution of a program, they are not the same. While a constant is a name
that represents a value, a literal is not a name, but instead the value itself.
declaration of the array and complain that a constant
expression was expected.

DECLARING AN ARRAY DECLARING AN ARRAY


– May use a constant instead of a literal as a size • Each requires a data type and a variable name and ends
declarator. in a semicolon. However, there are two differences.
#include <iostream.h> – First, the declaration of a constant must begin
main() with the const keyword. This tells the compiler
{ that you are declaring a constant instead of a
const int numTests = 3; variable.
int testScore[numTests];
return 0; – Second, the declaration terminates by assigning
} the constant a value.
– The syntax for declaring a constant is similar to, • Assigning a variable a value when you declare it is
but not the same as, a syntax for declaring a optional. On the other hand, assigning a constant a
value when declaring it is mandatory.
variable.

3
2/9/2011

DECLARING AN ARRAY DECLARING AN ARRAY


• The declaration of the constant will not compile if you #include <iostream.h>
don’t assign a value. The compiler error being that a main()
constant object must be initialized. {
• You cannot assign a value of a constant after you const int numTests = 3;
declare it, the only time you can assign a value to a cout << “Enter the number of test scores:”;
constant is when you declare it. cin >> numTests;
int testScore[numTests];
Note: The declaration of a constant does reserve memory just as does the
declaration of a variable. The difference is that with a constant the value return 0;
stored at the memory address cannot change during the life of the }
program. • The result is a compiler error. The compiler will flag the
#include <iostream.h> int testScore[numTests]; attempt to assign a value to the constant and complain
main() return 0; that the stream extraction operator >> cannot have a
{ } right-hand operand that is a constant.
const int numTests = 3;

DECLARING AN ARRAY DECLARING AN ARRAY


#include <iostream.h> – Why use a constant instead of a literal array to
main() declare the size of an array?
{ const int numTests = 3;
• The reason is that in the code you may need to often
int num;
refer to the size of the array, not only when declaring it,
cout << “Enter the number of test scores:”;
but also when assigning values to, and displaying them
cin >> num;
from, the array.
numTest = num;
int testScore[numTests]; – However, the needs of the program may require
return 0; to modify the code to change the size of the array,
} usually to make it larger.
• The result is a compiler error. The compiler will flag the • Use a constant to change the value in the code in just
attempt to assign a value to the constant; the error one place.
message will be that “l-value specifies const object”.
The l in “l-value” refers to the value to the left of the
– Constant have many uses in addition to signifying
assignment operator. the size of an array.

4
2/9/2011

DECLARING AN ARRAY DECLARING AN ARRAY


• Array Index – The fact that the first index in an array is 0 instead
– The entire array has only one name. of 1 is because of an offset.
• You need to be able to refer to individual elements of – An offset refers to a value added to a base address
the array. to produce a second address.
Index 0 1 2
– Can refer to the individual elements of the array
Offset 0*4 = 0 1*4 = 4 2*4 = 8
by their position within the array. This position is Address 101 105 109
referred to as an index or subscript. – The base address of an array is the address where
– The first index in an array is always 0. the array begins.
– The last index in an array is always 1 less than the – The address of the first element of the array is the
number of elements in the array. same as the base address of the array itself.
• There are no exceptions.

DECLARING AN ARRAY INITIALIZATION


• Therefore, the value that would be added to the base • Is when a value is assigned to a variable in the
address of the array to obtain the address of the first
element of the array is 0, which is the index of the first same statement in which the variable is
element of the array. declared.
– The address of any element of the array is the • Assignment on the other hand is assigning a
base address of the array plus the offset, and in value to a variable in a statement after the
turn the offset is determined by the index of the declaration statement.
array multiplied by the size of the array’s data
type. • Two alternative methods in initializing an
– Since the first index in an array must always be 0, array:
the last index in an array must always be 1 less
than the number of elements in the array.

5
2/9/2011

INITIALIZATION INITIALIZATION
– Explicit array sizing • Explicit Array Sizing
• In which the square brackets contains a numerical int testScore[3] = {74, 87,92};
constant that explicitly specifies the size of the array. float milesPerGallon[4] = {44.4, 22.3, 11.6, 33.3};
– Implicit array sizing char grades[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’};
string days[7] = {“Sunday”, “Monday”, “Tuesday”, “Wednesday”,
• In which the square brackets are empty and the size of
“Thursday”, “Friday”, “Saturday”};
the array is indicated implicitly by the number of
elements on the right side of the assignment operator.

You might also like