Print Friendly and PDF
An array allows you to store and work with multiple values of the same data type the variables you have worked with so far are designed to hold only one value at a time. Each of the variable denitions in below causes only enough memory to be reserved to hold one value of the specied data type. 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. Here is a denition of an array of integers:

CONCEPT:

An array allows you to store and work with multiple values of the same data type the variables you have worked with so far are designed to hold only one value at a time.
Each of the variable definitions in below causes only enough memory to be reserved to
hold one value of the specified data type.
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. Here is a denition of an
array of integers:

int days[6]
int count; Enough memory for 1 int
12314
float price;Enough memory for 1 float
56.981
char letter ; Enough memory for 1 char; A

The name of this array is days .The number inside the brackets is the array size declarator.

It indicates the number of elements or values , the array can hold . The array can store days ,
six elements, each one an integer. This is depicted in Figur


Element0 Element 1 Element 2 Element 3 Element4, Element5,

An array size declarator must be a constant integer expression with a value greater
than zero. It can be either a literal, as in the previous example, or a named constant, as
shown in the following

const int NUM-DAYS = 6;
int days[NUM-DAYS];
Arrays of any data type can be defined. The following are all valid array definitions

float temperatures[100]; // Array of 100 floats
string names[10]; // Array of 10 string objects
long units[50]; // Array of 50 long integers
double sizes[1200]; // Array of 1200 doubl

Memory Requirements of Arrays

The amount of memory used by an array depends on the arrays data type and the numbers of elements The hours arrays, defined here ,is an array of six shorts
short hours[6];
On a typical PC, a short uses two bytes of memory , so the hours array would occupy 12 hours,
bytes. This is shown in Fig




The size of an array can be calculated by multiplying the size of an individual element by
the number of elements in the array t
able shows the typical sizes of various arrays


Tables
Array Definition
Number of Elements
Size of each element
Size of the array
Char letters[25];
Short rings[100];
Int miles[84];
Float temp[12];
Double distance[1000];
25
100
84
12
1000
1 byte
2 byte
4 byte
4 byte
8 byte
25 bytes
200 bytes
336 bytes
48 bytes
8000 bytes

Accessing Array Elements

CONCEPT

The individual elements of an array are assigned unique subscripts. These subscript are used to access the 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
subscript. A subscript is used as an index to pinpoint a specific element within an array. The first element is assigned the subscript 0, the second element is assigned 1, and so forth The six elements in the array would have the subscripts 0 through 5.
shown in Figure


Inputting and Outputting Array Contents

Array elements may be used with the cout and cin objects like any other variable
being used to store and display values entered by hours Program below shows the array
the user
/ This program asks for the number of hours worked
// by six employees. It stores the values in an array. 

Example 

#include <iostream>
using namespace std;
int main()
{ 
7
const int NUM_EMPLOYEES = 6;
int hours[NUM_EMPLOYEES];

// Get the hours worked by each employee.
cout << "Enter the hours worked by " << NUM_EMPLOYEES << " employees: ";
cin >> hours[0];
cin >> hours[1];
cin >> hours[2];
cin >> hours[3];
cin >> hours[4];
cin >> hours[5];
// Display the values in the array.
cout << "The hours you entered are:";
cout << " " << hours[0];
cout << " " << hours[1];
cout << " " << hours[2];
cout << " " << hours[3];
cout << " " << hours[4];
cout << " " << hours[5] << endl;
return 0;
}
Output with Example Input
Enter the hours worked by 6 employees :20 12 40 30 30 15 [Enter]
The hours you entered are: 20 12 40 30 30 15

Even though the size declarator of an array denition must be a constant or a literal, sub-script numbers can be stored in variables.This makes it possible to use a loop to cycle through
an entire array, performing the same operation on each element. For example,
look at the following code:

const int ARRAY_SIZE = 5;
int numbers[ARRAY_SIZE];
for (int count = 0; count < ARRAY_SIZE; count++)
numbers[count] = 99

Example

include <iostream>
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 6; // Number of employees
int hours[NUM_EMPLOYEES]; // Each employee's hours
int count; // Loop counter

// Input the hours worked.
for (count = 0; count < NUM_EMPLOYEES; count++)
{
cout << "Enter the hours worked by employee "
<< (count + 1) << ": ";
cin >> hours[count];
}

// Display the contents of the array.
cout << "The hours you entered are:";
for (count = 0; count < NUM_EMPLOYEES; count++)
cout << " " << hours[count];
cout << endl;
return 0;
}

Program Output with Example Input Shown in Bold
Enter the hours worked by employee 1:20 [Enter]
Enter the hours worked by employee 2:12 [Enter]
Enter the hours worked by employee 3:40 [Enter]
Enter the hours worked by employee 4:30 [Enter]
Enter the hours worked by employee 5:30 [Enter]
Enter the hours worked by employee 6:15 [Enter]

The hours you entered are: 20 12 40 30 30 15






zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: