Array
❖ Introducing Arrays
❖ Declaring Array Variables, Creating Arrays,
❖ and Initializing Arrays
❖ Passing Arrays to Methods
❖ Search and Sorting Methods
❖ Copying Arrays
❖ Multidimensional Arrays
Introducing Arrays
• data structure that represents a collection of the
same types of data or homogeneous elements.
• Structures that related to data items
• It can contain one type of data either:
• Integer
• Floating point
• Numbers
• Characters
Differentiating arrays and lists.
An array is an indexed set of variables and it
store an item.
A list is a set of items.
dancer 1, dancer 2, dancer
3, ……
dancer[1], dancer[2], dancer
[3],……
Differentiating arrays and lists.
Difference between arrays and lists during
delete operation
Differentiating arrays and lists.
In a lists, the missing item is filled with the
next items and it filled when the places is
deleted/ empty.
Differentiating arrays and lists.
In an array, it left blank when the
item/variable is deleted
Declaring Array Variables
• datatype[] arrayname;
Example:
double[] myList;
• datatype arrayname[];
Example:
double myList[];
Creating Arrays
arrayName = new datatype[arraySize];
Example:
myList = new int[10];
myList[0] references the first element in the array.
myList[9] references the last element in the array.
Writing and Reading an array through
Loop
• Reading an array
for (i=0; i<=9; i++)
scanf(“%d”,&myList[]);
• Writing an array
for (i=0; i<=9; i++)
printf(“%d”,myList[]);
Writing and Reading an array through
Loop
• reading or writing two-dimensional array it would
require two loops. And similarly the array of a N
dimension would required N loops.
• common operation performed on array are:
✓Creation of an array
✓Traversing an array
✓Insertion of new element
✓Deletion of required element
✓Modification of an element
✓Merging of arrays
Array Initialization
Arrays may be initialized, but we need to give a list of values
Syntax:
Type Name[Size] =
{ value0, value1, value2, …, valueSize-1 };
value0 initializes Name[0], value1 initializes Name[1], etc.
values must be of appropriate type (though automatic casting will
occur)
Array Initialization
Example:
int numDays[12] = { 31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31 };
/* Jan is 0, Feb is 1, etc. */
int numDays[13] = { 0, 31, 28, 31, 30,
31, 30, 31, 31, 30, 31, 30, 31 };
/* Jan is 1, Feb is 2, */
Note:
if too few values provided, remaining array members not initialized
if too many values provided, a syntax error or warning may occur (extra values
ignored)
Declaring, creating, initializing Using
the Shorthand Notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand notation is equivalent to the following
statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
CAUTION
Using the shorthand notation, you have to
declare, create, and initialize the array all in
one statement. Splitting it would cause a
syntax error. For example, the following is
wrong:
double[] myList;
myList = {1.9, 2.9, 3.4, 3.5};
Accessing Array Elements
• Requires
• array name,
• subscript labeling individual element
• Accessed using an integer index
• Start with 0 until the size of array is -1
• Syntax: name[subscript]
• Example
myList[3] refers to the sales totals for employee 3
myList[3] can be treated like any float variable:
• myList[3] = 3.5;
printf(“My List is : $%7.2f\n”, myList[3]);
Sales Data
Example of Sales Data:
Employee # Sale
---------- ----
3 9.99
7 16.29
9 7.99
3 2.59
.
.
.
7 49.00
Idea: declare Sales as array, update array items
Updating Sales Data
while
(fscanf(sdata,”%d%f”,&numE,&amtS) ==
2) {
switch (numE) {
case 0: sales[0] += amtS; break;
case 1: sales[1] += amtS; break;
case 2: sales[2] += amtS; break;
/* cases 3-8 */
case 9: sales[9] += amtS; break;
}
}
Updating with Arrays
while (fscanf(sdata,”%d%f”,&numE,&amtS) == 2)
{
sales[numE] += amtS;
}
When referencing array element can use any expression producing
integer as subscript
[ ] is an operator, evaluates subscript expression then the appropriate
location from the array is found
Note, when we have an integer expression, we may want to check
subscript before using:
if ((numE >= 0) && (numE <= 9))
sales[numE] += amtS;
else
/* Problem employee # */
Using Array Elements
Array elements can be used like any variable
read into:
printf(“Sales for employee 3: “);
scanf(“%f”,&(sales[3]));
printed:
printf(“Sales for employee 3
$%7.2f\n”,sales[3]);
used in other expressions:
total = sales[0] + sales[1] + sales[2] +
…;
Arrays and Loops
• Problem: initialize Sales with zeros
sales[0] = 0.0;
sales[1] = 0.0;
…
sales[9] = 0.0;
• Should be done with a loop:
for (i = 0; i < 10; i++)
sales[i] = 0.0;
Processing All Elements of
Array
• Process all elements of array A using for:
/* Setup steps */
for (i = 0; i < arraySize; i++)
process A[I]
/* Clean up steps */
• Notes
i initialized to 0
Terminate when i reaches arraySize
Exercise Tracing the Output
Exercise Tracing the Output
Thank You