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

A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
Imdadul Himu
 
Arrays
ArraysArrays
Array in c++
Array in c++Array in c++
Array in c++
Mahesha Mano
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Array
ArrayArray
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
janani thirupathi
 
Array
ArrayArray
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
Rajendran
 
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
Dr. Volkan OBAN
 
Array in C
Array in CArray in C
Array in C
Kamal Acharya
 
Java arrays
Java arraysJava arrays
Java arrays
BHUVIJAYAVELU
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
C programming , array 2020
C programming , array 2020C programming , array 2020
C programming , array 2020
Osama Ghandour Geris
 
Array
ArrayArray
Array
Deep Shah
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Uma mohan
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
tanmaymodi4
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
sandhya yadav
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 

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

Arrays
ArraysArrays
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
nmahi96
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
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
Appili Vamsi Krishna
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
Dr.Subha Krishna
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
Munazza-Mah-Jabeen
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
Arrays
ArraysArrays
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
Hemantha Kulathilake
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
Array
ArrayArray
Array
hjasjhd
 
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
imtiazalijoono
 
Session 4
Session 4Session 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
rohinitalekar1
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
aarockiaabinsAPIICSE
 
Arrays
ArraysArrays
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 

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

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
Celine George
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
taiba qazi
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
ak6969907
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 

Recently uploaded (20)

A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
How to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP ModuleHow to Add Chatter in the odoo 17 ERP Module
How to Add Chatter in the odoo 17 ERP Module
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
DRUGS AND ITS classification slide share
DRUGS AND ITS classification slide shareDRUGS AND ITS classification slide share
DRUGS AND ITS classification slide share
 
World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024World environment day ppt For 5 June 2024
World environment day ppt For 5 June 2024
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 

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