0% found this document useful (0 votes)
43 views22 pages

10 Arrays

The document discusses various data structures in C including arrays, stacks, queues, linked lists, and trees. It provides brief definitions and descriptions of each: arrays allow storing and accessing data without order, stacks follow LIFO, queues follow FIFO, linked lists allow inserting and deleting in any order through nodes, and trees store data non-linearly with a root node and sub-nodes. The document also provides examples of using pointers to access array elements and a function to add two matrices.

Uploaded by

Ahmed K
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)
43 views22 pages

10 Arrays

The document discusses various data structures in C including arrays, stacks, queues, linked lists, and trees. It provides brief definitions and descriptions of each: arrays allow storing and accessing data without order, stacks follow LIFO, queues follow FIFO, linked lists allow inserting and deleting in any order through nodes, and trees store data non-linearly with a root node and sub-nodes. The document also provides examples of using pointers to access array elements and a function to add two matrices.

Uploaded by

Ahmed K
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
You are on page 1/ 22

Array in C

Index

Introduction to Arrays.
Declaration of a Array. Variables, Creating Arrays.
The Length of Arrays.
Initializing Arrays.
Multidimensional Arrays.
Passing array to function
Accessing array through pointer
Introduction to ARRAYS
Array is a data structure that represents
a collection of the same types of data.

Data Types
Data items

 Sequence of elements stored one after another in


memory represents an array.
 An array is used to store a collection of data
 Strings……………………
The Length of Arrays.

Place a value into “b” with


The statement:
b=5;

Place a value into “a” with


a statement:
a[2]=5;
ARRAY Declaration
Data type int a[10] Size of array

Name of array
 a –array of 10 integers
 Square braces represents number of elements
 Array index starts from a[0] as shown below:

Note: In C, array indices always begin with zero.


Initialization of ARRAY
 Can be initialized once they are declared.
 int a[10]={28,15,86,54,78,21,34,7,91,67};
 int a[]={28,15,86,54,78,21,34,7,91,67};
 Values should be given in curly brackets.
 Less than 10 elements-’0’ will be filled.
 Greater than 10 elements-excessive element declared
warning will be reported.
 List of values-separated by commas.
Accessing ARRAY Elements

28 15 86 54 78 21 34 7 91 67

 Array index points to particular elements in an


array.
 Read/Write
 Example:
◦ a[3]
◦ a[7]
◦ a[5]
◦ a[10]
Usage of for loop
 Commonly used to access array elements.
for(i=0;i<10;i++)
{
printf(“%d \n”, a[i]);
}
 Prints the array elements
 If you try to access the undefined element, result will
be unpredictable.(compilation will be successful)

Array1.c
 Take the case where a is an array int a [10]
 What will be the output of the following statement?

printf(“%d”, sizeof(a[0]));
printf(“%d”, sizeof(a));
2-D Arrays

 To handle matrix and tables, need array with


multiple subscripts.
 Two-dimensional array-two subscripts
 Row and column-starts with zero
2-D ARRAY Declaration
Size of an
Data type float marks[4][3] array
4 rows
Name of the array 3 columns
0 1 2
0 35.5 40.5 45.5
1 50.5 55.5 60.5
2 65.0 70.0 75.0
3 80.0 85.0 90.0

Marks[0][0]=35.5
Marks[1][1]=55.5
Marks[2][1]=70.0 T
Marks[3][1]=85.0
Storage of 2-D ARRAY
200 204 208 212 216 220 224 228 232 236 240 244

Marks[0][0]
Marks[1][0] Marks[2][0] Marks[3][0]
Marks[0][1]

Initialization:
Float marks[4][3]={ {35.5,40.5,45.5},
{50.5,55.5,60.5},
{65.0,70.0,75.0},
{80.0,85.0,90.0} };
T
3-D ARRAY

Array3d.c
Passing ARRAY to a Function
/*Pass by Value*/
main( )
{
int i ;
int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i <=6 ; i++ ) {
display ( marks[i] ) ;}
}
display ( int m )
{
printf ( "%d ", m ) ;
}
arrpv.c
Passing ARRAY to a Function
/* Call by reference */
main( )
{
int i ;
int marks[ ] = { 55, 65, 75, 56, 78, 78, 90 } ;
for ( i = 0 ; i <= 6 ; i++ ) {
disp ( &marks[i] ) ; }
}
disp ( int *n )
{
printf ( "%d ", *n ) ;
} arrpcallr.c
POINTERS and ARRAYS

 Facts to be considered:
 Array elements are always stored in contiguous

memory locations.
 Pointer when incremented always points to

immediately next location of its type.


POINTER to ARRAY
main()
{
int num[ ]={24,34,12,44,56,17};
int i=0,*j;
j=&num[0];
while(i<=5)
{
printf(“Address=%u \n”,&num[i]);
printf(“Element=%d \n”,*j);
i++;
j++;
}
}
arraypnt.c
POINTER to ARRAY
 Accessing array elements using pointer is faster than
accessing them by subscripts.
 Should be accessed using pointers, if the elements are
to be accessed in a fixed order.
 If there is no fixed logic, then subscript can be used
 Pointer notations:

*num, *(num+1), *(num+2)


 Write a program to add two matrices using
function
 E.g.: function addmat()
Data Structure in C

Data structures are used to store data in a computer in an organized


form. In C language Different types of data structures are; Array, Stack,
Queue, Linked List, Tree.

•Array: Array is collection of similar data type, you can insert and
deleted element form array without follow any order.
•Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last
entered element removed first.
•Queue: Queue work on the basis of First-In-First-Out (FIFO). First
entered element removed first.
•Linked List: Linked list is the collection of node, Here you can insert
and delete data in any order.
•Tree: Stores data in a non linear form with one root node and sub
nodes.

You might also like