0% found this document useful (0 votes)
6 views

Unit-1

This document provides an overview of arrays, which are data structures that store a fixed number of items of the same data type in contiguous memory. It explains key concepts such as array declaration, initialization, and accessing elements using indices and subscripts. Additionally, it highlights the use of loops for inputting values into arrays.

Uploaded by

Lance Andrei
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)
6 views

Unit-1

This document provides an overview of arrays, which are data structures that store a fixed number of items of the same data type in contiguous memory. It explains key concepts such as array declaration, initialization, and accessing elements using indices and subscripts. Additionally, it highlights the use of loops for inputting values into arrays.

Uploaded by

Lance Andrei
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/ 10

Unit 1: Working with

Arrays
CONCEPT
• An array allows you to store a group of items of the same data type
together in memory. Processing a large number of items in an array is usually
easier than processing a large number of items stored in separate variables.
Arrays are one of the simplest and most widely used data structures in
computer programs. Arrays in any programming language all share a few
common properties:
 The contents of an array are stored in contiguous memory. It can be
individually referenced by adding an index to a unique identifier.
 All of the elements of an array must be of the same type; hence, arrays are
referred to as homogeneous data structures.
 Array elements can be directly accessed.
What is an array?
• An array is a fixed number of data items that are all of the same type. The data items in an
array are referred to as elements.
• An array is a special type of variable that can contain or hold one or more values of the same
data type with reference to only one variable name.
a normal variable: an an array variable:
int x; int array[] = new int[4];
Array elements and subscripts
• An array can be distinguished through a pair of square brackets and a specified number inside the
square brackets [ ].
• The number inside the square brackets is called an index.
• Each element is assigned a unique number known as subscripts.
• Subscripts are used to identify specific elements in an array
• In most languages, the first element is assigned the subscripts 0, the second element is assigned the
subscript 1, and so forth.
• Because subscript numbering starts at zero, the subscript of the last element in an array is one less than
the total number of elements in the array.
Declaration and definition
• The datatype declares the base type of the array, which is the type of each element in the array.
• arrayname is the identifier or variable name and arraysize defines how many elements the array will
hold [n-1].
data type arrayname[] = new data type[arraysize];
• Therefore, in order to declare an array named x, it is simple as:
int x[] = new int[5];
Note: In a fixed-length array, the size of the array is constant and must have a value at
compilation time
Storing values in array
• Declaration and definition only reserve space for the elements in the array. If we want to
store values in the array, we must initialize the elements, read values from the keyboard, or
assign values to each individual element
Initialization
• An array can be initialized when it is declared when initializing the array, the value for their various
indexed variables are enclosed in braces and separated by commas.

int num[]={10,20,30,40,50};
• num[4] 50
• num[3] 40
• num[2] 30
• num[1] 20
• num[0] 10
Cont..
• The example above is a simple declaration of five integers and is typical of the way array initialization is
coded.
• For each element in the array we provide a value. We can assign values to individual elements using the
assignment operator
• The declaration above is equivalent to the following:
num[4]=50
num[3]=40
num[2]=30
num[1]=20
num[0]=10
The above code fragment assigns the value 10 to index 0, the value 20 to index 1, and so forth.
Initialization without size
int number[]=new int[]={3,7,8,5,5};
Inputting values
• When the array is to be completely filled, the most appropriate loop is the for loop because the number
of elements is fixed and known.

You might also like