SlideShare a Scribd company logo
9
Most read
11
Most read
22
Most read
ArraysArrays
inin
C ProgrammingC Programming
V.V. SubrahmanyamV.V. Subrahmanyam
SOCIS, IGNOUSOCIS, IGNOU
Date: 24-05-08Date: 24-05-08
Time: 11-00 to 11-45Time: 11-00 to 11-45
IntroductionIntroduction
 Many programs require theMany programs require the
processing of multiple, related dataprocessing of multiple, related data
items that have commonitems that have common
characteristics likecharacteristics like list of numbers,list of numbers,
marks in a course etc..marks in a course etc..
ExampleExample
 Consider to store marks of fiveConsider to store marks of five
students. They can be stored usingstudents. They can be stored using
five variables as follows:five variables as follows:
int ml,m2,m3,m4,m5;int ml,m2,m3,m4,m5;
Now, if we want to do the same thingNow, if we want to do the same thing
for 100 students in a class then onefor 100 students in a class then one
will find it difficult to handle 100will find it difficult to handle 100
variables.variables.
 In such situations it is oftenIn such situations it is often
convenient to place the dataconvenient to place the data
items into an array, where theyitems into an array, where they
will share the same name with awill share the same name with a
subscript.subscript.
Characteristic Features of an ArrayCharacteristic Features of an Array
 An array is a collection of similarAn array is a collection of similar
kind of data elements stored inkind of data elements stored in
adjacent memory locations and areadjacent memory locations and are
referred to by a single array-name.referred to by a single array-name.
 Arrays are defined in much theArrays are defined in much the
same manner as ordinary variables,same manner as ordinary variables,
except that each array name mustexcept that each array name must
be accompanied by a sizebe accompanied by a size
specification.specification.
Contd…Contd…
 In the case of C, you have toIn the case of C, you have to
declare and define array before itdeclare and define array before it
can be used.can be used.
 Declaration and definition tell theDeclaration and definition tell the
compiler the name of the array,compiler the name of the array,
the data type of the elements,the data type of the elements,
and the size or number ofand the size or number of
elements.elements.
Syntax of an Array DeclarationSyntax of an Array Declaration
data-type array_name [size];data-type array_name [size];
 Data-typeData-type refers to the type of elementsrefers to the type of elements
you want to storeyou want to store
 sizesize is the number of elementsis the number of elements
Examples:Examples:
int char[80];int char[80];
floatfloat farr[500];farr[500];
static int iarr[80];static int iarr[80];
charchar charray[40];charray[40];
int ar[100];int ar[100];
 In the above figure, as each integer valueIn the above figure, as each integer value
occupies 2 bytes.occupies 2 bytes.
 200 bytes of consecutive memory200 bytes of consecutive memory
locations were allocated in the memory.locations were allocated in the memory.
2001 2003 2199
Points to RememberPoints to Remember
There are two things to remember forThere are two things to remember for
using arrays in C:using arrays in C:
 The amount of storage for a declaredThe amount of storage for a declared
array has to be specified atarray has to be specified at compile timecompile time
before execution. This means that anbefore execution. This means that an
array has a fixed size.array has a fixed size.
 The data type of an array appliesThe data type of an array applies
uniformly to all the elements; for thisuniformly to all the elements; for this
reason, an array is called areason, an array is called a
homogeneoushomogeneous data structure.data structure.
Use of Symbolic ConstantUse of Symbolic Constant
To declare size of the array it would be better to use theTo declare size of the array it would be better to use the
symbolic constant as shown below:symbolic constant as shown below:
#include< stdio.h >#include< stdio.h >
#define SIZE 100#define SIZE 100
main( )main( )
{{
int i = 0;int i = 0;
int stud_marks[SIZE];int stud_marks[SIZE];
for( i = 0;i<SIZE;i++)for( i = 0;i<SIZE;i++)
{{
printf (“Element no. =%d”,i+1);printf (“Element no. =%d”,i+1);
printf(“ Enter the value of the element:”);printf(“ Enter the value of the element:”);
scanf(“%d”,&stud_marks[i]);scanf(“%d”,&stud_marks[i]);
}}
Array InitializationArray Initialization
 Arrays can be initialized at the timeArrays can be initialized at the time
of declaration.of declaration.
The syntax is:The syntax is:
datatype array-name[ size ] = {val 1, val 2, .......val n};datatype array-name[ size ] = {val 1, val 2, .......val n};
ExamplesExamples
int digits [10] = {1,2,3,4,5,6,7,8,9,10};int digits [10] = {1,2,3,4,5,6,7,8,9,10};
int digits[ ] = {1,2,3,4,5,6,7,8,9,10};int digits[ ] = {1,2,3,4,5,6,7,8,9,10};
char thing[4] = “TIN”;char thing[4] = “TIN”;
char thing[ ] = “TIN”;char thing[ ] = “TIN”;
Note:Note: A special character called null character ‘ 0 ’,A special character called null character ‘ 0 ’,
implicitly suffixes every string.implicitly suffixes every string.
/* Linear Search*/
# include<stdio.h>
# define SIZE 05
main()
{
int i = 0;
int j;
int num_list[SIZE];
printf(“Enter any 5 numbers: n”);
for(i = 0;i<SIZE;i ++)
{ printf(“Element no=%d Value of the element=”,i+1);
scanf(“%d”,&num_list[i]); }
printf (“Enter the element to be searched:”);
scanf (“%d”,&j);
/* search using linear search */
for(i=0;i<SIZE;i++)
{
if(j == num_list[i])
{ printf(“The number exists in the list at position: %dn”,i+1);
break; }
}
}
Multidimensional ArraysMultidimensional Arrays
 In principle, there is no limit to theIn principle, there is no limit to the
number of subscripts (or dimensions)number of subscripts (or dimensions)
an array can have.an array can have.
 Arrays with more than oneArrays with more than one
dimension are calleddimension are called multi-multi-
dimensional arraysdimensional arrays..
SyntaxSyntax
datatypedatatype array_namearray_name[[size1size1][][size2size2];];
ExamplesExamples
float table[50][50];float table[50][50];
int a[100][50];int a[100][50];
char page[24][80];char page[24][80];
Illustration of a 3X3 ArrayIllustration of a 3X3 Array
Initialization of 2 Dimensional arraysInitialization of 2 Dimensional arrays
int table[2][3] = { 1,2,3,4,5,6 };int table[2][3] = { 1,2,3,4,5,6 };
OrOr
intint table[2][3] = { {1,2,3},table[2][3] = { {1,2,3},
{4,5,6} };{4,5,6} };
It means that element:It means that element:
table [ 0][0] = 1;table [ 0][0] = 1;
table [ 0][1] = 2;table [ 0][1] = 2;
table [ 0][2] = 3;table [ 0][2] = 3;
table [ 1][0] = 4;table [ 1][0] = 4;
table [ 1][1] = 5;table [ 1][1] = 5;
table [ 1][2] = 6;table [ 1][2] = 6;
Transpose of a MatrixTranspose of a Matrix
#include<stdio.h>#include<stdio.h>
#defind SIZE 3#defind SIZE 3
main()main()
{{
int mat[SIZE][SIZE];int mat[SIZE][SIZE];
int i,j;int i,j;
printf(“Enter the elements of the matrixn”);printf(“Enter the elements of the matrixn”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{
for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
{ scanf(“%d”,&mat[i][j]);{ scanf(“%d”,&mat[i][j]);
}}
}}
printf(“Transpose of the matrixn”);printf(“Transpose of the matrixn”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
{printf(“%d”,&mat[j][i]);{printf(“%d”,&mat[j][i]);
}}
printf(“n”);printf(“n”);
}}
}}
Addition of Two MatricesAddition of Two Matrices
#include<stdio.h>#include<stdio.h>
#defind SIZE 3#defind SIZE 3
main()main()
{{
int a[SIZE][SIZE], b[SIZE][SIZE];int a[SIZE][SIZE], b[SIZE][SIZE];
int i,j;int i,j;
printf(“Enter the elements of the matrix An”);printf(“Enter the elements of the matrix An”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{
for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
scanf(“%d”,&a[i][j]);scanf(“%d”,&a[i][j]);
}}
printf(“Enter the elements of the matrix B”);printf(“Enter the elements of the matrix B”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
scanf(“%d”,&b[i][j]);scanf(“%d”,&b[i][j]);
}}
Contd…Contd…
printf(“n Matrix Additionn”);printf(“n Matrix Additionn”);
for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++)
{{
for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++)
printf(“%d”,a[i][j]+b[i][j]);printf(“%d”,a[i][j]+b[i][j]);
printf(“n”);printf(“n”);
}}
}}
Passing Arrays to FunctionsPassing Arrays to Functions
 An entire array can be passed to aAn entire array can be passed to a
function as an argument.function as an argument.
 To pass an array to a function, theTo pass an array to a function, the
array must appear by itself, withoutarray must appear by itself, without
brackets or subscripts, as an actualbrackets or subscripts, as an actual
argument within the function call.argument within the function call.
 The size of the array is not specifiedThe size of the array is not specified
within the formal argumentwithin the formal argument
declaration.declaration.
IllustrationIllustration
#include<stdio.h>#include<stdio.h>
main()main()
{{
int n;int n;
float average(int a, float x[]);float average(int a, float x[]);
float avg;float avg;
float list[100];float list[100];
…………..
…………..
avg=average(n,list);avg=average(n,list);
……
……..
}}
float average(int a, float x[])float average(int a, float x[])
{{
………………..
}}

More Related Content

PPTX
Array in c language
PPTX
2D Array
DOC
Arrays and Strings
PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Array in c programming
PPTX
Pointers in C
PPTX
Array in C
Array in c language
2D Array
Arrays and Strings
Arrays 1D and 2D , and multi dimensional
Array in c programming
Pointers in C
Array in C

What's hot (20)

PPTX
Programming in c Arrays
PPT
Array in c
PDF
sparse matrix in data structure
PPTX
Basic Data Types in C++
PDF
Data types in c++
PPT
structure and union
PPTX
Abstract Data Types
PPT
File handling in c
PPTX
Arrays in c
PDF
Character Array and String
PPTX
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
PPTX
Introduction to Array ppt
PPTX
Presentation on array
PPTX
C Programming: Control Structure
PPTX
Circular link list.ppt
PPT
constants, variables and datatypes in C
PPTX
Dynamic memory allocation in c
PPTX
Control statements in c
Programming in c Arrays
Array in c
sparse matrix in data structure
Basic Data Types in C++
Data types in c++
structure and union
Abstract Data Types
File handling in c
Arrays in c
Character Array and String
Array Introduction One-dimensional array Multidimensional array
Introduction to Array ppt
Presentation on array
C Programming: Control Structure
Circular link list.ppt
constants, variables and datatypes in C
Dynamic memory allocation in c
Control statements in c
Ad

Similar to Arrays in c (20)

PPT
Arrays
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
PPTX
Arrays & Strings
PPTX
3.ArraysandPointers.pptx
PPTX
2 Arrays & Strings.pptx
PPTX
Array ppt you can learn in very few slides.
PPT
Arrays Basics
PPTX
Chapter 13.pptx
PPTX
PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
PDF
Array in C.pdf
PDF
Array.pdf
PPTX
PDF
Array
PPTX
BHARGAVIARRAY.PPT.pptx
PPTX
Basic array in c programming
PDF
Array&amp;string
PPTX
Module_3_Arrays - Updated.pptx............
PDF
Programming Fundamentals Arrays and Strings
PPTX
Unit4pptx__2024_11_ 11_10_16_09.pptx
Arrays
Array THE DATA STRUCTURE. ITS THE STRUCT
Arrays & Strings
3.ArraysandPointers.pptx
2 Arrays & Strings.pptx
Array ppt you can learn in very few slides.
Arrays Basics
Chapter 13.pptx
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
Array in C.pdf
Array.pdf
Array
BHARGAVIARRAY.PPT.pptx
Basic array in c programming
Array&amp;string
Module_3_Arrays - Updated.pptx............
Programming Fundamentals Arrays and Strings
Unit4pptx__2024_11_ 11_10_16_09.pptx
Ad

More from vampugani (19)

PPTX
Social media presentation
PPTX
Creating Quick Response(QR) Codes for the OER
PPTX
Arithmetic Computation using 2's Complement Notation
PPTX
Post Graduate Diploma in Computer Applications (PGDCA)
PPTX
Overview of Distributed Systems
PPT
Protection and Security in Operating Systems
PPT
Virtual Memory
PPT
Memory Management in OS
PPT
Process Scheduling
PPT
Processes
PPT
Introduction to OS
PPT
Operating Systems
PPT
Distributed Systems
PPT
Multiprocessor Systems
PPT
File Management in Operating Systems
PPT
Strings in c
PPT
Control statements and functions in c
PPT
Introduction to C Programming
PPT
Introduction to C Programming - I
Social media presentation
Creating Quick Response(QR) Codes for the OER
Arithmetic Computation using 2's Complement Notation
Post Graduate Diploma in Computer Applications (PGDCA)
Overview of Distributed Systems
Protection and Security in Operating Systems
Virtual Memory
Memory Management in OS
Process Scheduling
Processes
Introduction to OS
Operating Systems
Distributed Systems
Multiprocessor Systems
File Management in Operating Systems
Strings in c
Control statements and functions in c
Introduction to C Programming
Introduction to C Programming - I

Recently uploaded (20)

PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Classroom Observation Tools for Teachers
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PPTX
Lesson notes of climatology university.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
master seminar digital applications in india
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Complications of Minimal Access Surgery at WLH
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Classroom Observation Tools for Teachers
LDMMIA Reiki Yoga Finals Review Spring Summer
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
01-Introduction-to-Information-Management.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
Lesson notes of climatology university.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Chinmaya Tiranga quiz Grand Finale.pdf
master seminar digital applications in india
Anesthesia in Laparoscopic Surgery in India
Final Presentation General Medicine 03-08-2024.pptx
Complications of Minimal Access Surgery at WLH
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
A systematic review of self-coping strategies used by university students to ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Paper A Mock Exam 9_ Attempt review.pdf.

Arrays in c

  • 1. ArraysArrays inin C ProgrammingC Programming V.V. SubrahmanyamV.V. Subrahmanyam SOCIS, IGNOUSOCIS, IGNOU Date: 24-05-08Date: 24-05-08 Time: 11-00 to 11-45Time: 11-00 to 11-45
  • 2. IntroductionIntroduction  Many programs require theMany programs require the processing of multiple, related dataprocessing of multiple, related data items that have commonitems that have common characteristics likecharacteristics like list of numbers,list of numbers, marks in a course etc..marks in a course etc..
  • 3. ExampleExample  Consider to store marks of fiveConsider to store marks of five students. They can be stored usingstudents. They can be stored using five variables as follows:five variables as follows: int ml,m2,m3,m4,m5;int ml,m2,m3,m4,m5; Now, if we want to do the same thingNow, if we want to do the same thing for 100 students in a class then onefor 100 students in a class then one will find it difficult to handle 100will find it difficult to handle 100 variables.variables.
  • 4.  In such situations it is oftenIn such situations it is often convenient to place the dataconvenient to place the data items into an array, where theyitems into an array, where they will share the same name with awill share the same name with a subscript.subscript.
  • 5. Characteristic Features of an ArrayCharacteristic Features of an Array  An array is a collection of similarAn array is a collection of similar kind of data elements stored inkind of data elements stored in adjacent memory locations and areadjacent memory locations and are referred to by a single array-name.referred to by a single array-name.  Arrays are defined in much theArrays are defined in much the same manner as ordinary variables,same manner as ordinary variables, except that each array name mustexcept that each array name must be accompanied by a sizebe accompanied by a size specification.specification.
  • 6. Contd…Contd…  In the case of C, you have toIn the case of C, you have to declare and define array before itdeclare and define array before it can be used.can be used.  Declaration and definition tell theDeclaration and definition tell the compiler the name of the array,compiler the name of the array, the data type of the elements,the data type of the elements, and the size or number ofand the size or number of elements.elements.
  • 7. Syntax of an Array DeclarationSyntax of an Array Declaration data-type array_name [size];data-type array_name [size];  Data-typeData-type refers to the type of elementsrefers to the type of elements you want to storeyou want to store  sizesize is the number of elementsis the number of elements Examples:Examples: int char[80];int char[80]; floatfloat farr[500];farr[500]; static int iarr[80];static int iarr[80]; charchar charray[40];charray[40];
  • 8. int ar[100];int ar[100];  In the above figure, as each integer valueIn the above figure, as each integer value occupies 2 bytes.occupies 2 bytes.  200 bytes of consecutive memory200 bytes of consecutive memory locations were allocated in the memory.locations were allocated in the memory. 2001 2003 2199
  • 9. Points to RememberPoints to Remember There are two things to remember forThere are two things to remember for using arrays in C:using arrays in C:  The amount of storage for a declaredThe amount of storage for a declared array has to be specified atarray has to be specified at compile timecompile time before execution. This means that anbefore execution. This means that an array has a fixed size.array has a fixed size.  The data type of an array appliesThe data type of an array applies uniformly to all the elements; for thisuniformly to all the elements; for this reason, an array is called areason, an array is called a homogeneoushomogeneous data structure.data structure.
  • 10. Use of Symbolic ConstantUse of Symbolic Constant To declare size of the array it would be better to use theTo declare size of the array it would be better to use the symbolic constant as shown below:symbolic constant as shown below: #include< stdio.h >#include< stdio.h > #define SIZE 100#define SIZE 100 main( )main( ) {{ int i = 0;int i = 0; int stud_marks[SIZE];int stud_marks[SIZE]; for( i = 0;i<SIZE;i++)for( i = 0;i<SIZE;i++) {{ printf (“Element no. =%d”,i+1);printf (“Element no. =%d”,i+1); printf(“ Enter the value of the element:”);printf(“ Enter the value of the element:”); scanf(“%d”,&stud_marks[i]);scanf(“%d”,&stud_marks[i]); }}
  • 11. Array InitializationArray Initialization  Arrays can be initialized at the timeArrays can be initialized at the time of declaration.of declaration. The syntax is:The syntax is: datatype array-name[ size ] = {val 1, val 2, .......val n};datatype array-name[ size ] = {val 1, val 2, .......val n};
  • 12. ExamplesExamples int digits [10] = {1,2,3,4,5,6,7,8,9,10};int digits [10] = {1,2,3,4,5,6,7,8,9,10}; int digits[ ] = {1,2,3,4,5,6,7,8,9,10};int digits[ ] = {1,2,3,4,5,6,7,8,9,10}; char thing[4] = “TIN”;char thing[4] = “TIN”; char thing[ ] = “TIN”;char thing[ ] = “TIN”; Note:Note: A special character called null character ‘ 0 ’,A special character called null character ‘ 0 ’, implicitly suffixes every string.implicitly suffixes every string.
  • 13. /* Linear Search*/ # include<stdio.h> # define SIZE 05 main() { int i = 0; int j; int num_list[SIZE]; printf(“Enter any 5 numbers: n”); for(i = 0;i<SIZE;i ++) { printf(“Element no=%d Value of the element=”,i+1); scanf(“%d”,&num_list[i]); } printf (“Enter the element to be searched:”); scanf (“%d”,&j); /* search using linear search */ for(i=0;i<SIZE;i++) { if(j == num_list[i]) { printf(“The number exists in the list at position: %dn”,i+1); break; } } }
  • 14. Multidimensional ArraysMultidimensional Arrays  In principle, there is no limit to theIn principle, there is no limit to the number of subscripts (or dimensions)number of subscripts (or dimensions) an array can have.an array can have.  Arrays with more than oneArrays with more than one dimension are calleddimension are called multi-multi- dimensional arraysdimensional arrays..
  • 16. ExamplesExamples float table[50][50];float table[50][50]; int a[100][50];int a[100][50]; char page[24][80];char page[24][80];
  • 17. Illustration of a 3X3 ArrayIllustration of a 3X3 Array
  • 18. Initialization of 2 Dimensional arraysInitialization of 2 Dimensional arrays int table[2][3] = { 1,2,3,4,5,6 };int table[2][3] = { 1,2,3,4,5,6 }; OrOr intint table[2][3] = { {1,2,3},table[2][3] = { {1,2,3}, {4,5,6} };{4,5,6} }; It means that element:It means that element: table [ 0][0] = 1;table [ 0][0] = 1; table [ 0][1] = 2;table [ 0][1] = 2; table [ 0][2] = 3;table [ 0][2] = 3; table [ 1][0] = 4;table [ 1][0] = 4; table [ 1][1] = 5;table [ 1][1] = 5; table [ 1][2] = 6;table [ 1][2] = 6;
  • 19. Transpose of a MatrixTranspose of a Matrix #include<stdio.h>#include<stdio.h> #defind SIZE 3#defind SIZE 3 main()main() {{ int mat[SIZE][SIZE];int mat[SIZE][SIZE]; int i,j;int i,j; printf(“Enter the elements of the matrixn”);printf(“Enter the elements of the matrixn”); for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++) {{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++) { scanf(“%d”,&mat[i][j]);{ scanf(“%d”,&mat[i][j]); }} }} printf(“Transpose of the matrixn”);printf(“Transpose of the matrixn”); for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++) {{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++) {printf(“%d”,&mat[j][i]);{printf(“%d”,&mat[j][i]); }} printf(“n”);printf(“n”); }} }}
  • 20. Addition of Two MatricesAddition of Two Matrices #include<stdio.h>#include<stdio.h> #defind SIZE 3#defind SIZE 3 main()main() {{ int a[SIZE][SIZE], b[SIZE][SIZE];int a[SIZE][SIZE], b[SIZE][SIZE]; int i,j;int i,j; printf(“Enter the elements of the matrix An”);printf(“Enter the elements of the matrix An”); for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++) {{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++) scanf(“%d”,&a[i][j]);scanf(“%d”,&a[i][j]); }} printf(“Enter the elements of the matrix B”);printf(“Enter the elements of the matrix B”); for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++) {{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++) scanf(“%d”,&b[i][j]);scanf(“%d”,&b[i][j]); }}
  • 21. Contd…Contd… printf(“n Matrix Additionn”);printf(“n Matrix Additionn”); for(i=0;i<SIZE;i++)for(i=0;i<SIZE;i++) {{ for (j=0;j<SIZE;j++)for (j=0;j<SIZE;j++) printf(“%d”,a[i][j]+b[i][j]);printf(“%d”,a[i][j]+b[i][j]); printf(“n”);printf(“n”); }} }}
  • 22. Passing Arrays to FunctionsPassing Arrays to Functions  An entire array can be passed to aAn entire array can be passed to a function as an argument.function as an argument.  To pass an array to a function, theTo pass an array to a function, the array must appear by itself, withoutarray must appear by itself, without brackets or subscripts, as an actualbrackets or subscripts, as an actual argument within the function call.argument within the function call.  The size of the array is not specifiedThe size of the array is not specified within the formal argumentwithin the formal argument declaration.declaration.
  • 23. IllustrationIllustration #include<stdio.h>#include<stdio.h> main()main() {{ int n;int n; float average(int a, float x[]);float average(int a, float x[]); float avg;float avg; float list[100];float list[100]; ………….. ………….. avg=average(n,list);avg=average(n,list); …… …….. }} float average(int a, float x[])float average(int a, float x[]) {{ ……………….. }}