0% found this document useful (0 votes)
33 views12 pages

Topic 6 Arrays

Arrays allow the storage of multiple values of the same type. An array is declared with the data type followed by square brackets and the variable name. Individual elements in an array are accessed using indexes that start from 0. Two-dimensional arrays store tabular data and elements are accessed using row and column indexes. Arrays can be initialized at declaration with initial values or dynamically sized based on user input. Arrays provide an efficient way to organize and process collections of related data.

Uploaded by

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

Topic 6 Arrays

Arrays allow the storage of multiple values of the same type. An array is declared with the data type followed by square brackets and the variable name. Individual elements in an array are accessed using indexes that start from 0. Two-dimensional arrays store tabular data and elements are accessed using row and column indexes. Arrays can be initialized at declaration with initial values or dynamically sized based on user input. Arrays provide an efficient way to organize and process collections of related data.

Uploaded by

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

TOPIC 6 : ARRAYS

1
Array
• An array is a collection of data values.
• If your program needs to deal with 100
integers, 500 Account objects, 365 real
numbers, etc., you will use an array.
• An array is also an indexed collection of
data values of the same type.

2
Arrays of Primitive Data Types
• Array Declaration

<data type> <variable>[ ]


• Examples
i. float marks[10];
ii. const int SIZE =50;
double number[SIZE];
iii. #define MAXSIZE 20;
char data[MAXSIZE];

3
Accessing Individual Elements
• Individual elements in an array accessed with the indexed
expression.

double rainfall[12];

rainfall
0 1 2 3 4 5 6 7 8 9 10 11

This
Thisindexed
indexedexpression
expression
refers
refers to the elementatat
to the element
The index of the first rainfall[2] position
position#2 #2

position in an array is 0.

4
Array Processing – Sample1
double rainfall[12];
The
Thepublic
publicconstant
constant
length
length returnsthe
returns the
double annualAverage, capacity
capacityofofan
anarray.
array.
sum = 0.0;

for (int i = 0; i < 12; i++) {

cout<<“Enter rainfall for each month”;


cin>>rainfall[i];
sum += rainfall[i];
}

annualAverage = sum / 12;

5
Array Initialization
• Like other data types, it is possible to
declare and initialize an array at the same
time.
int number[5] = { 2, 4, 6, 8, 10};
int measure [] = {0, 0, 0, 0};

double samplingData[] = { 2.443, 8.99, 12.3, 45.009, 18.2,


9.00, 3.123, 22.084, 18.08 };

char *monthName[] = { "January", "February", "March",


"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };

number 10 elements of int


samplingData 9 elements of double
monthName 12 elements of char
6
Variable-size Declaration
• In C++, we are not limited to fixed-size array
declaration.
• The following code prompts the user for the size
of an array and declares an array of designated
size:
int size;

int number[];
cout<<“Enter Size of an array”;
cin>>size
int number[size];

7
Two-Dimensional Arrays
• Two-dimensional arrays are useful in representing tabular information.

8
Declaring and Creating a 2-D Array
Declaration

<data type> [][] <variable> //variation 1


<data type> <variable>[][] //variation 2

Creation

<variable> = new <data type> [ <size1> ][ <size2> ]


payScaleTable
Example 0 1 2 3 4

double[][] payScaleTable;
0
payScaleTable 1
= new double[4][5]; 2
3

9
Accessing an Element
• An element in a two-dimensional array is
accessed by its row and column index.

10
Sample 2-D Array Processing
• Find the average of each row.

double[ ] average = { 0.0, 0.0, 0.0, 0.0 };

for (int i = 0; i < payScaleTable.length; i++) {

for (int j = 0; j < payScaleTable[i].length; j++) {

average[i] += payScaleTable[i][j];
}

average[i] = average[i] / payScaleTable[i].length;


}

11
Implementation of 2-D Arrays
• The sample array creation

payScaleTable = new double[4][5];

is really a shorthand for

payScaleTable = new double [4][ ];

payScaleTable[0] = new double [5];


payScaleTable[1] = new double [5];
payScaleTable[2] = new double [5];
payScaleTable[3] = new double [5];
12

You might also like