Ch4 Arrays Lec1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

BASIC DATA STRUCTURES

BASIC ARRAYS
Variables and Multiple Values
• A variable is capable of storing only one value
at a time.
• Each of the variable definitions below cause
only enough memory to be reserved to hold
one value of the specified data type.
Example

 Referring to the previous example, what if we want to


store multiple values of price at the same time like
56.981,23.124 and 90.157
 We would need to declare three variables and store
the values in those variables
 i.e. double price1 = 56.981;
double price2 = 23.124;
double price3 = 90.157
 An array works like a variable that can store a group of values, all of the
same type.
 The values are stored together in consecutive memory locations.
 Defining arrays
• Syntax
type name[size declarator];
 The size declarator specifies the number of elements or values that the
array can hold

The size must be a constant of one


of the integer types with a value
greater than zero. Once defined, the
size is fixed.
Example
double prices[3];

The prices array can store three elements or values, each


one a double type.
Prices array: Enough memory to hold three double values

Element 0 Element 1 Element 2


 The size declarator can be either a literal, as in the previous example, or a
named constant, as shown here:
 const int SIZE = 3;
 double prices[SIZE];
Memory Requirements of Arrays
 The amount of memory used by an array depends on the array’s data type
and the number of elements.
 E.g short age[6];

 A short uses 2 bytes of memory, so the age array would occupy 12 bytes.
Accessing Array Elements
 Even though an entire array has only one name, the elements may be
accessed and used as individual variables.
 This is possible because each element is assigned a number known as a
subscript
 . A subscript is used as an index to pinpoint a specific element within an
array.
 E.g See the subscripts below in an array of size 3

 Subscript numbering in C++ always starts at zero.


 The subscript of the last element in an array is one less than
the total number of elements in the array.
 Each element in the prices array above, when accessed by its subscript, can
be used as a double variable.

prices[0]= 56.981;
Note: The expression prices[0] is pronounced “prices sub zero”
e.g. prices sub 1 is assigned 23.124
prices[0] prices[1] prices[2]

56.981 ? ?

 Note: If an array holding numeric values is defined globally,


all of its elements are initialized to zero by default.
 Array elements may receive values with assignment
statements just like other variables.
 However, entire arrays may not receive values for all
their elements at once.
Inputting and Displaying Array Contents
 Array elements may also have information read into them using the cin
object and have their values displayed with the cout object, just like regular
variables, as long as it is done one element at a time.
Arrays and for Loops
 Array elements are conveniently accessed using for loops.
No Bounds Checking in C++

 C++ does not perform array bounds checking.


 This means you can write programs with subscripts that go
beyond the boundaries of a particular array.
 This could cause the program to crash and, under some
operating systems, could even cause the computer to lock
up.
 Example
// This program unsafely accesses an area of memory by writing
// values beyond an array's boundary. WARNING: If you compile
// and run this program, it may crash.
#include <iostream>
using namespace std;
int main ()
{
const int SIZE = 3;
int values[SIZE]; // An array of 3 integers
int count;
cout << "I will store 5 numbers in a 3-element array!\n";
for (count = 0; count < 5; count++)
values[count] = 100;
cout << "If you see this message, it means the computer\n";
cout << "has not crashed! Here are the numbers:\n";
for (count = 0; count < 5; count++)
cout << values[count] << endl;
return 0;
}
Array Initialization
 Sometimes it is more appropriate to set variable values within a program
than to input them.
 However, writing separate assignment statements for the individual
elements of an array can mean a lot of typing, especially for large arrays.
 Example
Initializing Arrays

 Fortunately, C++ allows you to initialize arrays when


you define them. By using an initialization list, all the
elements of the array can be easily initialized when
the array is created.
• Syntax
type array_name[size] = initializer_list;
• The initializer list contains a set of comma separated
values within a pair of curly braces.

 These values are stored in the array elements in the order they
appear in the list. (The first value, 31, is stored in days[0], the
second value, 28, is stored in days[1], and so forth).
Partial Array Initialization
 An initialization list cannot have more values than the array has elements, but
it may have fewer values than there are elements.
 That is, C++ does not require a value for every element. It’s possible to only
initialize part of an array, such as
int numbers[7] = {1, 2, 4, 8};
Implicit Array Sizing
 It’s possible to define an array without specifying its size, as long as you
provide an initialization list that includes a value for every element.
 C++ automatically makes the array large enough to hold all the
initialization values.
 For example, the following definition creates an array with five elements:
double ratings[] = {1.0, 1.5, 2.0, 2.5, 3.0};

You might also like