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

CSE 109 Lec 9 Array Basic

The document discusses arrays in C programming. It defines an array as a collection of elements of the same type stored in contiguous memory locations. Arrays allow grouping of related data of the same type in one variable. The document covers array characteristics, declaration syntax using data type and size, accessing elements using indexes from 0 to size-1, and examples of array applications.

Uploaded by

anisruhan6
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 views15 pages

CSE 109 Lec 9 Array Basic

The document discusses arrays in C programming. It defines an array as a collection of elements of the same type stored in contiguous memory locations. Arrays allow grouping of related data of the same type in one variable. The document covers array characteristics, declaration syntax using data type and size, accessing elements using indexes from 0 to size-1, and examples of array applications.

Uploaded by

anisruhan6
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/ 15

CSE-109: Computer Programming

Shahriar Rahman Khan


Lecturer, Department of CSE, MIST
Email: [email protected]

1
Lec 9: Array in C

PREPARED BY
SHAHRIAR RAHMAN KHAN
Lecturer, Department of CSE, MIST
INTRODUCTION
 An array is a collection of elements of the same type that are referenced by a
common name. An individual variable in the array is called an array element.
 All the elements of an array occupy a set of contiguous memory locations.
 Arrays allow programmers to group related items of the same data type in one
variable.
 However, when referring to an array, one has to specify not only the array or
variable name but also the index number of interest.

Example:
int var[10];
float venus[20];
Books
double earth[5];
char pluto[7];

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 3


Array Characteristics
 Array is a collection - Array is a container that can hold a collection of data.
 Array is finite - The collection of data in array is always finite, which is
determined prior to its use.
 Array is sequential - Array stores collection of data sequentially in memory.
 Array contains homogeneous data - The collection of data in array must share
a same data type.

We can divide arrays in two categories.


• One-dimensional array (Or single-dimensional array)
• Multi-dimensional array

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 4


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;

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;
}

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 6


Why Need To Use Array Type?
 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.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 7


Why Need To Use Array Type?

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 8


Array Application

 Given a list of test scores, determine the max, min, average, sum
 Array count (Ex: Given the height measurements of students in a class, output
the names of those students who are taller than average.)
 Array reverse (temp array, in-place)
 Frequency of digits in a number
 Read in a list of student names and rearrange them in alphabetical order
(sorting).
 Searching a particular item from a given list.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 9


Rules for Declaring 1-D Array in C
 Before using and accessing, we must declare the array variable.
 In an array, indexing starts from 0 and ends at size-1. For example, if we have
arr[10] of size 10, then the indexing of elements ranges from 0 to 9.
 We must include data type and variable name while declaring one-dimensional
arrays in C.
 We can initialize them explicitly when the declaration specifies array size
within square brackets is not necessary.
 Each element of the array is stored at a contiguous memory location with a
unique index number for accessing.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 10


Array Declaration
 Syntax:
 data_type arrayName [array_size]
int studMark [1000];
 <type> define the base type of the array, which is the type of each element in
the array i.e. common to all array elements
 <array_size> The size of the array is indicated by the number of elements in
the array. How big the array is.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 11


Array Declaration
 array of 5 uninitialized integers:
int arr[5];

All the consecutive array elements are at a distance of 4 bytes from each other as an int block
occupies 4 bytes of memory in the system (64-bit Architecture). Also, each array element contains
a garbage value because we have not initialized the array yet.

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 12


Accessing Array Index
• The general form (syntax) of accessing an array component is:

arrayName[indexExp]

where indexExp, called index, is any expression whose value is a nonnegative integer

• Index value specifies the position of the component in the array


• The [] operator is called the array subscripting operator
• The array index always starts at 0

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 14


Accessing Array Index
• Declare an array of 10 integers array named Ar:
int Ar[10]; // array of 10 ints
0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --

• The expression in the brackets is known as the index/Subscript. The [] operator is


called the array subscripting operator.
• First element of array has index 0: Ar[0]
• Second element of array has index 1, and so on.
Ar[1], Ar[2], Ar[3],…
• Last element has an index one less than the size of the array: Ar[9]

• Array indexes always start at zero in C

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 15


Subscripting
• Declare an array of 10 integers array named Ar:
int Ar[10]; // array of 10 ints

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- -- -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]

Ar[3] = 1;

0 1 2 3 4 5 6 7 8 9
Ar -- -- -- 1 -- -- -- -- -- --
Ar[0] Ar[1] Ar[2] Ar[3] Ar[4] Ar[5] Ar[6] Ar[7] Ar[8] Ar[9]

11/20/2023 LEC SHAHRIAR RAHMAN KHAN 16


GOOD NEWS!
T H E TO P I C I S
OVER…
THANK YOU!

11/20/2023 18

You might also like