SlideShare a Scribd company logo
1 of 23
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

What's hot

What's hot (20)

concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Data Types In C
Data Types In CData Types In C
Data Types In C
 
Array in C
Array in CArray in C
Array in C
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
Array and string
Array and stringArray and string
Array and string
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
ARRAY
ARRAYARRAY
ARRAY
 
Strings in C
Strings in CStrings in C
Strings in C
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Arrays
ArraysArrays
Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
Array
ArrayArray
Array
 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 

Similar to C Arrays Guide (20)

Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
SPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in CSPL 10 | One Dimensional Array in C
SPL 10 | One Dimensional Array in C
 
Array
ArrayArray
Array
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
C_Arrays.pptx
C_Arrays.pptxC_Arrays.pptx
C_Arrays.pptx
 
presentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.pptpresentation_arrays_1443553113_140676.ppt
presentation_arrays_1443553113_140676.ppt
 
6.array
6.array6.array
6.array
 
C data types, arrays and structs
C data types, arrays and structsC data types, arrays and structs
C data types, arrays and structs
 
Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays
ArraysArrays
Arrays
 
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
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2VIT351 Software Development VI Unit2
VIT351 Software Development VI Unit2
 
L5 array
L5 arrayL5 array
L5 array
 

More from vampugani

Social media presentation
Social media presentationSocial media presentation
Social media presentationvampugani
 
Creating Quick Response(QR) Codes for the OER
Creating Quick Response(QR) Codes for the OERCreating Quick Response(QR) Codes for the OER
Creating Quick Response(QR) Codes for the OERvampugani
 
Arithmetic Computation using 2's Complement Notation
Arithmetic Computation using 2's Complement NotationArithmetic Computation using 2's Complement Notation
Arithmetic Computation using 2's Complement Notationvampugani
 
Post Graduate Diploma in Computer Applications (PGDCA)
Post Graduate Diploma in Computer Applications (PGDCA)Post Graduate Diploma in Computer Applications (PGDCA)
Post Graduate Diploma in Computer Applications (PGDCA)vampugani
 
Overview of Distributed Systems
Overview of Distributed SystemsOverview of Distributed Systems
Overview of Distributed Systemsvampugani
 
Protection and Security in Operating Systems
Protection and Security in Operating SystemsProtection and Security in Operating Systems
Protection and Security in Operating Systemsvampugani
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memoryvampugani
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OSvampugani
 
Process Scheduling
Process SchedulingProcess Scheduling
Process Schedulingvampugani
 
Introduction to OS
Introduction to OSIntroduction to OS
Introduction to OSvampugani
 
Operating Systems
Operating SystemsOperating Systems
Operating Systemsvampugani
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systemsvampugani
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systemsvampugani
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systemsvampugani
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in cvampugani
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming vampugani
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I vampugani
 

More from vampugani (18)

Social media presentation
Social media presentationSocial media presentation
Social media presentation
 
Creating Quick Response(QR) Codes for the OER
Creating Quick Response(QR) Codes for the OERCreating Quick Response(QR) Codes for the OER
Creating Quick Response(QR) Codes for the OER
 
Arithmetic Computation using 2's Complement Notation
Arithmetic Computation using 2's Complement NotationArithmetic Computation using 2's Complement Notation
Arithmetic Computation using 2's Complement Notation
 
Post Graduate Diploma in Computer Applications (PGDCA)
Post Graduate Diploma in Computer Applications (PGDCA)Post Graduate Diploma in Computer Applications (PGDCA)
Post Graduate Diploma in Computer Applications (PGDCA)
 
Overview of Distributed Systems
Overview of Distributed SystemsOverview of Distributed Systems
Overview of Distributed Systems
 
Protection and Security in Operating Systems
Protection and Security in Operating SystemsProtection and Security in Operating Systems
Protection and Security in Operating Systems
 
Virtual Memory
Virtual MemoryVirtual Memory
Virtual Memory
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Process Scheduling
Process SchedulingProcess Scheduling
Process Scheduling
 
Processes
ProcessesProcesses
Processes
 
Introduction to OS
Introduction to OSIntroduction to OS
Introduction to OS
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 
Distributed Systems
Distributed SystemsDistributed Systems
Distributed Systems
 
Multiprocessor Systems
Multiprocessor SystemsMultiprocessor Systems
Multiprocessor Systems
 
File Management in Operating Systems
File Management in Operating SystemsFile Management in Operating Systems
File Management in Operating Systems
 
Control statements and functions in c
Control statements and functions in cControl statements and functions in c
Control statements and functions in c
 
Introduction to C Programming
Introduction to C Programming Introduction to C Programming
Introduction to C Programming
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 

Recently uploaded

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 

Recently uploaded (20)

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........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
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 

C Arrays Guide

  • 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[]) {{ ……………….. }}