You are on page 1of 16

Basic Data Structures

Chapter 5
5.1

BASIC ARRAYS AND STRINGS


Variables and Multiple Values
• A single variable is capable of storing only one
value at a time.
• For example, to perform computations on the
ages of four students, we would need four
variables to store the ages.
Array
• An array allows us to group
a number of values of the
same type into one large
unit.
• The individual values are
stored together in
consecutive memory
locations and they are
referred to as elements of
the array.
Defining Arrays
The size must be a constant of
• Syntax one of the integer types.
Once defined, the size is
type name[size]; fixed.
• The size specifies the maximum number of
elements that the array can hold
• Examples:
long populations[210];
float salaries[15];
char name[30];
More Examples ...
const int SIZE = 110;
int num_people = 100;

char letters[26];
double atomic_weights[SIZE];
float heights[num_people];
float areas[34.4];

Which of these array declarations are valid?


Accessing the Elements
• The entire array has only
one name.
• Each element is assigned
a number known as a
subscript or an array
index.
• Each element can be
assigned as an individual
variable through the
subscript.
Accessing the Elements, contd
• To access an element of the array, you use
Subscript numbering starts at
array_name[subscript] zero. The subscript of the last
element is one less than the total
• Example: number of elements in the array.

int age[4]; //an array to hold 4 integers


cout<<”Enter the ages of four students: “;

cin>>>>age[0]>>age[1]>>age[2]>>age[3];

cout<<”After two years”


<<”\nStudent 1 will be “<<age[0]+2
<<”\nStudent 2 will be “<<age[1]+2
<<”\nStudent 3 will be “<<age[2]+2
<<”\nStudent 4 will be “<<age[3]+2;
Arrays and for Loops
• Array elements are conveniently accessed
using for loops.
const int TOTAL = 100;
int age[TOTAL]; //an array to hold TOTAL integers
cout<<”Enter the ages of “<<TOTAL<<” students: “;

for (int i=0; i<TOTAL; ++i)


cin>>>>age[i];

cout<<”After two years”;


for (int j=0; j<TOTAL; ++j)
cout<<”\nStudent “<<j+1<<” will be “<<age[j]+2;
Note
• It is not possible to assign one array to
another or use an array in a expression
hoping to affect all its elements.
Initializing Arrays
• Syntax
type array_name[size] = initializer_list;
• The initializer list contains a set of comma
separated values within a pair of curly braces.
• Example
int age[4] = {25, 12, 15, 27};
/* 25 is stored in age[0], 12 is stored in age[1],
15 is stored in age[2], 27 is stored in age[3] */
Initializing Arrays, contd...
• C++ allows us to define an array without
specifying its size, as long as we provide an
initialization list.
• Example
double factors[] = {1.0, 1.5, 2.0, 2.5};
More Examples

int age[4] = {25, 12};


//age[0] = 25, age[1]=12, age[2]=0, age[3]=0

char name[] = {'B', 'i', 'r', 'u', 'k'};


double factors[4] = {1.0, 1.5, 2.0, 2.5, 3.0};
//ERROR!
Strings
• A string is a special array of characters. It is an
array of type char ending with a null character
('\0').
• Example
char greeting[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0'};
More on Strings
• Strings are special because
– We have an alternate form of initialization
using double quotes
char greeting[] = “Hello!”;
– Strings can be used with cin and cout like any
other variable.

You might also like