[go: up one dir, main page]

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

Array

The document discusses scalar and aggregate variables in C programming, focusing on arrays as a type of aggregate variable. It explains how to define, initialize, and manipulate one-dimensional and multi-dimensional arrays, including user input and searching algorithms. Additionally, it covers practical examples such as finding sums, maximums, and copying elements between arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views20 pages

Array

The document discusses scalar and aggregate variables in C programming, focusing on arrays as a type of aggregate variable. It explains how to define, initialize, and manipulate one-dimensional and multi-dimensional arrays, including user input and searching algorithms. Additionally, it covers practical examples such as finding sums, maximums, and copying elements between arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

st

Array 1 part
Rakib Mahmud

11/10/24 1
Scalar Variables versus Aggregate
Variables
• So far, the only variables we’ve seen are scalar: capable of
holding a single data item.
• C also supports aggregate variables, which can store
collections of values.
• There are two kinds of aggregates in C: arrays and structures.

11/10/24 2
Introduction
• Int roll1, roll2, roll3,…..,roll45 => int roll[45]
• An array is a data structure containing a number of data values,
all of which have the same type.
• The simplest kind of array has just one dimension.
• The elements of a one-dimensional array a are conceptually
arranged one after another in a single row (or column):

11/10/24 3
Defining Arrays
• When defining arrays, specify
• Name
• Type of array (Array can be defined over any type)
• Number of elements which is array size.
arrayType arrayName[ numberOfElements ];
• Examples:
float grade[ 7 ];
int c[ 10 ];
• Defining multiple arrays of same type
• Example:
int b[ 100 ], x[ 27 ];

11/10/24 4
Array Initialization
• int roll[5];
• It means roll[0], roll[1], roll[2], roll[3], roll[4]
• 0, 1, 2, 3, 4 these are indexes
• Initialization array during
declaration:
• roll[0] = 8
• roll[1] = 9
• int roll[5] = {8, 9, 7, 10, 6}
• roll[2] =7 • int roll[ ] = {8, 9, 7, 10, 6}
• roll[3] =10 roll[0] roll[1] roll[2] roll[3] roll[4]
• roll [4] =6 8 9 7 10 6

11/10/24 5
Why array index starts from zero
• int ara1[ ] = { 1, 2, 3, 4, 5};
• Elements in the array are stored in consecutive memory
locations.
• ara1 represent base address (1000)
• If we want to find address of another element = Base Address +
Positionindex
* Data type size;Address Values
0 1000 1
1 1004 2
2 1008 3
3 1012 4
4 1016 5
11/10/24 6
Printing an array
• int roll[5] = {8, 9, 7, 10, 6}
• We want to show this output: 8, 9,10, 7, 6
roll[0] roll[1] roll[2] roll[3] roll[4]
8 9 7 10 6

printf(“%d\n”,roll[0]); for(int i=0;i<=4;i++)


printf(“%d\n”,roll[1]);
printf(“%d\n”,roll[2]);
{
printf(“%d\n”,roll[3]); printf(“%d\n”,roll[i]);
printf(“%d\n”,roll[4]); }

These values, known as elements, can be individually selected by


their position within the array.

11/10/24 7
Getting user input
• int roll[5] = {8, 9, 7, 10, 6}
roll[0] roll[1] roll[2] roll[3] roll[4] roll[0] roll[1] roll[2] roll[3] roll[4]
8 9 7 10 6

scanf(“%d”,&roll[0]);
scanf(“%d”,&roll[1]); for(int i=0;i<=4;i++)
{
scanf(“%d”,&roll[2]);
scanf(“%d”,&roll[i]);
scanf(“%d”,&roll[3]);
}
scanf(“%d”,&roll[4]);

11/10/24 8
Types of Array
• Array can of following types:
1. One dimensional (1-D) arrays or Linear arrays.
• Example:
• int roll[5];
2. Multi dimensional arrays
1. Two dimensional (2-D) arrays or Matrix arrays. Example:
• int roll[2][3];
2. Three dimensional arrays. Example:
• int marks[2][3][2];

11/10/24 9
C Program to Find Sum of 1D Array
(when array size is known)

11/10/24 10
C Program to Find Sum of 1D Array
(when array size is unknown)

11/10/24 11
C Program to Find Maximum of an Array

11/10/24 12
C Program to Find Minimum of an Array

11/10/24 13
C Program to Make Fibonacci Series
Using Array

11/10/24 14
Searching a number in array
• The searching algorithm searches for the specified element in
the given list.
• Binary search and Linear search are the commonly used
searching algorithm.
• The linear search is the algorithm of choice for short lists,
because it’s simple and requires minimal code to implement.

11/10/24 15
Linear Search
• int num[ ] = { 1, 2, 7, 5, 4, 9 }
• Search the value 5
• If the item found?
• If yes then what’s the position?
• Ans: Yes, 4th position.

11/10/24 16
C Program to Perform Linear Search

11/10/24 17
Copy All the Elements of One Array to
Another Array
• Input:
• First Array: ara1[5] = {3, 6, 9, 2, 5}
• Output:
• First Array : ara1[5] = {3, 6, 9, 2, 5}
• Second Array : ara2[5] = {3, 6, 9, 2, 5}

11/10/24 18
C Program to Copy All the Elements of
One Array to Another Array

11/10/24 19
Thank
You
11/10/24 20

You might also like