SlideShare a Scribd company logo
1 of 35
Array
• a fixed-size sequential collection of elements of the same type
• a collection of variables of the same type
• arrays consist of contiguous memory locations. The lowest address corresponds to
the first element and the highest address to the last element.
• An array is a collection of similar data items stored in continuous memory
locations with single name.
Array indexes always begin with 0. Hence when we say array of size 10, array has
elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11],
intArr[200], the C compiler will not show any error, but we will get run time errors
while executing the program
Characteristics of Array
• An array holds elements that have the same data type.
• Array elements are stored in contiguous memory locations.
• All the elements of an array share the same name.
• Array name represents the address of the starting element.
Advantage of Array
• Using arrays, other data structures like linked lists, stacks, queues, trees, graphs
etc can be implemented.
• Two-dimensional arrays are used to represent matrices.
• Arrays represent multiple data items of the same type using a single name.
• In arrays, the elements can be accessed randomly by using the index number.
• It is better and convenient way of storing the data of same datatype with same
size.
• It allows us to store known number of elements in it.
• It allocates memory in contiguous memory locations for its elements. It does not
allocate any extra space/ memory for its elements. Hence there is no memory
overflow or shortage of memory in arrays.
Disadvantage of Array
• The number of elements to be stored in an array should be known in advance.
• An array is a static structure (which means the array is of fixed size). Once
declared the size of the array cannot be modified. The memory which is allocated
to it cannot be increased or decreased.
• Insertion and deletion are quite difficult in an array as the elements are stored in
consecutive memory locations and the shifting operation is costly.
• Allocating more memory than the requirement leads to wastage of memory space
and less allocation of memory also leads to a problem.
• It is not possible to hold dissimilar data type.
One Dimensional
Multi Dimensional
Types of Array
One Dimensional Array
• single dimensional arrays are used to store a row of values. In single dimensional
array, data is stored in linear form.
• Single dimensional arrays are also called as one-dimensional arrays, Linear
Arrays or simply 1-D Arrays.
• An array which has only one subscript is termed as one dimensional array.
• For example:
• Char name[25];
Declaration of one dimensional array
• Data_type array_name[array_size];
The type of array
elements
The name of array
variable
The number of
elements that can
be stored
For Example: declaration of one dimensional array
Syntax:
data_type array_name[array_size];
Example:
char name[25];
int a[10];
float temperature[25];
float marks[25];
char fname[20];
Array Initialization :One dimensional array
• Syntax:
Data_type array_name[array_size]={val1,val2,val3,………,valn};
For example:
int marks[4]={76,78,98,55};
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
#include <stdio.h>
void main()
{
int arr[10];
int i;
printf("Input 10 elements in the
array :n");
for(i=0; i<10; i++)
{
printf("element - %d : ",i);
scanf("%d", &arr[i]);
}
printf("nElements in array are:
");
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
printf("n");
}
WAP to input 10 numbers in array and display them
WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY
THOSE PERSONS WHOSE BETWEEN 50 AND 60.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,age[100],count=0;
printf("Enter the number of persons ::
");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
printf("nEnter age of %d persons ::
",i);
scanf("%d",&age[i]);
}
for (i=1;i<=n;i++)
{
if(age[i]>50 && age[i] < 60)
count++;
}
printf("nnNumber of persons whose
age between 50-60 are :: %d",count);
getch();
}
WAP to input ‘n’ numbers and sort them in ascending order
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N n");
scanf("%d", &n);
printf("Enter the numbers n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}}}
printf("The numbers arranged in ascending
order are given below n");
for (i = 0; i < n; ++i)
printf("%dn", number[i]);
}
Array to have sum of numbers
#include <conio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
sum+=a[i];
}
printf("sum of array is : %d",sum);
return 0;
}
#include <stdio.h>
int main()
{
int n, r = 0, t;
printf("Enter a number to check if
it's a palindrome or notn");
scanf("%d", &n);
t = n;
while (t != 0)
{
r = t%10;
r = r + t*10;
t = t/10;
}
if (n == r)
printf("%d is a palindrome
number.n", n);
else
printf("%d isn't a palindrome
number.n", n);
return 0;
}
Palindrome
Multi Dimensional Array
• An array of arrays is called as multi dimensional array. In simple words, an
array created with more than one dimension (size) is called as multi dimensional
array.
• Multi dimensional array can be of two dimensional array or three
dimensional array or four dimensional array or more...
• Most popular and commonly used multi dimensional array is two dimensional
array. The 2-D arrays are used to store data in the form of table. We also use
2-D arrays to create mathematical matrices.
• The maximum capacity of elements of array is the product of row size and column
size.
Declaration of multi dimensional array
Data_type array_Name [ row_Size ] [ column_Size ] ;
For example:
int a[m][n];
Here a[0][0] is initial and a[m-1][n-1] is the last element.
Initialization of multi dimensional array
2-D array can be initialized in a way similar to that of 1-D array. for example:-
int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};
These values are assigned to the elements row wise, so the values of
elements after this initialization are
Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20
Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21
Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22
• While initializing we can group the elements row wise using inner
braces.
for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};
• If we initialize an array as
int mat[4][3]={{11},{12,13},{14,15,16},{17}};
Then the compiler will assume its all rest value as 0,which are not defined.
Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17
Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0
Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0
WAP to input data in 2-dimensional array and display in matrix form
#include<stdio.h>
int main(){
int disp[2][3];
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:",
i, j);
scanf("%d", &disp[i][j]);
} }
//Displaying array elements
printf("Two Dimensional array
elements:n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("n");
}}}
return 0;
}
Transpose of a matrix: 2-dimensional array
#include <stdio.h>
void main()
{
int array[10][10];
int i, j, m, n;
printf("Enter the order of the matrix n");
scanf("%d %d", &m, &n);
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("Transpose of matrix is n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
printf(" %d", array[i][j]);
}
printf("n");
}
}
WAP to add two matrices
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
clrscr();
printf("nENTER VALUES FOR
MATRIX A:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("nENTER VALUES FOR
MATRIX B:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("nTHE VALUES OF
MATRIX C ARE:n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
printf("%5d",c[i][j]);
printf("n");
getch();
Assignments
• WAP to find transpose of 3*3 matrix.
• WAP to subtract two 3*3 matrices.
• WAP to multiply two 2*2 matrices.
• WAP to take salary of 100 employee and sort them on the basis of
ascending order.
• WAP to take ‘n’ input and find greatest and least number among
them.
• WAP to input marks of 20 students and display their sum and
average marks.
WAP to multiply two 2*2 matrices
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10],
multiply[10][10];
printf("Enter number of rows and
columns of first matrixn");
scanf("%d%d", &m, &n);
printf("Enter elements of first
matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter number of rows and
columns of second matrixn");
scanf("%d%d", &p, &q);
printf("Enter elements of second
matrixn");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum +
first[c][k]*second[k][d];
multiply[c][d] = sum;
sum = 0;
}}
printf("Product of the matrices:n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%dt", multiply[c][d]);
printf("n");
}}
return 0;
}
Differences between one dimensional and two
dimensional array
Array
Array

More Related Content

What's hot

What's hot (20)

A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Arrays
ArraysArrays
Arrays
 
Array in c++
Array in c++Array in c++
Array in c++
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Array
ArrayArray
Array
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Array
ArrayArray
Array
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Data Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat SheetData Wrangling with dplyr and tidyr Cheat Sheet
Data Wrangling with dplyr and tidyr Cheat Sheet
 
Array in C
Array in CArray in C
Array in C
 
Java arrays
Java arraysJava arrays
Java arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 

Similar to Array

Similar to Array (20)

Arrays
ArraysArrays
Arrays
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
02 arrays
02 arrays02 arrays
02 arrays
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays
ArraysArrays
Arrays
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Array
ArrayArray
Array
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
Session 4
Session 4Session 4
Session 4
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
Arrays
ArraysArrays
Arrays
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
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
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 

Array

  • 1.
  • 2. Array • a fixed-size sequential collection of elements of the same type • a collection of variables of the same type • arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. • An array is a collection of similar data items stored in continuous memory locations with single name.
  • 3. Array indexes always begin with 0. Hence when we say array of size 10, array has elements from index 0 to 9. If we specify or use array as intArr[10], intArr[11], intArr[200], the C compiler will not show any error, but we will get run time errors while executing the program
  • 4.
  • 5. Characteristics of Array • An array holds elements that have the same data type. • Array elements are stored in contiguous memory locations. • All the elements of an array share the same name. • Array name represents the address of the starting element.
  • 6. Advantage of Array • Using arrays, other data structures like linked lists, stacks, queues, trees, graphs etc can be implemented. • Two-dimensional arrays are used to represent matrices. • Arrays represent multiple data items of the same type using a single name. • In arrays, the elements can be accessed randomly by using the index number. • It is better and convenient way of storing the data of same datatype with same size. • It allows us to store known number of elements in it. • It allocates memory in contiguous memory locations for its elements. It does not allocate any extra space/ memory for its elements. Hence there is no memory overflow or shortage of memory in arrays.
  • 7.
  • 8. Disadvantage of Array • The number of elements to be stored in an array should be known in advance. • An array is a static structure (which means the array is of fixed size). Once declared the size of the array cannot be modified. The memory which is allocated to it cannot be increased or decreased. • Insertion and deletion are quite difficult in an array as the elements are stored in consecutive memory locations and the shifting operation is costly. • Allocating more memory than the requirement leads to wastage of memory space and less allocation of memory also leads to a problem. • It is not possible to hold dissimilar data type.
  • 10. One Dimensional Array • single dimensional arrays are used to store a row of values. In single dimensional array, data is stored in linear form. • Single dimensional arrays are also called as one-dimensional arrays, Linear Arrays or simply 1-D Arrays. • An array which has only one subscript is termed as one dimensional array. • For example: • Char name[25];
  • 11. Declaration of one dimensional array • Data_type array_name[array_size]; The type of array elements The name of array variable The number of elements that can be stored
  • 12. For Example: declaration of one dimensional array Syntax: data_type array_name[array_size]; Example: char name[25]; int a[10]; float temperature[25]; float marks[25]; char fname[20];
  • 13. Array Initialization :One dimensional array • Syntax: Data_type array_name[array_size]={val1,val2,val3,………,valn}; For example: int marks[4]={76,78,98,55}; double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
  • 14. #include <stdio.h> void main() { int arr[10]; int i; printf("Input 10 elements in the array :n"); for(i=0; i<10; i++) { printf("element - %d : ",i); scanf("%d", &arr[i]); } printf("nElements in array are: "); for(i=0; i<10; i++) { printf("%d ", arr[i]); } printf("n"); } WAP to input 10 numbers in array and display them
  • 15. WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY THOSE PERSONS WHOSE BETWEEN 50 AND 60. #include<stdio.h> #include<conio.h> void main() { int i,n,age[100],count=0; printf("Enter the number of persons :: "); scanf("%d",&n); for (i=1;i<=n;i++) { printf("nEnter age of %d persons :: ",i); scanf("%d",&age[i]); } for (i=1;i<=n;i++) { if(age[i]>50 && age[i] < 60) count++; } printf("nnNumber of persons whose age between 50-60 are :: %d",count); getch(); }
  • 16. WAP to input ‘n’ numbers and sort them in ascending order #include <stdio.h> void main() { int i, j, a, n, number[30]; printf("Enter the value of N n"); scanf("%d", &n); printf("Enter the numbers n"); for (i = 0; i < n; ++i) scanf("%d", &number[i]); for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { if (number[i] > number[j]) { a = number[i]; number[i] = number[j]; number[j] = a; }}} printf("The numbers arranged in ascending order are given below n"); for (i = 0; i < n; ++i) printf("%dn", number[i]); }
  • 17. Array to have sum of numbers #include <conio.h> int main() { int a[1000],i,n,sum=0; printf("Enter size of the array : "); scanf("%d",&n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) { sum+=a[i]; } printf("sum of array is : %d",sum); return 0; }
  • 18.
  • 19. #include <stdio.h> int main() { int n, r = 0, t; printf("Enter a number to check if it's a palindrome or notn"); scanf("%d", &n); t = n; while (t != 0) { r = t%10; r = r + t*10; t = t/10; } if (n == r) printf("%d is a palindrome number.n", n); else printf("%d isn't a palindrome number.n", n); return 0; } Palindrome
  • 20. Multi Dimensional Array • An array of arrays is called as multi dimensional array. In simple words, an array created with more than one dimension (size) is called as multi dimensional array. • Multi dimensional array can be of two dimensional array or three dimensional array or four dimensional array or more... • Most popular and commonly used multi dimensional array is two dimensional array. The 2-D arrays are used to store data in the form of table. We also use 2-D arrays to create mathematical matrices. • The maximum capacity of elements of array is the product of row size and column size.
  • 21. Declaration of multi dimensional array Data_type array_Name [ row_Size ] [ column_Size ] ; For example: int a[m][n]; Here a[0][0] is initial and a[m-1][n-1] is the last element.
  • 22.
  • 23. Initialization of multi dimensional array 2-D array can be initialized in a way similar to that of 1-D array. for example:- int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22}; These values are assigned to the elements row wise, so the values of elements after this initialization are Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20 Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21 Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22
  • 24. • While initializing we can group the elements row wise using inner braces. for example:- int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}}; • If we initialize an array as int mat[4][3]={{11},{12,13},{14,15,16},{17}}; Then the compiler will assume its all rest value as 0,which are not defined. Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17 Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0 Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0
  • 25. WAP to input data in 2-dimensional array and display in matrix form #include<stdio.h> int main(){ int disp[2][3]; int i, j; for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying array elements printf("Two Dimensional array elements:n"); for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==2){ printf("n"); }}} return 0; }
  • 26.
  • 27. Transpose of a matrix: 2-dimensional array #include <stdio.h> void main() { int array[10][10]; int i, j, m, n; printf("Enter the order of the matrix n"); scanf("%d %d", &m, &n); for (i = 0; i < m; ++i) { for (j = 0; j < n; ++j) { scanf("%d", &array[i][j]); } } printf("Transpose of matrix is n"); for (j = 0; j < n; ++j) { for (i = 0; i < m; ++i) { printf(" %d", array[i][j]); } printf("n"); } }
  • 28. WAP to add two matrices
  • 29. #include <stdio.h> #include <conio.h> void main() { int a[2][3],b[2][3],c[2][3],i,j; clrscr(); printf("nENTER VALUES FOR MATRIX A:n"); for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("nENTER VALUES FOR MATRIX B:n"); for(i=0;i<2;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<2;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("nTHE VALUES OF MATRIX C ARE:n"); for(i=0;i<2;i++) for(j=0;j<3;j++) printf("%5d",c[i][j]); printf("n"); getch();
  • 30. Assignments • WAP to find transpose of 3*3 matrix. • WAP to subtract two 3*3 matrices. • WAP to multiply two 2*2 matrices. • WAP to take salary of 100 employee and sort them on the basis of ascending order. • WAP to take ‘n’ input and find greatest and least number among them. • WAP to input marks of 20 students and display their sum and average marks.
  • 31. WAP to multiply two 2*2 matrices
  • 32. #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter number of rows and columns of first matrixn"); scanf("%d%d", &m, &n); printf("Enter elements of first matrixn"); for (c = 0; c < m; c++) for (d = 0; d < n; d++) scanf("%d", &first[c][d]); printf("Enter number of rows and columns of second matrixn"); scanf("%d%d", &p, &q); printf("Enter elements of second matrixn"); for (c = 0; c < p; c++) for (d = 0; d < q; d++) scanf("%d", &second[c][d]); for (c = 0; c < m; c++) { for (d = 0; d < q; d++) { for (k = 0; k < p; k++) { sum = sum + first[c][k]*second[k][d]; multiply[c][d] = sum; sum = 0; }} printf("Product of the matrices:n"); for (c = 0; c < m; c++) { for (d = 0; d < q; d++) printf("%dt", multiply[c][d]); printf("n"); }} return 0; }
  • 33. Differences between one dimensional and two dimensional array