3. ➢ Array is a collection of same type elements under
the same variable identifier referenced by index
number.
➢ It supports the homogeneous data type not
heterogeneous.
➢ Arrays are widely used within programming for
different purposes such as sorting, searching and
etc.
➢ Arrays are efficient and useful for performing
operations .
2/6/2021
Dharma Kumari Kalakheti 3
4. Group of elements storing a common type of data
int number[5] ={ };
Data type
array name
size
Elements differentiated by their positions
in the array
value
2/6/2021
Dharma Kumari Kalakheti 4
5. ➢ It always start from 0 (zero) position and end
with size -1.
➢ eg. int a[5]={21,23,14,15,22}
➢ Therefore in an array with “n” elements first
index is “0” and the last index is “n-1”.
➢ The value of each element of the array is listed
within two curly brackets ({ }) and a comma (,)
is used to separate one value from another.
2/6/2021
Dharma Kumari Kalakheti
21 23 14 15 22
0 1 2 3 4
5
6. ➢ Array name must not be separate form the square
brackets containing the index.
data-type array-name[array-size]={define variable};
int a[5]={11,12,13,14,15} // right declaration.
BUT
int a [5] ={11,12,13,14,15} // wrong declaration.
2/6/2021
Dharma Kumari Kalakheti 6
8. #include<stdio.h>
#include<conio.h> header file
void main() main function start
{
int a[5]={12,13,14,15,16},i;
printf("this is input result n");
for(i=0;i<5;i++) main program
printf("n %d",a[i]);
getch();
}
This is input result
12
13 Output result
14
15
16
12 13 14 15 16
0 1 2 3 4
2/6/2021
Dharma Kumari Kalakheti 8
9. One-dimensional arrays
Multidimensional array
➢ The type of arrays that we discussed up to now is
called a one dimensional array.
Single dimensional array is an array with a variable
with single subscript.
Declaration of one-dimensional array
data_type array_name[array_size];
For example: int age[5];
2/6/2021
Dharma Kumari Kalakheti 9
10. Here, the name of array is age. The size of
array is 5,i.e., there are 5 items(elements)
of array age. All element in an array are
of the same type (int, in this case).
2/6/2021
Dharma Kumari Kalakheti
int age[5];
10
11. 2/6/2021
Dharma Kumari Kalakheti
/* example of an array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int n[5]= {2, 4, 6, 3, 0};
for (i=0; i<=4; i++)
{
printf("%dn", n[i]);
}
getch();
} 11
Output:
12. /* example to input marks of 5 students and to display them */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int n[5];
for (i=1; i<=5; i++)
{
printf("enter the marks:");
scanf("%d", &n[i]);
}
for (i=1; i<=5; i++)
{
printf("%dn", n[i]);
}
getch();
}
2/6/2021 Dharma Kumari Kalakheti 12
Sample Program 2
13. 3. Write a flowchart and a program to read 5 persons age maximum,
minimum
#include <stdio.h>
#include <conio.h>
void main()
{
int age[5],max=0,min=100,i;
clrscr();
for (i=1;i<=5;i++)
{
printf("enter the age: ");
scanf("%d",&age[i]);
if (age[i]>max)
max=age[i];
if(age[i]<min)
min=age[i];
}
printf("nmax=%d", max);
printf("nmin=%d",min);
getch();
}
14. WAP to interchange given numbers
{3,61,80,9,70,51,2,1}
#include <stdio.h>
#include <conio.h> //interchange the number
void main()
{
int num[8]={3,61,80,9,70,51,2,1};
int i,temp;
clrscr();
for(i=0;i<=7;i+=2)
{
temp=num[i];
num[i]=num[i+1];
num[i+1]=temp;
}
for(i=0;i<=7;i++)
printf("n marks=%d",num[i]);
getch();
}
15. 2/6/2021 Dharma Kumari Kalakheti 15
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int arr[5]={25,17,31,13,2},i,j,temp;
for(i=0;i<5;i++)
{
for(j=0; j<5-i; j++)
{
if (arr[j]> arr[j+1])
{
temp=arr[j+1];
arr[j+1]= arr[j];
arr[j]=temp; }
}
}
printf("nnarray after sorting n");
for(j=0;j<5;j++)
printf("%dt",arr[j]);
getch();
}
//Program to sort following numbere
in Ascending order= 25,17,31,13,2
16. #include<stdio.h>
#include<conio.h>
int fact(int n);
void main()
{
int n;
clrscr();
printf("Enter any Num");
scanf("%d",&n);
printf("fact=%d",fact(n));
getch();
}
int fact(int n)
{
if(n<=1)
return(1);
else
return(n*fact(n-1));
}
/*WAP to find the factorial of a number using recursive function*/
17. 2/6/2021 Dharma Kumari Kalakheti 17
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int arr[5],i,j,temp;
printf("enter any numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<5;i++)
{
for(j=0; j<5-i; j++)
{
if (arr[j]> arr[j+1])
{
temp=arr[j];
arr[j]= arr[j+1];
arr[j+1]=temp;
}
}
}
printf("nnarray after sorting n");
for(j=0;j<5;j++)
printf("%dt",arr[j]);
getch();
}
WAP to enter the 5 number and sorting in
ascending order
19. 2/6/2021 Dharma Kumari Kalakheti 19
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int arr[5]={25,17,31,13,12},i,j,temp;
for(i=0;i<5;i++)
{
for(j=0; j<5-i; j++)
{
if (arr[j]> arr[j+1])
{
temp=arr[j];
arr[j]= arr[j+1];
arr[j+1]=temp;
}
}
}
printf("nnarray after sorting n");
for(j=0;j<5;j++)
printf("%dt",arr[j]);
getch();
}
//Program to sort following numbere in
decending order= 25,17,31,13,2
20. #include<stdio.h>
#include<conio.h>
void num();
void main()
{
clrscr();
num();
getch();
}
void num()
{
clrscr();
int arr[5]={25,17,31,13,12},i,j,temp;
for(i=0;i<5;i++)
{
for(j=0; j<5-i; j++)
{
if (arr[j]> arr[j+1])
{
temp=arr[j];
arr[j]= arr[j+1];
arr[j+1]=temp;
}
}
}
printf("nnarray after sorting n");
for(j=0;j<5;j++)
printf("%dt",arr[j]);
}
WAP to enter the 10 number and sorting in
ascending order using function
21. 7. WAP to print the odd and even numbers from 1 to 10 and
sum them separately.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[10]={1,2,3,4,5},sum=0,even=0,odd=0,i;
for(i=0;i<=4;i++)
{
//printf("enter the number=");
//scanf("%d",&a[i]);
sum=sum+a[i];
if (a[i]%2==0)
{
even=even+a[i];
}
else
odd=odd+a[i];
}
printf("ntotal sum=%d",sum);
printf("ntotal even=%d",even);
printf("ntotal odd=%d",odd);
getch();
}
23. /* Write a program to read five persons age using array and find out
average age. (preeboard exam 2070*/
2/6/2021 Dharma Kumari Kalakheti 23
#include <stdio.h>
#include <conio.h>
void main()
{
int age[5],sum=0,avg,i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter five persons age:");
scanf("%d",&age[i]);
sum= sum+age[i];
}
avg=sum/5;
//printf("nsum=%d",sum);
printf("navg=%d",avg);
getch();
}
25. Show this (2072-3-18)
N
N E
N E P
N E P A
N E P A L
#include <stdio.h >
#include <conio.h>
void main()
{
char n[6]= "NEPAL",i,j;
for(i=0;i<=5;i++)
{
for(j=0;j<i;j++)
{
printf("t%c",n[j]);
}
printf("n");
}
getch();
26. Show this
NEPAL
NEPA
NEP
NE
N
#include <stdio.h >
#include <conio.h>
void main()
{
char n[6]= "NEPAL",i,j;
for(i=5;i>1;i--)
{
for(j=0;j<i;j++)
{
printf("t%c",n[j]);
}
printf("n");
}
getch();
}
27. Multi- dimensional arrays are those which have more then one
dimensions. Multi- dimensional arrays are defined in much the
same manner as one dimensional array, except that a separate
pair of square brackets is required for each subscript .thus ,two
dimensional arrays will require two pairs of square brackets.
In 2-D array, the first dimensional specifies number of rows and
second specifies columns. Each row contains elements of many
columns. Thus ,a row is 1-D array .2-D array contains multiple
rows .Thus, 2-D array is an array of 1-D arrays. As each row will
contain elements of many columns, 2-D array is an array with a
variable with two subscripts e.g
Int a[2] [2]
28. This matrix can be represented by double
dimensional array
Int a [2] [2] where
A [0] [0]=2
A [0] [1]=3
A [1] [0]=1
A [1] [1]=4
We can represent matrix in double dimensional
array 2 3
1 4
30. 2/6/2021 Dharma Kumari Kalakheti 30
#include <stdio.h>
#include <conio.h>
void main() // print the 2/2 matrix
{
int A[2][2],i,j;
for (i=0;i<2;i++)
for(j=0;j<2;j++)
//this is a main matrix entry
{
printf("enter the element %d %d:", i, j);
scanf("%d",&A[i][j]);
}
//print matrix
printf("this is my first matrix: ");
for (i=0;i<2;i++)
{
printf("n");
for(j=0;j<2;j++)
{
printf("%dt",A[i][j]);
}
printf("n");
}
getch();
}
31. #include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int a[2][2],i,j;
printf("enter the matrix:n");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
scanf("%d",&a[i][j]);
}
printf("the matrix you supplied");
for(i=1;i<=2;i++)
{
printf("n");
for(j=1;j<=2;j++)
printf("t%d",a[i][j]);
}
printf("n");
printf("transpose of the matrixn");
for(i=1;i<=2;i++)
{
for(j=1;j<=2;j++)
{
printf("%dt",a[j][i]);
}
printf("n");
}
getch();
}
32. #include <stdio.h>
#include <conio.h>
void main()
{
int A[2][2],i,j;
for (i=0;i<2;i++)
for(j=0;j<2;j++)
//this is a main matrix entry
{
printf("enter the element %d %d:", i,j);
scanf("%d",&A[i][j]);
}
//print matrix
printf("this is 2/2 matrix: ");
for (i=0;i<2;i++)
{
printf("n");
for(j=0;j<2;j++)
{
printf("%dt",A[i][j]);
}
printf("n");
}
printf("transpose of the matrixn");
for(i=0;i<2;i++)
{
printf("n");
for(j=0;j<2;j++)
{
printf("%dt",A[j][i]);
}
printf("n");
}
getch();
}
34. Enter the number 1
1
0
1
Enter the number 2
1
1
1
Enter the number 3
1
0
1
The result is :-
1 0 1
1 1 1
1 0 1
2/6/2021
Dharma Kumari Kalakheti 34
35. 2/6/2021 Dharma Kumari Kalakheti 35
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][3], i, j, sum=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("nEnter the value for
A[%d][%d]:",i,j);
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=sum+arr[i][j];
}
}
/*Display the value of sum*/
printf("nThe sum of the elements of 2-D
array is %d", sum);
getch();
}
Program that accept values in 2-Dimensional
3 by 3 array and displays the sum of all the
elements.