4. What Is Array ??
• Collection Of same type of data.
• Data Type ( Int, float, double, char).
• Saves data in a sequence.
• Examples:
– 1. Employees Info of an organization.
– 2. Score of students of a class.
– 3. Table of daily rainfall data.
– 4. Temperature record of hours.
5. 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;
– }
6. ARRAYS
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.
7. Examples Using Arrays
Initializers :
int n[ 5 ] = { 1, 2, 3, 4, 5 };
– If not enough initializers, rightmost elements become 0
int n[ 5 ] = { 0 }
• All elements 0
– If too many a syntax error is produced syntax error
– C arrays have no bounds checking
If size omitted, initializers determine it
int n[ ] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore 5 element array
8. 1 /* Fig.01: fig01.c
2 Histogram printing program */
3 #include <stdio.h>
4 #define SIZE 10
5
6 int main()
7 {
8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
9 int i, j;
10
11 printf( "%s%13s%17sn", "Element", "Value", "Histogram"
);12
13 for ( i = 0; i <= SIZE - 1; i++ ) {
14 printf( "%7d%13d ", i, n[ i ]) ;
15
16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar
*/17 printf( "%c", '*' );
18
19 printf( "n" );
20 }
21
22 return 0;
23 }
10. Types Of Array
Three Types Of Array
– One Dimensional Array(1-D)
– Two Dimensional Array(2-D)
– Multi Dimensional Array(A[s1][s2[s3][s4]…)
11. One Dimensional Array(1-D)
– Initialization:
– Type Array_name [Size];
– Type Array_name [Size] = {list of Values};
Example:
int A[10];
int A[5] ={50,60,70,80,90};
User Define:
int A[n]; [Size of Array declare by User.]
12. Two Dimensional Array(2-D)
• Initialization:
– Type Array_name[row_size][column_size];
– Type Array_name[row_size][column_size]={{s1},{s2},{s3}};
• Here s1,s2,s3 refers to sets of number.
Example:
int A[3][3] ;
int A[3][3] = {{10,20,45},{42,79,81},{89,9,36}};
User Define:
int A[r][n];
Value of {r,n} given by
13. Multi Dimensional Array
Initialization:
– Type Array_name[s1][s2][s3]…..[sm];
Example:
int survey [3][5][3];
float table [5][4][5][3];
Pros & Cons:
Two dimensional array often
consider as multi dimensional array.
Multi dimensional array is derived
from basic C Language.
Multi
Dimensional array
Example
14. Insertion In an Array
One Dimensional Array(1-D):
– Loops runs from 0 to n-1 th index.
– Sorting might involve if data was ask in sorting mode.
How 1-D array insertion Works:
For 0 to n-1
{input value;(Using scanf)}
int A[5
15. Insertion In An Array
Two Dimensional Array(2 - D):
– Loops runs
for 0 to row – 1
{for 0 to column – 1
input value;(scanf) }
How It works:
int A[3][4];
Step 1 Step 2 Step 3
16. Deletion In an Array
One Dimensional Array(1-D):
User given input.
Search for data
Runs in the loops looking for the given input.
2 Condition: Found or Not found
Found condition:
Example: search: 4 (Found if available).
Not Found (Print Not Found)
ORIGIN (Array) After Deletion
17. Deletion In an Array
Two Dimensional Array(2D)
User Given Input(search: 12)
Process:
Runs inside the loops looking for element.
Condition:
If found delete the input value and replace by ‘0’
Else print not found.
Example: Before After