2/6/2021
Dharma Kumari Kalakheti 1
2/6/2021
Dharma Kumari Kalakheti 2
➢ 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
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
➢ 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
➢ 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
index[0] = 11 index[1] = 12 index[2] = 13
index[3] =14 index[4] = 15
int digits[5]={11,12,13,14,15};
2/6/2021
Dharma Kumari Kalakheti 7
#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
 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
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
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:
/* 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
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();
}
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();
}
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
#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*/
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
2/6/2021 Dharma Kumari Kalakheti 18
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int n[5],i,j,temp;
printf("enter any numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&n[i]);
}
for(i=0;i<5;i++)
{
for(j=0; j<5-i; j++)
{
if (n[j]> n[j+1])
{
temp=n[j];
n[j]= n[j+1];
n[j+1]=temp;} } }
printf("nnarray after sorting n");
for(j=0;j<5;j++)
printf("%dt",n[j]);
getch();
}
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
#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
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();
}
2/6/2021 Dharma Kumari Kalakheti 22
#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+1];
arr[j+1]= arr[j];
arr[j]=temp;
}
}
}
printf("nnarray after sorting n");
for(j=1;j<=5;j++)
printf("%dt",arr[j]);
getch();
}
/* 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();
}
Print “NEPAL”
#include <stdio.h >
#include <conio.h>
void main()
{
char n[5]= "NEPAL",i;
for(i=0;i<5;i++)
{
printf("%c",n[i]);
}
getch();
}
2/6/2021 Dharma Kumari Kalakheti 24
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();
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();
}
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]
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
Syntax:-
data_type array_name[expression 1][expression 2]…[expression n] ;
int no[3][3] ;
Defining 2 dimensional array
0 1 2
1
2
2/6/2021 Dharma Kumari Kalakheti 29
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();
}
#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();
}
#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();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int no[3][3],i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
{
printf("enter the number %d = ",i,j);
scanf("%d",&no[i][j]);
}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("t %d",no[i][j]);
}
printf("n");
}
getch();
} 2/6/2021
Dharma Kumari Kalakheti 33
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
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.
2/6/2021
Dharma Kumari Kalakheti
36

More Related Content

PPTX
Array
PPT
Chap09
PPT
Arrays and structures
PPT
Multidimensional array in C
PPT
Data Structure and Algorithms Arrays
PPTX
R교육1
PPT
arrays
Array
Chap09
Arrays and structures
Multidimensional array in C
Data Structure and Algorithms Arrays
R교육1
arrays

What's hot (20)

PPTX
PDF
Arrays in Java | Edureka
PDF
Array&amp;string
PDF
Arrays in C
PPTX
Data structures
PPT
Java Arrays
PPT
C++ Arrays
PPT
PPTX
Arrays in java
PDF
PPTX
arrays of structures
DOCX
Array assignment
PPT
Array
PDF
Array and Collections in c#
PDF
2 data structure in R
PPTX
Array in c#
PPTX
Unit 1 LINEAR DATA STRUCTURES
PPT
PDF
Data Structures (BE)
Arrays in Java | Edureka
Array&amp;string
Arrays in C
Data structures
Java Arrays
C++ Arrays
Arrays in java
arrays of structures
Array assignment
Array
Array and Collections in c#
2 data structure in R
Array in c#
Unit 1 LINEAR DATA STRUCTURES
Data Structures (BE)
Ad

Similar to About Array (20)

PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
Array Introduction One-dimensional array Multidimensional array
PDF
array-191103180006.pdf
PPTX
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
PPTX
Data structure.pptx
PPT
PPTX
PPTX
C (PPS)Programming for problem solving.pptx
PPT
Fp201 unit4
PPT
PDF
Arrays and library functions
PDF
PPT
PPTX
Module_3_Arrays - Updated.pptx............
PPTX
unit1Intro_final.pptx
PPTX
Arrays & Strings
PPTX
arrays in c programming - example programs
PDF
ARRAYS
PDF
Introduction to Arrays in C
Arrays 1D and 2D , and multi dimensional
Array Introduction One-dimensional array Multidimensional array
array-191103180006.pdf
01245xsfwegrgdvdvsdfgvsdgsdfgsfsdgsdgsdgdg
Data structure.pptx
C (PPS)Programming for problem solving.pptx
Fp201 unit4
Arrays and library functions
Module_3_Arrays - Updated.pptx............
unit1Intro_final.pptx
Arrays & Strings
arrays in c programming - example programs
ARRAYS
Introduction to Arrays in C
Ad

More from DharmaKumariBhandari (6)

PPTX
De Mornan Theory, Boolean Algebra, 7 logical get, truth table,
PDF
Loop's definition and practical code in C programming
PDF
C programme presentation
PPTX
social prospective in Education
De Mornan Theory, Boolean Algebra, 7 logical get, truth table,
Loop's definition and practical code in C programming
C programme presentation
social prospective in Education

Recently uploaded (20)

PDF
Psychology and Work Today 10th Edition by Duane Schultz Test Bank.pdf
PPTX
Life Skills Education - Introduction - 1
PPTX
2-THE-NATIONAL-EARLY-LEARNING-FRAMEWORK.STE.pptx
PPTX
Unlocking Success Through the Relentless Power of Grit
PDF
Anxiety Awareness Journal One Week Preview
PPT
Critical Thinking Lecture 1 2008 University.ppt
PPTX
Hazards-of-Uncleanliness-Protecting-Your-Health.pptx
PDF
The Blogs_ Humanity Beyond All Differences _ Andy Blumenthal _ The Times of I...
PPTX
My future self called today–I answered.pptx
PPT
Thinking Critically Presentation w Exercise.ppt
DOCX
Paulo Tuynmam: Nine Timeless Anchors of Authentic Leadership
PDF
Why is mindset more important than motivation.pdf
PPTX
Arabic Grammar with related Qurani ayat .pptx
PPT
Sanskar for Kids a cultural intervension
PDF
OneRead_20250728_1807.pdfbdjsajaajjajajsjsj
PPTX
SELF ASSESSMENT Power Point Presentation Activity
PPTX
Atomic and Molecular physics pp p TTT B
PPTX
Modulation is the process of varying one or more properties of a carrier sign...
PDF
Dominate Her Mind – Make Women Chase, Lust, & Submit
PDF
PLAYLISTS DEI MEGAMIX E DEEJAY PARADE DAL 1991 AL 2004 SU RADIO DEEJAY
Psychology and Work Today 10th Edition by Duane Schultz Test Bank.pdf
Life Skills Education - Introduction - 1
2-THE-NATIONAL-EARLY-LEARNING-FRAMEWORK.STE.pptx
Unlocking Success Through the Relentless Power of Grit
Anxiety Awareness Journal One Week Preview
Critical Thinking Lecture 1 2008 University.ppt
Hazards-of-Uncleanliness-Protecting-Your-Health.pptx
The Blogs_ Humanity Beyond All Differences _ Andy Blumenthal _ The Times of I...
My future self called today–I answered.pptx
Thinking Critically Presentation w Exercise.ppt
Paulo Tuynmam: Nine Timeless Anchors of Authentic Leadership
Why is mindset more important than motivation.pdf
Arabic Grammar with related Qurani ayat .pptx
Sanskar for Kids a cultural intervension
OneRead_20250728_1807.pdfbdjsajaajjajajsjsj
SELF ASSESSMENT Power Point Presentation Activity
Atomic and Molecular physics pp p TTT B
Modulation is the process of varying one or more properties of a carrier sign...
Dominate Her Mind – Make Women Chase, Lust, & Submit
PLAYLISTS DEI MEGAMIX E DEEJAY PARADE DAL 1991 AL 2004 SU RADIO DEEJAY

About Array

  • 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
  • 7. index[0] = 11 index[1] = 12 index[2] = 13 index[3] =14 index[4] = 15 int digits[5]={11,12,13,14,15}; 2/6/2021 Dharma Kumari Kalakheti 7
  • 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
  • 18. 2/6/2021 Dharma Kumari Kalakheti 18 #include <stdio.h> #include <conio.h> void main() { clrscr(); int n[5],i,j,temp; printf("enter any numbers:"); for(i=0;i<5;i++) { scanf("%d",&n[i]); } for(i=0;i<5;i++) { for(j=0; j<5-i; j++) { if (n[j]> n[j+1]) { temp=n[j]; n[j]= n[j+1]; n[j+1]=temp;} } } printf("nnarray after sorting n"); for(j=0;j<5;j++) printf("%dt",n[j]); getch(); }
  • 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(); }
  • 22. 2/6/2021 Dharma Kumari Kalakheti 22 #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+1]; arr[j+1]= arr[j]; arr[j]=temp; } } } printf("nnarray after sorting n"); for(j=1;j<=5;j++) printf("%dt",arr[j]); 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(); }
  • 24. Print “NEPAL” #include <stdio.h > #include <conio.h> void main() { char n[5]= "NEPAL",i; for(i=0;i<5;i++) { printf("%c",n[i]); } getch(); } 2/6/2021 Dharma Kumari Kalakheti 24
  • 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
  • 29. Syntax:- data_type array_name[expression 1][expression 2]…[expression n] ; int no[3][3] ; Defining 2 dimensional array 0 1 2 1 2 2/6/2021 Dharma Kumari Kalakheti 29
  • 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(); }
  • 33. #include<stdio.h> #include<conio.h> void main() { int no[3][3],i,j; for(i=1;i<=3;i++) for(j=1;j<=3;j++) { printf("enter the number %d = ",i,j); scanf("%d",&no[i][j]); } for(i=1;i<=3;i++) { for(j=1;j<=3;j++) { printf("t %d",no[i][j]); } printf("n"); } getch(); } 2/6/2021 Dharma Kumari Kalakheti 33
  • 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.