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

Arrays

Computer programming 1

Uploaded by

briangitonga133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Arrays

Computer programming 1

Uploaded by

briangitonga133
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Arrays

Arrays
● An array is a collection of similar data objects (homogeneous data items) or
collection of similar entity stored in contiguous memory location. E.g. A collection
of characters is a string array.
● An array is used to store a collection of data, but it is often more useful to think of
an array as a collection of variables of the same type.
● Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables.
● Each data item of an array is called an element. A specific element in an array is
accessed by an index.
Characteristics of an Array
An array has the following characteristics;

● Items share a name(identifier)


● Items can be of any simple data type e.g. char, float, int, double. (All the items in an
array should be of the same data type)
● Individual elements are accessed using an integer index whose value ranges from 0
to the value of the array size.
● Array variable can store more than one value at a time where other variable can
store one value at a time.
Declaring a 1 Dimension Array
● An array definition comprises;
○ Data type
○ Array name
○ Array size expression (usually a positive integer or symbolic constant). This is enclosed in square
brackets.
● Syntax:
○ data_type array_name[Array size];
● Examples :
○ int a[10]; - an array a, that can store 10 integers
○ float b[5];- Here, we declared an array, b, of floating-point type. And its size is 5. Meaning, it can hold
5 floating-point values.
○ char c[25];
Initializing Arrays
● There are two ways in which arrays can be initialized
○ at compile time

○ and at run time.

● Array initialization at compile time means initializing the array’s elements with
known values at the time of writing the code, rather than during runtime.
○ int arr1[5] = {1, 2, 3, 4, 5}; // Initializing an array of integers

○ char str[] = "Hello"; // Initializing an array of characters (string)

○ float arr2[] = {1.1, 2.2, 3.3, 4.4, 5.5}; // Initializing an array of floats
Run Time Initialization
● Runtime initialization refers to initializing an array during the execution of the
program, as opposed to at compile time. This typically involves assigning values to
the array elements using variables or expressions whose values are determined
during runtime. Here's an example in C:
Accessing Values of an Array
You can access elements of an array by indices.
● Suppose you declared an array int mark[5] .
● The first element is mark[0], the second element is mark[1] and so on.

Keynotes:
● Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
● If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
● Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d.
Similarly, the address of mark[2] will be 2128d and so on.
● This is because the size of a float is 4 bytes.
Change Value of Array elements
● Suppose we declare an array as follows: int mark[5] = {19,
10, 8, 17, 9}
● Make the value of the third element to -1
○ mark[2] = -1;
● make the value of the fifth element to 0
○ mark[4] = 0;
Example 1: Array Input/Output
Here, we have used a for loop to
take 5 inputs from the user and
store them in an array. Then, using
another for loop, these elements are
displayed on the screen.
Example 2: Calculate Average

Here, we have computed the


average of n numbers entered by
the user.
C Multidimensional Arrays
● In C programming, you can create an array of arrays. These arrays are known as
multidimensional arrays. For example,
○ float x[3][4];

● Here, x is a two-dimensional (2d) array. The array can hold 12 elements. You can
think the array as a table with 3 rows and each row has 4 columns.
C Multidimensional Arrays

● Similarly, you can declare a three-dimensional (3d) array. For


example,
○ float y[2][4][3];
● Here, the array y can hold 24 elements.
Initializing a two-dimensional array

● Here is how you can initialize two-dimensional and three-dimensional arrays:


Initializing a three-dimensional array

● You can initialize a three-dimensional array in a similar way


like a two-dimensional array. Here's an example,
Example 1: Two-dimensional array to store and
print values

You might also like