SlideShare a Scribd company logo
Submitted by,
T. Janani M.Sc (CS)
N. S. College Arts & Science
Arrays:
An array is a collection of data that holds
fixed number of values of same type.
The size and type of arrays cannot be
changed after its declaration.
For Example : If you want to store marks
of 100 students you can create an array for it.
float marks[100];
Some examples where the concept of an
array can be used:
ā€¢ 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.
ā€¢ Table of daily rainfall data.
How to declare an array in C?
Syntax:
data_type array_name[array_ size];
For example:
float mark[5];
Here we declared an array, mark, of
floating-point type and size 5. That it holds 5
floating-point values.
Elements of an array and How to access them?
You can access elements of an array by indices.
Suppose you declared an array mark as above. The first
element is mark[0], second element is mark[1] and so on.
Few Key points:
ā€¢ Arrays have 0 as the first index not 1. In this example,
mark[0].
ā€¢ If the size of an array is n, to access the last element, (n-1)
index is used. In this example, mark[4].
ā€¢ Suppose the starting address of mark[0] is 2120d. Then, the
next address, mark[1], will be 2124d, address of mark[2] will
be 2128d and so on. Its because the size of a float is 4 bytes.
How to Initialize an array?
Its possible to initialize an array during declaration.
For example:
int mark[5] = {9,4,6,3,5};
Another method of initialize array during declaration
int mark[ ] ={9,4,6,3,5};
Here,
mark[0] is equal to 9
mark[1] is equal to 4
mark[2] is equal to 6
mark[3] is equal to 3
mark[4] is equal to 5
Important thing to remember when working with
C arrays:
Suppose you declared an array of 10 elements. Lets say,
int testArray[10];
You can use the array members from testArray[0] to
testArray[9].
If you try to access array elements outside of its bound, lets
say testArray[12], the compiler may not show any error.
However, this may cause unexpected output (undefined
behavior).
Arrays are of three types:
1. One-dimensional arrays.
2. Two-dimensional arrays.
3. Multidimensional arrays.
One-dimensional Array:
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 a one dimensional array.
The Syntax for an array declaration is:
type variable-name[size];
Example:
float height[50];
int groupt[10];
char name[10];
The type specifies the type of the element that will be
contained in the array, such as int, float, or char and the size
indicates the maximum number of elements that can be stored
inside the array.
Now as we declare a array
int number[5];
Then the computer reserves five storage locations as
the size o the array is 5 as show below.
Reserved Space Storing values after Initialization
number[0] number[0]
number[1] number[1]
number[2] number[2]
number[3] number[3]
number[4] number[4]
35
20
40
57
19
Initialization of one dimensional array:
After an array is declared, its elements must be
initialized. In C programming an array can be initialized at
either of the following stages:
At compile time
At run time
Compile Time Initialization:
The general form of initialization of array is:
type array-name[size] ={list of values};
The values in the list are separated by commas.
For example:
int number[3] = {0,5,4};
will declare the variable ā€˜numberā€™ as an array of
size 3 and will assign the values to each elements.
If the number of values in the list is less than the
number of elements, then only that many elements will be
initialized.
The remaining elements will be set to zero
automatically.
Remember, if we have more initializers than the
declared size, the compiler will produce an error.
Run time initialization:
An array can also be explicitly initialized at run time.
For example consider the following segment of a c program.
for(i=0;i<10;i++)
{
scanf(ā€œ%dā€, &x[i]);
}
In the run time initialization of the arrays looping
statements are almost compulsory.
Looping statements are used to initialize the values of the
arrays one by one using assignment operator or through the
keyboard by the user.
One dimensional Array Program:
#include<stdio.h>
void main()
{
int array[5];
printf(ā€œEnter 5 numbers to store them in array nā€);
for(i=0;i<5;i++)
{
Scanf(ā€œ%dā€, &array[i]);
}
printf(ā€œElement in the array are: nā€);
For(i=0;i<5;i++)
{
printf(ā€œElement stored at a[%d]=%d nā€, i, array[i]);
}
getch();
}
Input:
Enter 5 elements in the array: 23 45 32 25 45
Output:
Element in the array are:
Element stored at a[0]:23
Element stored at a[0]:45
Element stored at a[0]:32
Element stored at a[0]:25
Element stored at a[0]:45
Two-dimensional Arrays:
The simplest form of multidimensional array is
the two-dimensional array. A two-dimensional array is, in
essence, a list of one-dimensional arrays.
To declare a two-dimensional integer array of
size[x][y], you would write something as follows:
type arrayName[x][y];
Where type can be any valid C data type and
arrayName will be a valid C identifier.
A two-dimensional array can be considered as
a table which will have x number o rows and y number o
columns.
A two-dimensional array a, which contains
three rows and four columns can be shown as follows.
Column 0 Column 1 Column 2 Column 3
Row 0
Row 1
Row 2
a[0][0] a[0][1]
a[0][2] a[0][3]
a[0][2] a[0][2] a[0][2] a[0][2]
a[0][2] a[0][2] a[0][2] a[0][2]
Thus, every element in the array a is identified
by an element name of the form a[i][j].
where ā€˜aā€™ is the name of the array, and ā€˜iā€™ and ā€˜jā€™
are the subscripts that uniquely identify each element in ā€˜aā€™.
Initializing Two-Dimensional Arrays:
Multidimensional arrays may be initialized by
specifying bracketed values for each row.
Following is an array with 3 rows and each row
has 4 columns.
int a[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
The nested braces, which indicate the intended
row, are optional. The following initialization is equivalent
to the previous example
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements:
An element in a two-dimensional array is
accessed by using the subscripts. i.e., row index and
column index of the array.
For Example:
int val = a[2][3];
The above statement will take the 4th element
from the 3rd row of the array.
Two-Dimensional Arrays program:
#include<stdio.h>
int main()
{
int a[5][2]={{0,0},{1,2},{2,4},{3,6},{4,8}};
int i,j;
for(i=0;i<5;i++)
{
for(j=0;j<2;j++)
{
printf(ā€œa[%d][%d] = %dnā€,i,j,a[i][j]);
}
}
return 0;
}
Output:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
Multi-Dimensional Arrays:
C programming language supports
multidimensional Arrays.
ā€¢ Multi dimensional arrays have more than one
subscript variables.
ā€¢ Multi dimensional array is also called as matrix.
ā€¢ Multi dimensional arrays are array o arrays.
Declaration of Multidimensional Array
A multidimensional array is declared using the
following syntax
Syntax:
data_type array_name[d1][d2][d3][d4]....[dn];
Above statement will declare an array on N
dimensions of name array_name, where each element of
array is of type data_type.
The maximum number of elements that can be
stored in a multi dimensional array array_name is size1 X
size2 X size3....sizeN.
For example:
char cube[50][60][30];
Multidimensional Array program:
#include<stdio.h>
int main()
{
int r,c,a[50][50],b[50][50],sum[50][50],i,j;
printf("tn Multi-dimensional array");
printf("n Enter the row matrix:");
scanf("%d",&r);
printf("n Enter the col matrix:");
scanf("%d",&c);
printf("n Element of A matrix");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("n a[%d][%d]:",i+1,j+1);
scanf("%d",&a[i][j]);
}
printf("n Element of B matrix");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("n b[%d][%d]:",i+1,j+1);
scanf("%d",&b[i][j]);
}
printf("n Addition of matrix");
for(i=0;i<r; i++)
{
for(j=0;j<c;j++)
sum[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<r;i++)
{
printf("n");
for(j=0;j<c;j++)
{
printf("t%d",sum[i][j]);
}
printf("nt");
}
if(j==c-1)
{
printf("n");
}
return 0;
}
Output
Multidimensional array
Enter the row matrix:2
Enter the col matrix:2
Element of A matrix
a[1][1]:1
a[1][2]:4
a[2][1]:6
a[2][2]:3
Element of B matrix
b[1][1]:1
b[1][2]:7
b[2][1]:3
b[2][2]:9
Addition of a matrix is
2 11
9 12
Programming in c Arrays

More Related Content

What's hot

Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
Ā 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
Ā 
String functions in C
String functions in CString functions in C
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
Ā 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav
Ā 
Arrays
ArraysArrays
Arrays
SARITHA REDDY
Ā 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming9
Ā 
C tokens
C tokensC tokens
C tokens
Manu1325
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
Ā 
Strings
StringsStrings
Strings
Mitali Chugh
Ā 
2D Array
2D Array 2D Array
2D Array
Ehatsham Riaz
Ā 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Venkata.Manish Reddy
Ā 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Sangani Ankur
Ā 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Sajid Hasan
Ā 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
Ā 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in JavaAbhilash Nair
Ā 
C string
C stringC string
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
Ā 
Strings in python
Strings in pythonStrings in python
Strings in python
Prabhakaran V M
Ā 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
Ā 

What's hot (20)

Presentation on array
Presentation on array Presentation on array
Presentation on array
Ā 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
Ā 
String functions in C
String functions in CString functions in C
String functions in C
Ā 
Functions in C
Functions in CFunctions in C
Functions in C
Ā 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
Ā 
Arrays
ArraysArrays
Arrays
Ā 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
Ā 
C tokens
C tokensC tokens
C tokens
Ā 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Ā 
Strings
StringsStrings
Strings
Ā 
2D Array
2D Array 2D Array
2D Array
Ā 
Data types in c++
Data types in c++Data types in c++
Data types in c++
Ā 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
Ā 
Basic array in c programming
Basic array in c programmingBasic array in c programming
Basic array in c programming
Ā 
Functions in c language
Functions in c language Functions in c language
Functions in c language
Ā 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Ā 
C string
C stringC string
C string
Ā 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Ā 
Strings in python
Strings in pythonStrings in python
Strings in python
Ā 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Ā 

Similar to Programming in c Arrays

Arrays
ArraysArrays
Arrays
Steven Wallach
Ā 
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
ajajkhan16
Ā 
Array.pptx
Array.pptxArray.pptx
Array.pptx
fixocin377
Ā 
Arrays
ArraysArrays
Array
ArrayArray
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
Ā 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
Ā 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
Ā 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
Ā 
2 arrays
2   arrays2   arrays
2 arraystrixiacruz
Ā 
ARRAYS
ARRAYSARRAYS
ARRAYS
muniryaseen
Ā 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Uma mohan
Ā 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
Shri Shankaracharya College, Bhilai,Junwani
Ā 
Array in C
Array in CArray in C
Array in C
adityas29
Ā 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
Ā 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
Ā 
Array
ArrayArray
Arrayhjasjhd
Ā 
Arrays
ArraysArrays
Arrays
Trupti Agrawal
Ā 

Similar to Programming in c Arrays (20)

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
Ā 
Array.pptx
Array.pptxArray.pptx
Array.pptx
Ā 
Arrays
ArraysArrays
Arrays
Ā 
Array
ArrayArray
Array
Ā 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
Ā 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
Ā 
Arrays In C
Arrays In CArrays In C
Arrays In C
Ā 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Ā 
2 arrays
2   arrays2   arrays
2 arrays
Ā 
ARRAYS
ARRAYSARRAYS
ARRAYS
Ā 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Ā 
Arrays
ArraysArrays
Arrays
Ā 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
Ā 
Ch08
Ch08Ch08
Ch08
Ā 
Array in C
Array in CArray in C
Array in C
Ā 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Ā 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Ā 
Array
ArrayArray
Array
Ā 
Arrays
ArraysArrays
Arrays
Ā 

More from janani thirupathi

Networks
NetworksNetworks
Networks
janani thirupathi
Ā 
Multimedia
MultimediaMultimedia
Multimedia
janani thirupathi
Ā 
Data structure
Data structureData structure
Data structure
janani thirupathi
Ā 
Java
JavaJava
Dip
Dip Dip
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
janani thirupathi
Ā 
data generalization and summarization
data generalization and summarization data generalization and summarization
data generalization and summarization
janani thirupathi
Ā 
Data warehouse architecture
Data warehouse architecture Data warehouse architecture
Data warehouse architecture
janani thirupathi
Ā 
Evolution of os
Evolution of osEvolution of os
Evolution of os
janani thirupathi
Ā 
B tree
B  treeB  tree
File sharing
File sharingFile sharing
File sharing
janani thirupathi
Ā 
Data transfer and manipulation
Data transfer and manipulationData transfer and manipulation
Data transfer and manipulation
janani thirupathi
Ā 
Arithmetic Logic
Arithmetic LogicArithmetic Logic
Arithmetic Logic
janani thirupathi
Ā 
Transaction management
Transaction managementTransaction management
Transaction management
janani thirupathi
Ā 
Memory System
Memory SystemMemory System
Memory System
janani thirupathi
Ā 
Cn assignment
Cn assignmentCn assignment
Cn assignment
janani thirupathi
Ā 
Narrowband ISDN
Narrowband ISDNNarrowband ISDN
Narrowband ISDN
janani thirupathi
Ā 

More from janani thirupathi (17)

Networks
NetworksNetworks
Networks
Ā 
Multimedia
MultimediaMultimedia
Multimedia
Ā 
Data structure
Data structureData structure
Data structure
Ā 
Java
JavaJava
Java
Ā 
Dip
Dip Dip
Dip
Ā 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
Ā 
data generalization and summarization
data generalization and summarization data generalization and summarization
data generalization and summarization
Ā 
Data warehouse architecture
Data warehouse architecture Data warehouse architecture
Data warehouse architecture
Ā 
Evolution of os
Evolution of osEvolution of os
Evolution of os
Ā 
B tree
B  treeB  tree
B tree
Ā 
File sharing
File sharingFile sharing
File sharing
Ā 
Data transfer and manipulation
Data transfer and manipulationData transfer and manipulation
Data transfer and manipulation
Ā 
Arithmetic Logic
Arithmetic LogicArithmetic Logic
Arithmetic Logic
Ā 
Transaction management
Transaction managementTransaction management
Transaction management
Ā 
Memory System
Memory SystemMemory System
Memory System
Ā 
Cn assignment
Cn assignmentCn assignment
Cn assignment
Ā 
Narrowband ISDN
Narrowband ISDNNarrowband ISDN
Narrowband ISDN
Ā 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
Ā 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
Ā 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
Ā 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
Ā 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
Ā 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
Ā 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
Ā 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
Ā 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
Ā 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
Ā 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
Ā 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
Ā 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
Ā 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
Ā 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
Ā 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
Ā 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
Ā 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
Ā 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
Ā 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
Ā 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Ā 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Ā 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Ā 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
Ā 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
Ā 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
Ā 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Ā 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
Ā 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Ā 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
Ā 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Ā 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Ā 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Ā 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ā 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Ā 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Ā 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Ā 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Ā 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Ā 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Ā 

Programming in c Arrays

  • 1. Submitted by, T. Janani M.Sc (CS) N. S. College Arts & Science
  • 2. Arrays: An array is a collection of data that holds fixed number of values of same type. The size and type of arrays cannot be changed after its declaration. For Example : If you want to store marks of 100 students you can create an array for it. float marks[100];
  • 3. Some examples where the concept of an array can be used: ā€¢ 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. ā€¢ Table of daily rainfall data.
  • 4. How to declare an array in C? Syntax: data_type array_name[array_ size]; For example: float mark[5]; Here we declared an array, mark, of floating-point type and size 5. That it holds 5 floating-point values.
  • 5. Elements of an array and How to access them? You can access elements of an array by indices. Suppose you declared an array mark as above. The first element is mark[0], second element is mark[1] and so on. Few Key points: ā€¢ Arrays have 0 as the first index not 1. In this example, mark[0]. ā€¢ If the size of an array is n, to access the last element, (n-1) index is used. In this example, mark[4]. ā€¢ Suppose the starting address of mark[0] is 2120d. Then, the next address, mark[1], will be 2124d, address of mark[2] will be 2128d and so on. Its because the size of a float is 4 bytes.
  • 6. How to Initialize an array? Its possible to initialize an array during declaration. For example: int mark[5] = {9,4,6,3,5}; Another method of initialize array during declaration int mark[ ] ={9,4,6,3,5}; Here, mark[0] is equal to 9 mark[1] is equal to 4 mark[2] is equal to 6 mark[3] is equal to 3 mark[4] is equal to 5
  • 7. Important thing to remember when working with C arrays: Suppose you declared an array of 10 elements. Lets say, int testArray[10]; You can use the array members from testArray[0] to testArray[9]. If you try to access array elements outside of its bound, lets say testArray[12], the compiler may not show any error. However, this may cause unexpected output (undefined behavior).
  • 8. Arrays are of three types: 1. One-dimensional arrays. 2. Two-dimensional arrays. 3. Multidimensional arrays.
  • 9. One-dimensional Array: 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 a one dimensional array. The Syntax for an array declaration is: type variable-name[size]; Example: float height[50]; int groupt[10]; char name[10];
  • 10. The type specifies the type of the element that will be contained in the array, such as int, float, or char and the size indicates the maximum number of elements that can be stored inside the array. Now as we declare a array int number[5]; Then the computer reserves five storage locations as the size o the array is 5 as show below. Reserved Space Storing values after Initialization number[0] number[0] number[1] number[1] number[2] number[2] number[3] number[3] number[4] number[4] 35 20 40 57 19
  • 11. Initialization of one dimensional array: After an array is declared, its elements must be initialized. In C programming an array can be initialized at either of the following stages: At compile time At run time
  • 12. Compile Time Initialization: The general form of initialization of array is: type array-name[size] ={list of values}; The values in the list are separated by commas. For example: int number[3] = {0,5,4}; will declare the variable ā€˜numberā€™ as an array of size 3 and will assign the values to each elements. If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to zero automatically. Remember, if we have more initializers than the declared size, the compiler will produce an error.
  • 13. Run time initialization: An array can also be explicitly initialized at run time. For example consider the following segment of a c program. for(i=0;i<10;i++) { scanf(ā€œ%dā€, &x[i]); } In the run time initialization of the arrays looping statements are almost compulsory. Looping statements are used to initialize the values of the arrays one by one using assignment operator or through the keyboard by the user.
  • 14. One dimensional Array Program: #include<stdio.h> void main() { int array[5]; printf(ā€œEnter 5 numbers to store them in array nā€); for(i=0;i<5;i++) { Scanf(ā€œ%dā€, &array[i]); } printf(ā€œElement in the array are: nā€); For(i=0;i<5;i++) { printf(ā€œElement stored at a[%d]=%d nā€, i, array[i]); } getch(); }
  • 15. Input: Enter 5 elements in the array: 23 45 32 25 45 Output: Element in the array are: Element stored at a[0]:23 Element stored at a[0]:45 Element stored at a[0]:32 Element stored at a[0]:25 Element stored at a[0]:45
  • 16. Two-dimensional Arrays: The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size[x][y], you would write something as follows: type arrayName[x][y]; Where type can be any valid C data type and arrayName will be a valid C identifier.
  • 17. A two-dimensional array can be considered as a table which will have x number o rows and y number o columns. A two-dimensional array a, which contains three rows and four columns can be shown as follows. Column 0 Column 1 Column 2 Column 3 Row 0 Row 1 Row 2 a[0][0] a[0][1] a[0][2] a[0][3] a[0][2] a[0][2] a[0][2] a[0][2] a[0][2] a[0][2] a[0][2] a[0][2]
  • 18. Thus, every element in the array a is identified by an element name of the form a[i][j]. where ā€˜aā€™ is the name of the array, and ā€˜iā€™ and ā€˜jā€™ are the subscripts that uniquely identify each element in ā€˜aā€™. Initializing Two-Dimensional Arrays: Multidimensional arrays may be initialized by specifying bracketed values for each row.
  • 19. Following is an array with 3 rows and each row has 4 columns. int a[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }; The nested braces, which indicate the intended row, are optional. The following initialization is equivalent to the previous example int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
  • 20. Accessing Two-Dimensional Array Elements: An element in a two-dimensional array is accessed by using the subscripts. i.e., row index and column index of the array. For Example: int val = a[2][3]; The above statement will take the 4th element from the 3rd row of the array.
  • 21. Two-Dimensional Arrays program: #include<stdio.h> int main() { int a[5][2]={{0,0},{1,2},{2,4},{3,6},{4,8}}; int i,j; for(i=0;i<5;i++) { for(j=0;j<2;j++) { printf(ā€œa[%d][%d] = %dnā€,i,j,a[i][j]); } } return 0; }
  • 22. Output: a[0][0]: 0 a[0][1]: 0 a[1][0]: 1 a[1][1]: 2 a[2][0]: 2 a[2][1]: 4 a[3][0]: 3 a[3][1]: 6 a[4][0]: 4 a[4][1]: 8
  • 23. Multi-Dimensional Arrays: C programming language supports multidimensional Arrays. ā€¢ Multi dimensional arrays have more than one subscript variables. ā€¢ Multi dimensional array is also called as matrix. ā€¢ Multi dimensional arrays are array o arrays.
  • 24. Declaration of Multidimensional Array A multidimensional array is declared using the following syntax Syntax: data_type array_name[d1][d2][d3][d4]....[dn]; Above statement will declare an array on N dimensions of name array_name, where each element of array is of type data_type. The maximum number of elements that can be stored in a multi dimensional array array_name is size1 X size2 X size3....sizeN. For example: char cube[50][60][30];
  • 25. Multidimensional Array program: #include<stdio.h> int main() { int r,c,a[50][50],b[50][50],sum[50][50],i,j; printf("tn Multi-dimensional array"); printf("n Enter the row matrix:"); scanf("%d",&r); printf("n Enter the col matrix:"); scanf("%d",&c); printf("n Element of A matrix"); for(i=0;i<r;++i) for(j=0;j<c;++j)
  • 26. { printf("n a[%d][%d]:",i+1,j+1); scanf("%d",&a[i][j]); } printf("n Element of B matrix"); for(i=0;i<r;++i) for(j=0;j<c;++j) { printf("n b[%d][%d]:",i+1,j+1); scanf("%d",&b[i][j]); } printf("n Addition of matrix"); for(i=0;i<r; i++)
  • 29. Output Multidimensional array Enter the row matrix:2 Enter the col matrix:2 Element of A matrix a[1][1]:1 a[1][2]:4 a[2][1]:6 a[2][2]:3
  • 30. Element of B matrix b[1][1]:1 b[1][2]:7 b[2][1]:3 b[2][2]:9 Addition of a matrix is 2 11 9 12