SlideShare uma empresa Scribd logo
1 de 17
Session Objectives
Explain Declaration,Initialization of Array
Explain Types of Array
One Dimensional,Two Dimensional and Multi
Dimensional Array
Explain Arrays
Array is defined as a set of homogeneous data items.
An Array is a group of elements that share a common name
that are differentiated from one another by their positions within the
array
Datatype arrayname[subscript];
1) Arrayname should be a valid “C” variable
2) Arrayname should be unique
3) The elements in the array should be of same type
4) Subscript (array size) cannot be negative
5) Subscript must always be an integer
Single or One Dimensional Arrays
Arrays whose elements are specified by one subscript
are called One dimensional array or linear array.
Syntax :
datatype arrayname[size];
For Example :
Note :
By default array index should starts with zero (0)
Write a program for entering data into an
array & Reading data from an array
#include<stdio.h>
void main()
{
int arr[10],I,n;
printf("n ENter N Elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter arr[%d]=",i);
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
printf("%dn",arr[i]);
}
}
Enter N Elements : 3
Enter arr[0] : 2
Enter arr[1] : 5
Enter arr[2] : 3
2
5
3
PROGRAM-ARRAY INITIALIZATION
Array Initialization
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50};
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("%dn",a[i]);
}
getch();
}
10
20
30
40
50
Write a “C” program to sort the given number is in ascending order using one dimensional
array
#include<stdio.h>
void main()
{
int i,j,n, a[10],temp;
printf("n size of vector=");
scanf("%d",&n);
printf("vector elements:");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("nnElements in asending order is=n");
for(i=0;i<n;i++)
printf("%d",a[i]);
printf("nnElements in descending order is=n");
for(i=n-1;i>=0;i--)
printf("%d",a[i]);
getch();
}
Two Dimensional Arrays
A Arrays whose elements are specified by two subscript
such as row and column are called One dimensional array
or linear array.
Row  means horizontally
Column  means vertically
A two - dimensional array looks like a school time-table
consisting of rows and columns.
A two – dimensional array is declared as -
Two Dimensional Array Initialization
The result of the above assignment will be as follows :
Write a “C” program to perform the addition of two matrices
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Input A - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Input B - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("Sum of A and B Matrix=n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("%d",c[i][j]);
printf("n");
}
}
Write a “C” program to perform the subtraction of two matrices
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
printf("Input A - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("Input B - Matrixn");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
printf("Sum of A and B Matrix=n");
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
printf("%d",c[i][j]);
printf("n");
}
}
Write a “C” program to sort the given names in Alphabetical order using One
dimensional array
#include<stdio.h>
#include<string.h>
void main()
{
int i,j,n;
char a[10][10],temp[10];
printf("n Enter the N Values");
scanf("%d",&n);
printf("Enter the Names one by one :n");
for(i=0;i<n;i++)
{
scanf("%s",&a[i]);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if((strcmp(a[i],a[j]))>0)
{
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
printf("The Names in Alphabetical Order is =n");
for(i=0;i<n;i++)
printf("n%s",a[i]);
}
Write a “C” program to perform matrix multiplication using two dimensional array
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
printf("Input row and column of A matrix n");
scanf("%d %d",&n,&m);
printf(" Input row and column of B matrix n");
scanf("%d %d",&p,&q);
if(n==q){
printf(" Matrices can be Multiplied: n");
printf(" Input A-matrix n");
for(i=0;i<n;++i)
for(j=0;j<m;++j)
scanf("%d",&a[i][j]);
printf(" Input B-matrix n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("The resultant matrix ist:n");
for(i=0;i<n;++i){
for(j=0;j<m;++j){
c[i][j]=0;
for(k=0;k<m;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("%d",c[i][j]);}
printf("n");}}
else
printf("Matrices cannot be multiplied n");
}
Write a “C” program to find the largest and smallest numbers given in the array
#include<stdio.h>
void main()
{
int i,n;
float a[20],large,small;
printf("nEnter the N values=");
scanf("%d",&n);
printf("Enter the values one by one :n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
large=a[0];
for(i=1;i<n;i++)
{
if(a[i]>large)
large=a[i];
}
small=a[0];
for(i=1;i<n;i++)
{
if(a[i]<small)
small=a[i];
}
printf("Largest element is = %fn",large);
printf("Smallest element = %fn",small);
}
Session Summary
 Arrayname should be a unique and valid “C” Variable name
 The number of elements in a multi dimensional array is the product of its subscripts
 Arrays can be initialized to the same type in which they are declared
 The character array receives the terminating ‘0’ in the string constant
 The individual values in the array are called as elements
 It is not necessary to specify the length of an array, explicitly in case if initializers are
provided for the array during declaration itself
EXERCISES
1. Write a program to search an element and to find how many times it is present in
the array?
2. Write a program to find the sum of diagonal elements in a matrix
3. Write a program to find the second largest number in an array?
4. Write a program to remove the duplicate elements of the array?
5. Write a program to merge two arrays and print the merged array in ascending
order?
6. Write a program to insert an element into an sorted array of integers?
7. Write a program to display only the negative elements of the array?

Mais conteúdo relacionado

Semelhante a presentation_arrays_1443553113_140676.ppt

2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
Nooryaseen9
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 

Semelhante a presentation_arrays_1443553113_140676.ppt (20)

Array
ArrayArray
Array
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Session 7 En
Session 7 EnSession 7 En
Session 7 En
 
Array
ArrayArray
Array
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
array.ppt
array.pptarray.ppt
array.ppt
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Arrays
ArraysArrays
Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Basic C Programming Lab Practice
Basic C Programming Lab PracticeBasic C Programming Lab Practice
Basic C Programming Lab Practice
 
Array
ArrayArray
Array
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
Arrays
ArraysArrays
Arrays
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
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
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 

Último

result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 

Último (20)

UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 

presentation_arrays_1443553113_140676.ppt

  • 1.
  • 2. Session Objectives Explain Declaration,Initialization of Array Explain Types of Array One Dimensional,Two Dimensional and Multi Dimensional Array Explain Arrays
  • 3. Array is defined as a set of homogeneous data items. An Array is a group of elements that share a common name that are differentiated from one another by their positions within the array Datatype arrayname[subscript]; 1) Arrayname should be a valid “C” variable 2) Arrayname should be unique 3) The elements in the array should be of same type 4) Subscript (array size) cannot be negative 5) Subscript must always be an integer
  • 4.
  • 5. Single or One Dimensional Arrays Arrays whose elements are specified by one subscript are called One dimensional array or linear array. Syntax : datatype arrayname[size]; For Example : Note : By default array index should starts with zero (0)
  • 6. Write a program for entering data into an array & Reading data from an array #include<stdio.h> void main() { int arr[10],I,n; printf("n ENter N Elements"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter arr[%d]=",i); scanf("%d",&arr[i]); } for(i=0;i<n;i++) { printf("%dn",arr[i]); } } Enter N Elements : 3 Enter arr[0] : 2 Enter arr[1] : 5 Enter arr[2] : 3 2 5 3
  • 7. PROGRAM-ARRAY INITIALIZATION Array Initialization #include<stdio.h> #include<conio.h> void main() { int a[5]={10,20,30,40,50}; int i; clrscr(); for(i=0;i<5;i++) { printf("%dn",a[i]); } getch(); } 10 20 30 40 50
  • 8. Write a “C” program to sort the given number is in ascending order using one dimensional array #include<stdio.h> void main() { int i,j,n, a[10],temp; printf("n size of vector="); scanf("%d",&n); printf("vector elements:"); for (i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("nnElements in asending order is=n"); for(i=0;i<n;i++) printf("%d",a[i]); printf("nnElements in descending order is=n"); for(i=n-1;i>=0;i--) printf("%d",a[i]); getch(); }
  • 9. Two Dimensional Arrays A Arrays whose elements are specified by two subscript such as row and column are called One dimensional array or linear array. Row  means horizontally Column  means vertically A two - dimensional array looks like a school time-table consisting of rows and columns. A two – dimensional array is declared as -
  • 10. Two Dimensional Array Initialization The result of the above assignment will be as follows :
  • 11. Write a “C” program to perform the addition of two matrices #include<stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]+b[i][j]; printf("Sum of A and B Matrix=n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%d",c[i][j]); printf("n"); } }
  • 12. Write a “C” program to perform the subtraction of two matrices #include<stdio.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; printf("Input A - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); printf("Input B - Matrixn"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&b[i][j]); for(i=0;i<3;i++) for(j=0;j<3;j++) c[i][j]=a[i][j]-b[i][j]; printf("Sum of A and B Matrix=n"); for(i=0;i<3;++i) { for(j=0;j<3;++j) printf("%d",c[i][j]); printf("n"); } }
  • 13. Write a “C” program to sort the given names in Alphabetical order using One dimensional array #include<stdio.h> #include<string.h> void main() { int i,j,n; char a[10][10],temp[10]; printf("n Enter the N Values"); scanf("%d",&n); printf("Enter the Names one by one :n"); for(i=0;i<n;i++) { scanf("%s",&a[i]); } for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if((strcmp(a[i],a[j]))>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } printf("The Names in Alphabetical Order is =n"); for(i=0;i<n;i++) printf("n%s",a[i]); }
  • 14. Write a “C” program to perform matrix multiplication using two dimensional array #include<stdio.h> void main() { int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k; printf("Input row and column of A matrix n"); scanf("%d %d",&n,&m); printf(" Input row and column of B matrix n"); scanf("%d %d",&p,&q); if(n==q){ printf(" Matrices can be Multiplied: n"); printf(" Input A-matrix n"); for(i=0;i<n;++i) for(j=0;j<m;++j) scanf("%d",&a[i][j]); printf(" Input B-matrix n"); for(i=0;i<p;++i) for(j=0;j<q;++j) scanf("%d",&b[i][j]); printf("The resultant matrix ist:n"); for(i=0;i<n;++i){ for(j=0;j<m;++j){ c[i][j]=0; for(k=0;k<m;++k) c[i][j]=c[i][j]+a[i][k]*b[k][j]; printf("%d",c[i][j]);} printf("n");}} else printf("Matrices cannot be multiplied n"); }
  • 15. Write a “C” program to find the largest and smallest numbers given in the array #include<stdio.h> void main() { int i,n; float a[20],large,small; printf("nEnter the N values="); scanf("%d",&n); printf("Enter the values one by one :n"); for(i=0;i<n;i++) { scanf("%f",&a[i]); } large=a[0]; for(i=1;i<n;i++) { if(a[i]>large) large=a[i]; } small=a[0]; for(i=1;i<n;i++) { if(a[i]<small) small=a[i]; } printf("Largest element is = %fn",large); printf("Smallest element = %fn",small); }
  • 16. Session Summary  Arrayname should be a unique and valid “C” Variable name  The number of elements in a multi dimensional array is the product of its subscripts  Arrays can be initialized to the same type in which they are declared  The character array receives the terminating ‘0’ in the string constant  The individual values in the array are called as elements  It is not necessary to specify the length of an array, explicitly in case if initializers are provided for the array during declaration itself
  • 17. EXERCISES 1. Write a program to search an element and to find how many times it is present in the array? 2. Write a program to find the sum of diagonal elements in a matrix 3. Write a program to find the second largest number in an array? 4. Write a program to remove the duplicate elements of the array? 5. Write a program to merge two arrays and print the merged array in ascending order? 6. Write a program to insert an element into an sorted array of integers? 7. Write a program to display only the negative elements of the array?