One Dimensional Arrays
One Dimensional Arrays
ENGINEERING
PROGRAMMING
One dimensional arrays
defining arrays
Accessing elements of an array
Storing values in an array
Nyasha Muradzikwa H240416Z
Latifa Mufugami H240271Q
Nobukhosi Muleya H240588R
Mukanya Decibel H240096J
Mudawarima Kennedy H240461W
Learning Objective
In this ppt, we will discuss arrays. An array is a user-
defined data type that stores related information
together. All the information stored in an array belongs
to the same data type. So, in this ppt, we will learn how
arrays are defined, declared, initialized, and accessed.
Introduction
• An array is a collection of similar data elements. These data elements have the
same data type.
The elements of the array are stored in consecutive memory locations and are
referenced by an index (also known as the subscript). The subscript is an ordinal
number which is used to identify an element of the array.
DECLARATION OF ARRAYS
An array must be declared before being used. Declaring an array means specifying the following
Data type—the kind of values it can store, for example, int, char, float, double.
Name—to identify the array.
Size—the maximum number of values that the array can hold.
Storing related data items in a single array enables the programmers to develop
concise and efficient programs. But there is no single function that can operate
on all the elements of an array.
• To access all the elements, we must use a loop. That is, we can access all the
elements of an array by varying the value of the subscript into the array. But
note that the subscript must be an integral value or an expression that
evaluates to an integral value
Calculating the Address of Array Elements
When we use the array name, we are actually referring That is, with just the array name and the
index, C can calculate the address of any element in the array.
Since an array stores all its data elements in consecutive memory locations, storing just the base
address, that is the address of the first element in the array, is sufficient. The address of other data
elements can simply be calculated using the base address. to the first byte of the array.
The subscript or the index represents the offset from the beginning of the array to the element being
referenced.
Sol
We know that storing an integer value requires 2 bytes, therefore, its size is 2 bytes.
marks[4] = 1000 + 2(4 – 0)
= 1000 + 2(4) = 1008
Calculating the Length of an Array
The elements of an array can be initialized at the time of declaration, just as any
other variable.
When an array is initialized, we need to provide a value for every element in the
array. Arrays are initialized by writing,
type array_name[size]={list of values};
Note that the values are written within curly brackets and every value is separated
by a comma. It is a compiler error to specify more values than there are elements in
the array. When we write,
int marks[5]={90, 82, 78, 95, 88};
An array with the name marks is declared that has enough space to store five
elements. The first element, that is, marks[0] is assigned value 90. Similarly, the
second element of the array, that is marks[1], is assigned 82, and so on.
While initializing the array at the time of declaration, the programmer may omit the
size of the array. For example,
int marks[]= {98, 97, 90};
The above statement is absolutely legal. Here, the compiler will allocate enough space
for all the initialized elements. Note that if the number of values provided is less than
the number of elements in the array, the un-assigned elements are filled with zeros.
Inputting Values from the Keyboard
An array can be initialized by inputting values from the keyboard. In this method, a
while/do–while or a for loop is executed to input the value for each element of the array.
Int i, marks[1 ];
for(i= ;i<1 ;i++)
scanf(“%d”, &marks[i]);
In the code, we start at the index i at 0 and input the value for the first element of the
array. Since the array has 10 elements, we must input values for elements whose index
varies from 0 to 9.
Assigning Values to Individual Elements
The third way is to assign values to individual elements of the array by using the
assignment operator. Any value that evaluates to the data type as that of the array
can be assigned to the individual array element. A simple assignment statement can
be written as
marks[3] = 100;
Here, 100 is assigned to the fourth element of the array which is specified as
marks[3]
Note that we cannot assign one array to another array, even
if the two arrays have the same type and size. To copy an array,
you must copy the value of every element of the first array into
the elements of the second array