SlideShare a Scribd company logo
1 of 28
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 language
Group 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 Shah
Hemil Patel
Arrays
In C Language
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
INTRODUCTION
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.
0 1 2 3 4 Element
index
Element of an array
Array of 5
elements
 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]
Declaring & Creating Arrays
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];
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
… … … … …
Accessing Array Elements
READ AND MODIFY ELEMENTS BY INDEX
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
Arrays: Input and Output
Reading and Printing Arrays on the Console
Reading Arrays
 Ex. Int array[5];
Scanf(“%d”,&array[5]);
-------------------------------------------------
-------------------------------------------------
Printing Arrays
printf(“a[5]=%d”,array[5]);
-------------------------------------------------
-------------------------------------------------
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.
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];
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.
 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.
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-3
1. 10 20 30
2. 20 10 13
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.
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.
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>
Some Examples of Array
 Ex: Write a program to print first 10 number.
::
#inc lude< s tdio.h >
void main( )
{
Int i, r [10]; // pr [10] is ar r ay of 10
elements .
for ( i = 1; i < = 10; i+ + )
{
r [i]= i; // as s ign value to ar r ay
pr intf( "%d",r [i]) ;
pr intf( ” n”);
}
}
:: output::
1
2
3
4
5
6
7
8
9
10
 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 ] ) ;
} }
:: output ::
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 ] = 1
a [ 0 ] [ 1 ] = 2
a [ 0 ] [ 2 ] = 3
a [ 1 ] [ 0 ] = 4
a [ 1 ] [ 1 ] = 5
a [ 1 ] [ 2 ] = 6
a [ 2 ] [ 0 ] = 7
a [ 2 ] [ 1 ] = 8
a [ 2 ] [ 2 ] = 9
T 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 3
4 5 6
7 8 9
End of Presentation
Thank You

More Related Content

What's hot

C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2Rajendran
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)Imdadul Himu
 
Conditional operators
Conditional operatorsConditional operators
Conditional operatorsBU
 
Python dictionary
Python dictionaryPython dictionary
Python dictionaryeman lotfy
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6sumitbardhan
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 

What's hot (20)

Arrays in c
Arrays in cArrays in c
Arrays in c
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Conditional operators
Conditional operatorsConditional operators
Conditional operators
 
Heap
HeapHeap
Heap
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Python dictionary
Python dictionaryPython dictionary
Python dictionary
 
Strings
StringsStrings
Strings
 
Unit 2. Elements of C
Unit 2. Elements of CUnit 2. Elements of C
Unit 2. Elements of C
 
Strings in C
Strings in CStrings in C
Strings in C
 
358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6358 33 powerpoint-slides_6-strings_chapter-6
358 33 powerpoint-slides_6-strings_chapter-6
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Array
ArrayArray
Array
 
Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]Data Structures - Lecture 3 [Arrays]
Data Structures - Lecture 3 [Arrays]
 

Viewers also liked (8)

String functions in C
String functions in CString functions in C
String functions in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Structure c
Structure cStructure c
Structure c
 
Structure of a C program
Structure of a C programStructure of a C program
Structure of a C program
 
Structure in c
Structure in cStructure in c
Structure in c
 
Array in c language
Array in c languageArray in c language
Array in c language
 
String c
String cString c
String c
 
String in c
String in cString in c
String in c
 

Similar to C arrays and multi-dimensional arrays

Similar to C arrays and multi-dimensional arrays (20)

Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
02 arrays
02 arrays02 arrays
02 arrays
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays
ArraysArrays
Arrays
 
Abir ppt3
Abir ppt3Abir ppt3
Abir ppt3
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays
ArraysArrays
Arrays
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Array
ArrayArray
Array
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

C arrays and multi-dimensional arrays

  • 1. 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 language Group 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 Shah Hemil Patel
  • 3. 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
  • 5. 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.
  • 6. 0 1 2 3 4 Element index Element of an array Array of 5 elements
  • 7.  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]
  • 9. 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];
  • 10. 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 … … … … …
  • 11. Accessing Array Elements READ AND MODIFY ELEMENTS BY INDEX
  • 12. 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
  • 13. Arrays: Input and Output Reading and Printing Arrays on the Console
  • 14. Reading Arrays  Ex. Int array[5]; Scanf(“%d”,&array[5]); ------------------------------------------------- ------------------------------------------------- Printing Arrays printf(“a[5]=%d”,array[5]); ------------------------------------------------- -------------------------------------------------
  • 15. 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.
  • 16. 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];
  • 17. 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.
  • 18.  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.
  • 19. 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-3 1. 10 20 30 2. 20 10 13
  • 20. 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.
  • 21. 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.
  • 22. 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>
  • 24.  Ex: Write a program to print first 10 number. :: #inc lude< s tdio.h > void main( ) { Int i, r [10]; // pr [10] is ar r ay of 10 elements . for ( i = 1; i < = 10; i+ + ) { r [i]= i; // as s ign value to ar r ay pr intf( "%d",r [i]) ; pr intf( ” n”); } }
  • 26.  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 ] ) ; } }
  • 27. :: output :: 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 ] = 1 a [ 0 ] [ 1 ] = 2 a [ 0 ] [ 2 ] = 3 a [ 1 ] [ 0 ] = 4 a [ 1 ] [ 1 ] = 5 a [ 1 ] [ 2 ] = 6 a [ 2 ] [ 0 ] = 7 a [ 2 ] [ 1 ] = 8 a [ 2 ] [ 2 ] = 9 T 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 3 4 5 6 7 8 9

Editor's Notes

  1. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  2. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  3. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*
  4. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*