0% found this document useful (0 votes)
19 views19 pages

One Dimensional Arrays

This document provides an overview of one-dimensional arrays in computer programming, covering their definition, declaration, initialization, and methods for accessing and storing values. It explains the importance of arrays for managing related data efficiently, particularly in larger datasets, and includes examples of how to declare and manipulate arrays in C. Key concepts such as calculating addresses of elements, initializing arrays, and inputting values are also discussed.
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)
19 views19 pages

One Dimensional Arrays

This document provides an overview of one-dimensional arrays in computer programming, covering their definition, declaration, initialization, and methods for accessing and storing values. It explains the importance of arrays for managing related data efficiently, particularly in larger datasets, and includes examples of how to declare and manipulate arrays in C. Key concepts such as calculating addresses of elements, initializing arrays, and inputting values are also discussed.
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/ 19

EEE1202 COMPUTER

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

Consider a situation in which we have 10 students in a class and we have been


asked to write a program that reads and prints the marks of all the 10 students .
It would require 10 separate variables with different names and statements.
This approach may work for small data sets ,it becomes impractical for larger
data sets such as an entire course , college or university.to efficiently handle
large amounts of data, a structured approach is needed, which is where arrays
come in.
Definition

• 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.

Arrays are declared using the following syntax:


type name[size];
The type can be either int, float, double, char, or any other valid data type. The number within
brackets indicates the size of the array, i.e., the maximum number of elements that can be stored in the
array.

For example, if we write,


int marks[10];
then the statement declares marks to be an array containing 10 elements.
In C, the array index starts from zero. The first element will be stored in marks[0], second element
in marks[1], and So on. Therefore, the last element, that is the 10 th element, will be stored in
marks[9].
In the memory, the array will be stored as shown
ACCESSING THE ELEMENTS OF AN ARRAY

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.

The formula to perform this calculation is,


Address of data element, A[k] = BA(A) + w(k – lower_bound)
Here, A is the array, k is the index of the element of which we have to calculate the address, BA is
the base address of the array A, and w is the size of one element in memory, for example, size of
int is 2.
Example
Given an array int marks[ ]={99,67,78,56,88,90,34,85}, calculate the address of
marks[4] if the base address = 1000.

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 length of an array is given by the number of elements


stored in it. The general formula to calculate the length of an
array is
Length = upper_bound – lower_bound + 1
where upper_bound is the index of the last element and
lower_bound is the index of the first element in the array
STORING VALUES IN ARRAYS
When we declare an array, we are just allocating space for its elements; no
values are stored in the array. There are three ways to store values in an
array. First, to initialize the array elements during declaration; second, to
input values for individual elements from the keyboard; third, to assign
values to individual elements
Initializing Arrays during Declaration

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

Int i, arr1[1 ], arr2[1 ];


arr1[1 ]={ ,1,2,3,4,5,6,7,8,9};
for(i= ;i<1 ;i++)
arr2[i] = arr1[i];
the loop accesses each element of the first array
and simultaneously assigns its value to the corresponding
element of the second array. The index value i is incremented
to access the next element in succession. Therefore, when this
code is executed, arr2[0] = arr1[0], arr2[1] = arr1[1], arr2[2] .
= arr1[2], and so on

You might also like