0% found this document useful (0 votes)
66 views18 pages

Chapter 8 - Arrays

This document introduces arrays as an efficient way to store multiple values of the same type. Arrays allow values to be stored in adjacent memory locations. Arrays must be declared with a type, name, and size. Values can be initialized individually or sequentially. Multidimensional arrays can store values in a table structure accessed by index pairs. Loops are useful for sequentially accessing all elements of an array. Arrays provide a simpler alternative to declaring many individual variables when multiple values of the same type need to be stored.

Uploaded by

Ariff Salleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views18 pages

Chapter 8 - Arrays

This document introduces arrays as an efficient way to store multiple values of the same type. Arrays allow values to be stored in adjacent memory locations. Arrays must be declared with a type, name, and size. Values can be initialized individually or sequentially. Multidimensional arrays can store values in a table structure accessed by index pairs. Loops are useful for sequentially accessing all elements of an array. Arrays provide a simpler alternative to declaring many individual variables when multiple values of the same type need to be stored.

Uploaded by

Ariff Salleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CHAPTER 8: ARRAY

Prepared by Norhamreeza Abdul Hamid


TOO MANY VARIABLES
How if I want to create an
application to track student scores
in a class with 50 students?
Creating 50 variables will prove
inefficient…

How would I
name the
variable??
INTRODUCING ARRAY

An array might prove


a better solution to
your problem!
ONE DIMENSIONAL ARRAY
ARRAY HOLD MULTIPLE VALUE

Array is variable that can store multiple values of the same


type (e.g: int, float, double, char)

ARRAY Values are stored in adjacent memory locations

Declared using [] operator


int tests[5];
ARRAY DECLARATION
Must declare both the name of the array and the
number of cells associated with it
The declaration:
data_type array_name [expression]
• Expression: size of array
• Array name : name of array that are an identifier
The items in an array start with index 0
ARRAY DECLARATION (cont..)

name of the array

int tests[5];

size declarator. It shows


the data type of the
the number of elements
array elements
in the array.
ARRAY DECLARATION (cont..)
ARRAY INITIALIZATION
Array of 10 un-initialized integers
int A[10];
Initialize array
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31}
Elements for array months:
months[0]=31
months[1]=28
months[2]=31
months[3]=30
If the size of declared array is not given and not initialized to any
value, the size of array will be automatically set to the length of the
list
ARRAY INITIALIZATION (cont..)
Example
A[3] = 1;
int x = A[3]; --

1 --

-- -- --

0 1 2 3 4 5 6 7 8 9
A -- -- -- 1 -- -- -- -- -- --
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
ARRAY INITIALIZATION (cont..)
Using Loops for Sequential Access
Looping statement
can be used in array
for sequential
process
Array Square
Using the loop
counter as an array [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

index, gives access 0 1 4 9 16 25 36 49 64 81 100

to each array
element in turn
Example:
int square[SIZE], I;
The for loop
int square[SIZE], i;
for (i=0;i<SIZE; ++i)
square [i] = i * I;
EXAMPLE: Average Value
/*This program is to read 10 numbers and get an
arrays*/

#include<iostream>
using namespace std;
int main()
{
int x[10],total = 0,i;

for (i = 0;i < 10;i++)


{
cout << "Insert number : ";
cin >>x[i];
total+=x[i];
}
cout << "TOTAL = " << total << endl;
cout << "AVERAGE = " << total/10 << endl;
system("pause");
return (0);
}
TWO DIMENSIONAL ARRAY
2-D ARRAY EXAMPLE

int table[5][4];

1st Dimension
2nd Dimension

Allocate
Row:0-4

Allocate
Column:0-3
TWO DIMENSIONAL ARRAY
INITIALIZATION
The right index of an array is
X[0][0] 1
increased first before the left index X[0][1] 2
Example: X[0][2] 3

int x[10][5] X[0][3] 4


X[1][0] 5
/*allocate 50 spaces (row:0-9, and
X[1][1] 6
column:0-4*/
X[1][2] 7
float a[3][20] 8
X[1][3]
/*allocate 60 spaces (row:0-2, and X[2][0] 9
column:0-19*/ X[2][1] 10
Example: X[2][2] 11

X[2][3] 12
int x[3]
[4]={1,2,3,4,5,6,7,8,9,10,11,12}
EXAMPLE
#include <iostream>
using namespace std;

main()
{
float b[3][4]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.10,11.1,12.12};
int i,j;
for (i=0;i<3;i++)
for(j=0;j<4;j++)
{
cout << "[" << i << "] [" << j << "] = " << b[i][j] << endl;
}
system("pause"); [0] [0] = 1.1
return (0); [0] [1] = 2.2
}
Output [0] [2] = 3.3
[0] [3] = 4.4
[1] [0] = 5.5
[1] [1] = 6.6
[1] [2] = 7.7
[1] [3] = 8.8
[2] [0] = 9.9
[2] [1] = 10.1
[2] [2] = 11.1
[2] [3] = 12.12

You might also like