0% found this document useful (0 votes)
26 views20 pages

Unit 2 (B)

The document provides an overview of arrays in programming, explaining their purpose as a collection of elements of the same type that can store multiple values in a single variable. It covers one-dimensional (1D) and two-dimensional (2D) arrays, including their declaration, initialization, accessing elements, and examples of operations such as calculating averages and finding minimum values. Additionally, it discusses memory allocation for arrays and the importance of contiguous memory locations.

Uploaded by

repaer072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views20 pages

Unit 2 (B)

The document provides an overview of arrays in programming, explaining their purpose as a collection of elements of the same type that can store multiple values in a single variable. It covers one-dimensional (1D) and two-dimensional (2D) arrays, including their declaration, initialization, accessing elements, and examples of operations such as calculating averages and finding minimum values. Additionally, it discusses memory allocation for arrays and the importance of contiguous memory locations.

Uploaded by

repaer072
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

ARRAYS

1D, 2D

1/25
 An array is a collection of elements of the same type that
are referenced by a common name.
 Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for each
value.
 Compared to the basic data type (int, float & char) it
is an aggregate or derived data type.
 All the elements of an array occupy a set of contiguous
memory locations.
 Why need to use array type?
 Consider the following issue:
"We have a list of 1000 students' marks of an
integer type. If using the basic data type (int),
we will declare something like the following…"

int studMark0, studMark1, studMark2, ..., studMark999;

2/25
 Can you imagine how long we have to write
the declaration part by using normal variable
declaration?

int main(void)
{
int studMark1, studMark2, studMark3,
studMark4, …, …, studMark998, stuMark999,
studMark1000;


return 0;
}

3/25
 By using an array, we just declare like this,

int studMark[1000];

 This will reserve 1000 contiguous memory locations for storing the
students’ marks.
 Graphically, this can be depicted as in the following figure.

4/25
Access the Elements of an Array

• Array indexes start with 0: [0] is the first element. [1] is the second
element, etc.
• This statement accesses the value of the first element
[0] in myNumbers:

Examples

• int myNumbers[4] = {25, 50, 75, 100};


printf("%d", myNumbers[0]);

// Outputs 25

5/25
Change the Elements of an Array

Examples

•int myNumbers[4] = {25, 50, 75, 100};


myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25

5/25
Set Array Size

Examples

•// Declare an array of four integers:


int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;

5/25
Get Array Size or Length

Examples

•int myNumbers[5] = {10, 25, 50, 75, 100};


printf("%lu", sizeof(myNumbers));

But when you just want to find out how many elements an array has,
you can use the following formula (which divides the size of the array
by the size of one array element):

Examples
int myNumbers[5] = {10, 25, 50, 75, 100};
int length = sizeof(myNumbers) / sizeof(myNumbers[0]);

printf("%d", length);

5/25
create a program that calculates the
average of different ages:
 // An array storing different ages
int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70};

float avg, sum = 0;


int i;

// Get the length of the array


int length = sizeof(ages) / sizeof(ages[0]);

// Loop through the elements of the array


for (int i = 0; i < length; i++) {
sum += ages[i];
}

// Calculate the average by dividing the sum by the length


avg = sum / length;

// Print the average


printf("The average age is: %.2f", avg);
we create a program that finds the
lowest age among different ages:
 // An array storing different ages
int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70};

// Get the length of the array


int length = sizeof(ages) / sizeof(ages[0]);

// Create a variable and assign the first array element of


ages to it
int lowestAge = ages[0];

// Loop through the elements of the ages array to find the


lowest age
for (int i = 0; i < length; i++) {
if (lowestAge > ages[i]) {
lowestAge = ages[i];
}
}
ARRAYS

 Examples of the one-dimensional array declarations,

int xNum[20], yNum[50];


float fPrice[10];
char chLetter[70];
 The first example declares two arrays named xNum and
yNum of type int. Array xNum can store up to 20 integer
numbers while yNum can store up to 50 numbers.
 The second line declares the array fPrice of type float.
It can store up to 10 floating-point values.

8/25
ARRAYS
Array Initialization
 An array may be initialized at the time of declaration.
 Giving initial values to an array.
 Initialization of an array may take the following form,
type array_name[size] = {a_list_of_value};
 For example:
int idNum[7] = {1, 2, 3, 4, 5, 6, 7};
float fFloatNum[5] = {5.6, 5.7, 5.8, 5.9, 6.1};
char chVowel[6] = {'a', 'e', 'i', 'o', 'u', '\0'};
 The first line declares an integer array idNum and it immediately
assigns the values 1, 2, 3, ..., 7 to idNum[0], idNum[1],
idNum[2],..., idNum[6] respectively.
 The second line assigns the values 5.6 to fFloatNum[0], 5.7 to
fFloatNum[1], and so on.
 Similarly the third line assigns the characters 'a' to chVowel[0], 'e' to
chVowel[1], and so on. Note again, for characters we must use the
single apostrophe/quote (') to enclose them.
 Also, the last character in chVowel is NULL character ('\0').

9/25
ARRAYS
 Initialization of an array of type char for holding strings may take the following
form,

char array_name[size] = "string_lateral_constant";


 For example, the array chVowel in the previous example could have been
written more compactly as follows,

char chVowel[6] = "aeiou";


 When the value assigned to a character array is a string (which must be
enclosed in double quotes), the compiler automatically supplies the NULL
character but we still have to reserve one extra place for the NULL.
 For unsized array (variable sized), we can declare as follow,

char chName[ ] = "Mr. Dracula";


 C compiler automatically creates an array which is big enough to hold all the
initializer.

13/25
ARRAYS
Two Dimensional/2D Arrays

 A two dimensional array has two subscripts/indexes.


 The first subscript refers to the row, and the second, to the column.
 Its declaration has the following form,
data_type array_name[1st dimension size][2nd dimension size];

 For examples,
int xInteger[3][4];
float matrixNum[20][25];

 The first line declares xInteger as an integer array with 3 rows and
4 columns.
 Second line declares a matrixNum as a floating-point array with 20
rows and 25 columns.

14/25
ARRAYS
 If we assign initial string values for the 2D array it will look
something like the following,

char Name[6][10] = {"Mr. Bean", "Mr. Bush", "Nicole",


"Kidman", "Arnold", "Jodie"};

 Here, we can initialize the array with 6 strings, each with


maximum 9 characters long.
 If depicted in rows and columns it will look something like the
following and can be considered as contiguous arrangement in
the memory.

15/25
ARRAYS
 Take note that for strings the null character (\0) still needed.
 From the shaded square area of the figure we can determine the size of the
array.
 For an array Name[6][10], the array size is 6 x 10 = 60 and equal to the
number of the colored square. In general, for

array_name[x][y];
 The array size is = First index x second index = xy.
 This also true for other array dimension, for example three dimensional
array,

array_name[x][y][z]; => First index x second index x third index = xyz


 For example,

ThreeDimArray[2][4][7] = 2 x 4 x 7 = 56.
 And if you want to illustrate the 3D array, it could be a cube with wide, long
and height dimensions.

17/25
Examples
Program to take 5 values from the user and store them in
an array
Examples

 Addition of 2D matrix
 Subtraction of 2D matrix
 Multiplication of 2D matrix
 Transpose/ Inverse of 2D matrix
End-of-C-arrays

25/25

You might also like