











































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












































An array is a collection of data elements of same typein contiguous memory e.g. list of names, list of scores
-^
Easier way to compare and use data than havingseparate variables for data elements
-^
data_type
identifier[
size
int arr[3]
arr
with space
for 3 integer variables
Variables contiguous in memory; lowest address hasfirst value, next address has second variable value,and so on
An array is an indexed data structure
-^
An array stores a collection of variables
-^
All variables stored in an array are of the samedata type
-^
An individual variable within an array is called an element
of the array
An element of an array is accessed using the arrayname and an index
-^
The name of the array is the address of the firstelement. The index is the offset
size
variables
size
must be a constant
double taxrate[3]
= {0.15, 0.25, 0.3};
char word[]
= “hello”;
//word has size 6
char list[5]
= {‘h’,’e’,’l’,’l’,’o’};
//list of characters, not a string
double vector[100] = {0.0};
//assigns zero to all 100 elements
Initializing Arrays
-^
for
loops used to initialize arrays
#include
int
main() {
int
t,
sample[10];
//
this
reserves
10
integer
elements//
load
the
array
for(t=0;
t<10;
++t)
sample[t]=t; //
display
the
array
for(t=0;
t<10;
++t)
cout
<<
sample[t]
<<
'
';
return
0;
}
Initializing Arrays
int a[5];
-^
Now an indexed variable like
a[1]
can be used anywhere that a variable of type
int
can be used
int a[5];cin >> a[4] >> a[2];cout << a[2] << “ “ << a[4] << endl;cin >> a[3] >> a[0];int max; max = a[3] + a[0];
cout << max << endl;
int next;
cin >> next;
a[1]= next; cout << a[1] << endl;a[3]= 42;
cout << a[3] << endl;
int student = 2;a[student]
= 99;
cout << a[student] << “ “ << a[2] << endl;
-^
Also:
a[get_index())]
=
3;
if
get_index()
returns an
int
Initializing Arrays
Useful to give a
constant
name to the
size
of array
/*
const
type modifier*/
const int NUM_EMPLOYEES = 10;int salaries[NUM_EMPLOYEES];for (int i=0; i < NUM_EMPLOYEES; i++)
cin >> salaries[i];
Advantage? Why not use a variable name?
-^
Another way:
And yes,
sizeof(salaries);
gives what?
Total size of an array in bytes?
-^
Total bytes =
number of bytes in type * number of elements
e.g. int sample[10];
-^
Has size in bytes = 10 * sizeof(int)
Assigning One Array to Another
Assigning One Array to Another
-^
int a[5], b[5];// ...a = b; // error – illegal
-^
Cannot assign one array to another; each element must becopied individually
const
int
ARRAY_SIZE
=
5;
int
a[]
=
{10,
11,
12,
13,
14};
int
main() {
int
b[ARRAY_SIZE]; for
(int
i=0;
i
<
ARRAY_SIZE;
++i)
b[i]
=
a[i];
return
0;
}
In C++ all arrays are stored in contiguous memorylocations
-^
An individual element is accessed by use of an index.
-^
An index describes the position of an element withinan array.
-^
In C++ all arrays have zero as the index of their firstelement.
int main(){
const int SIZE = 10;int i, min_value, max_value;int list[SIZE];for(i=0; i