arrays in c language
TRANSCRIPT
![Page 1: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/1.jpg)
C.K.PITHAWALA COLLEGE OF ENGINEERING & TECHNOLOGY, SURAT
Branch:-computer 1st Year (Div. D)ALA Subject:- Computer Programming & Utilization
ALA Topic Name:- Arrays in C languageGroup No:-D9
Student Roll no Enrolment No Name
403 160090107051 Sharma Shubham 421 160090107028 Naik Rohan 455 160090107027 Modi Yash 456 160090107054 Solanki Divyesh
Submitted To
Unnati ShahHemil Patel
![Page 2: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/2.jpg)
Arrays In C Language
![Page 3: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/3.jpg)
Contents
I. Introduction Declaring and creating Arrays Accessing array elements Array: input & output
II. One- dimensional Arrays
III. Declaration of One- dimensional Arrays
IV. Initialization of One- dimensional Arrays
V. Two- dimensional Arrays
VI. Initialization of Two- dimensional Arrays
VII. Multi-dimensional Arrays
VIII.Dynamic Arrays
IX. Array examples
![Page 4: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/4.jpg)
INTRODUCTION
![Page 5: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/5.jpg)
What is An Array?
An array is a fixed-size sequenced collection of elements of the same data type.
It is simply a grouping of like-type data. In its simplest form, an array can be used to represent a list of numbers, or a list of names.
Some examples where the concept of array can be used are as under: List of temperatures recorded every hour in a day, or a month, or a
year. List of employees in an organization. List of products and their cost sold by a store. Test scores of a class of students. List of customers and their telephone numbers. Etc.
![Page 6: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/6.jpg)
0 1 2 3 4 Element index
Element of an array
Array of 5 elements
![Page 7: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/7.jpg)
An Array provides a convenient structure for representing data hence it is classified as one of the Data Structure In C.
Example:
The above example represents the income of employees. Individual values are called elements While the complete set of values is referred to as an array. In above example there can be maximum 10 elements.
An Array is a derived data type Based on the basis of dimensions there are three types of
array : 1. One - dimensional Arrays 2. Two - dimensional Arrays 3. Multi - dimensional Arrays
income[10]
![Page 8: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/8.jpg)
Declaring & Creating Arrays
![Page 9: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/9.jpg)
Declaring Arrays
Declaration defines the type of the elements Square brackets [ ] indicate “Array size" Examples:
(Where s.o.a is size of array)
int array[s.o.a];
![Page 10: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/10.jpg)
Creating and Initializing Arrays
Creating and initializing can be done together:
Int myIntArray[5] = {1, 2, 3, 4, 5};
myIntArray
managed heap(dynamic memory)
0 1 2 3 4… … … … …
![Page 11: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/11.jpg)
Accessing Array Elements
READ AND MODIFY ELEMENTS BY INDEX
![Page 12: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/12.jpg)
How to Access Array Element?
Array elements are accessed using the square brackets operator [ ] (indexer)› Array indexer takes element’s index as parameter
› The first element has index 0
› The last element has index Length-1
Array elements can be retrieved and changed by the [ ] operator
![Page 13: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/13.jpg)
Arrays: Input and Output
Reading and Printing Arrays on the Console
![Page 14: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/14.jpg)
Reading Arrays
Ex. Int array[5];
Scanf(“%d”,&array[5]);--------------------------------------------------------------------------------------------------
Printing Arrays
printf(“a[5]=%d”,array[5]);--------------------------------------------------------------------------------------------------
![Page 15: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/15.jpg)
One - Dimensional Arrays
A list of item can be given one variable name using only one subscript and such a variable is called a single subscripted variable or One- dimensional array.
It can be expressed as :
x[0], x[1], x[2], x[3], x[4]……..x[n]
C performs no bound checking and, therefore, care should be exercised to ensure that the array indices are within the declared limits.
![Page 16: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/16.jpg)
Declaration of One-Dimensional Array
Syntax:
Data type can be int, float, char etc. Ex.
> int chat[10];
> char pr[50]; Array of char data type is called STRING. When compiler sees a char String, it terminates it with an
additional null character. so string array holds the null char ‘\0’.we must allow 1 extra element space for the null terminator.
data type variable_name[s.o.a];
![Page 17: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/17.jpg)
Initialization of one Dimensional Array
An array can be initialized at either of the following two stages: At compile time At run time
• At compile time Syntax::
datatype array name[S.O.A]={list of value};
Ex:: int array[5]={1,2,3,4,5}; If we have more initializers than the declared size, the compiler
will produce an error. That is illegal in C. Ex:: int number[3]={1,2,3,4,5};
• Run Time Initialization An array can be explicitly initialized at run time. This
approach is usually applied for initialization large arrrays.
![Page 18: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/18.jpg)
Ex:: --------------------------------------------------------------- --------------------------------------------------------------- For (i=0; i<5; i++) { sum[i]=I; } --------------------------------------------------------------- ---------------------------------------------------------------.
We can also use a read function such as scanf to initialize array.
Ex:: Int x[3]; Scanf (“%d %d %d ,&x[0], &x[0], &x[0]”);
Will intialize array elements with the value entered through the keyboard.
![Page 19: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/19.jpg)
Two Dimensional Array
If we have store the value as table then we have to use 2-D array.
Ex:-
C allow s us to define such tables of items by using 2-D arrays.
Syntax:: Type array name[raw size][column size];
Hear the first index contains row size, second index contains column size.
i-1 i-2 i-31. 10 20 302. 20 10 13
![Page 20: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/20.jpg)
Initialization of Two Dimensional Array
Like 1-d array, 2-d array may be initialized by following their declaration with a list of initial values enclosed in braces.
Ex:: Int table[2][3]={0,0,0,1,1,1}; Int table[2][3]={ {0,0,0} ,{1,1,1} };
Hear the first index saw raw size, second Index saw column size.
![Page 21: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/21.jpg)
Multi Dimensional Array
C allows arrays of three or more dimensions. The exact limit is determined by the compiler .
The general form of a multi –dimensional array is….. Type array name[x1][x2][x3][x4]……..[xn];
Where x1 x1 is size of array.
ANSI C does not specify any limit for array dimension. However , most compiler permit seven to ten dimension . Some allow even more.
![Page 22: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/22.jpg)
Dynamic Arrays
We created array at run time, so we can not modify it at run time so they are called static array. This approach works fine as long as we know exactly what our data requirement are..
In C it is possible to allocate memory to arrays at run time , which known as dynamic memory allocation and the array called dynamic array.
Dynamic array are created using what are known as pointer variables and memory management function malloc, calloc, realloc, which are in <stdio.h>
![Page 23: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/23.jpg)
Some Examples of Array
![Page 24: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/24.jpg)
Ex: Write a program to print first 10 number. : :
# i n c l u d e < s t d i o . h >
v o i d m a i n ( )
{
I n t i , r [ 1 0 ] ; / / p r [ 1 0 ] i s a r r a y o f 1 0 e l e m e n t s .
f o r ( i = 1 ; i < = 1 0 ; i + + )
{
r [ i ] = i ; / / a s s i g n v a l u e t o a r r a y
p r i n t f ( " % d " , r [ i ] ) ;
p r i n t f ( ” \ n ” ) ;
}
}
![Page 25: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/25.jpg)
:: output::12345678910
![Page 26: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/26.jpg)
Write a program to read and display 3x3 matrix.# i n c l u d e < s t d i o . h >v o i d m a i n ( ){ i n t i , j , a [ 3 ] [ 3 ] ;
p r i n t f ( “ E n t e r t h e e l e m e n t s o f 3 x 3 M a t r i x : \ n ” ) ;
f o r ( i = 0 ; i < 3 ; i + + )
f o r ( j = 0 ; j < 3 ; j + + )
{
p r i n t f ( “ a [ % d ] [ % d ] = ” , i , j ) ;
s c a n f ( “ % d ” , a [ i ] [ j ] ) ;
}
p r i n t f ( “ T h e v a r i o u s e l e m e n t s i n 3 x 3 m a t r i x a r e : \ n ” ) ;
f o r ( i = 0 ; i < 3 ; i + + )
{
p r i n t f ( “ \ n \ t \ t ” ) ;
f o r ( j = 0 ; j < 3 ; j + + )
p r i n t f ( “ % d \ t ” , a [ i ] [ j ] ) ;
} }
![Page 27: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/27.jpg)
: : o u t p u t : :
E n t e r t h e e l e m e n t s o f t h e 3 x 3 m a t r i x :a [ 0 ] [ 0 ] = 1a [ 0 ] [ 1 ] = 2a [ 0 ] [ 2 ] = 3a [ 1 ] [ 0 ] = 4a [ 1 ] [ 1 ] = 5a [ 1 ] [ 2 ] = 6a [ 2 ] [ 0 ] = 7a [ 2 ] [ 1 ] = 8a [ 2 ] [ 2 ] = 9T h e v a r i o u s e l e m e n t s o f t h e 3 X 3 m a t r i x :
1 2 34 5 67 8 9
![Page 28: Arrays in C language](https://reader035.vdocuments.site/reader035/viewer/2022062900/58e65b591a28ab8d758b47d9/html5/thumbnails/28.jpg)
End of PresentationThank You