SlideShare a Scribd company logo
1
Set 1 Q1 Program to sort the array in ascending order using Selection Sort
Algorithm
Program
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,j,temp;
int *a;
printf("The no of elements ");
scanf("%d",&n);
a = (int *)malloc (sizeof(int) * n);
printf("Enter the elements n" );
for (i=0 ; i<n ; i++)
{
scanf("%d", &(*(a+i)));
}
for (i = 0; i < n; i++)
{
for (j = i+1; j < n; j++)
{
if (*(a+i) > *(a+j))
{
temp = *(a+i);
*(a+i)= *(a+j);
*(a+j)=temp;
}
}
}
printf("In ascending order ");
for (i=0 ; i<n ; i++)
{
printf("%d " , *(a+i));
}
printf("n");
2
free (a);
return 0;
}
Output
The no of elements 10
Enter the elements
9
7
55
4
-2
0
5
5
7
30
In ascending order -2 0 4 5 5 7 7 9 30 55
3
Set 1 Q2 Program to sort the array in descending order using the Bubble Sort
Algorithm
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a,i,n,j,temp;
printf("Enter the number of elements to be sorted");
scanf("%d",&n);
a = (int*) malloc (n*sizeof (int));
printf("Enter the elements ");
for(i=0;i<n;i++)
{
scanf ("%d",&(*(a+i)));
}
for (i=0; i<(n-1); i++)
{
for (j=0; j<(n-i-1);j++)
{
if (*(a+j)<*(a+j+1))
{
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
}
}
}
printf("In descending order: n");
for(i=0;i<n;i++)
{
printf("%d ",*(a+i));
}
free(a);
printf("n");
return 0;
}
4
Output
Enter the number of elements to be sorted10
Enter the elements 15
0
-4
7
8
0
20
15
35
10
In descending order:
35 20 15 15 10 8 7 0 0 -4
5
Set1 Q3 Program to search an element in an array using Linear Search
Algorithm
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int s, c, n, l, p;
int *a,*loc;
printf("Enter the number of elements in array");
scanf("%d",&n);
a = (int*) malloc (n*sizeof (int));
loc = (int*) malloc (n*sizeof (int));
printf("Enter the numbers ");
for (c = 0; c < n; c++)
scanf("%d", &(*(a+c)));
printf("Enter the number to search");
scanf("%d", &s);
l=0;
for (c = 0; c < n; c++)
{
if (*(a+c) == s)
{
*(loc+l) =c;
l=l+1;
}
}
if (l==0)
printf("%d is not present in array n", s);
else
{
for (p=0; p<l; p++)
printf("n Number is located at position - %d ", *(loc+p)+1);
}
6
printf("n");
free (a);
free (loc);
return 0;
}
Output 1
Enter the number of elements in array10
Enter the numbers 4
2
3
7
8
1
2
0
9
5
Enter the number to search2
Number is located at position - 2
Number is located at position – 7
Output 2
Enter the number of elements in array5
Enter the numbers 3
5
7
0
2
Enter the number to search1
1 is not present in array
7
Set 1 Q4 Program to search an element in an array using Binary Search
Algorithm
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int first, last, mid,temp, n,s,i,j,lo,pos;
int *a,*loc,*b;
printf("Enter number of elements ");
scanf("%d",&n);
a = (int*) malloc (n* sizeof(int));
b = (int*) malloc (n* sizeof(int));
loc = (int*) malloc (n* sizeof(int));
printf("Enter the elements n" );
for (i=0 ; i<n ; i++)
scanf("%d", &(*(a+i)));
for (i=0 ; i<n ; i++)
*(b+i)=*(a+i);
for (i = 0; i < n; i++)
{
for (j = i+1; j < n; j++)
{
if (*(a+i) > *(a+j))
{
temp = *(a+i);
*(a+i)= *(a+j);
*(a+j)=temp;
}
}
}
printf("Enter value to find ");
8
scanf("%d", &s);
first = 0;
last = n - 1;
mid = (first+last)/2;
while (first <= last)
{
if (*(a+mid) < s)
first = mid + 1;
else if (*(a+mid) == s)
{
lo=mid;
break;
}
else
last = mid - 1;
mid = (first + last)/2;
}
pos=0;
if (first>last)
{
printf("Not found! %d is not present in the listn", s);
}
else
{
for (j=0;j<n;j++)
{
if (*(b+j)==*(a+lo))
{
*(loc+pos)=j;
pos=pos+1;
}
}
for (j=0; j<pos; j++)
printf("n Number is located at position - %d ", loc[j]+1);
}
printf("n");
9
free (a);
free (b);
free (loc);
return 0;
}
Output 1
Enter number of elements 5
Enter the elements
2
3
4
6
5
Enter value to find 1
Not found! 1 is not present in the list
Output 2
Enter number of elements 10
Enter the elements
4
6
-2
7
8
9
6
1
7
7
Enter value to find 7
Number is located at position - 4
Number is located at position - 9
Number is located at position – 10
10
Set 1 Q5 Program to find the Addition of two matrices
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a,**b,**c,n,m,p,q,i,j;
printf("Enter the no of rows and columns for first matrix");
scanf("%d %d",&n ,&m);
printf("Enter the no of rows and columns for second matrix");
scanf("%d %d",&p, &q);
if ((n==p)&&(m==q))
{
a= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
a[i]= (int*) calloc(m,sizeof(int));
b= (int**) calloc(p,sizeof(int));
for (i=0;i<p; i++)
b[i]= (int*) calloc(q,sizeof(int));
c= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
c[i]= (int*) calloc(m,sizeof(int));
printf("Enter the elements of first matrix");
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
scanf("%d",&(*(*(a+i)+j)));
}
printf("Enter the elements of second matrix");
for (i=0; i<p; i++)
for (j=0; j<q; j++)
{
scanf("%d",&(*(*(b+i)+j)));
11
}
printf("Elements of first matrix aren");
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("%d",*(*(a+i)+j));
printf("t");
}
printf("n");
}
printf("Elements of second matrix aren");
for (i=0; i<p; i++)
{
for (j=0; j<q; j++)
{
printf("%d",*(*(b+i)+j));
printf("t");
}
printf("n");
}
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
*(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
}
}
printf("Addition of matrix isn");
for (i=0; i<n; i++)
{
for (j=0; j<q; j++)
{
printf("%dt",*(*(c+i)+j));
}
printf("n");
12
}
for (i=0;i<n; i++)
free (a[i]);
free (a);
for (i=0;i<p; i++)
free (b[i]);
free (b);
for (i=0;i<n; i++)
free (c[i]);
free (c);
}
else
printf("Number of columns and rows of both matrix should be equaln");
return 0;
}
Output
Enter the no of rows and columns for first matrix2
3
Enter the no of rows and columns for second matrix2
3
Enter the elements of first matrix-5
-4
-3
-2
-1
0
Enter the elements of second matrix0
1
2
3
4
5
Elements of first matrix are
-5 -4 -3
13
-2 -1 0
Elements of second matrix are
0 1 2
3 4 5
Addition of matrix is
-5 -3 -1
1 3 5
14
Set 1 Q6 Program to find the Product of two matrices
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a,**b,**c;
int n,m,p,q,i,j,k;
printf("Enter the no of rows and columns for first matrix");
scanf("%d %d",&n ,&m);
printf("Enter the no of rows and columns for second matrix");
scanf("%d %d",&p, &q);
if (m==p)
{
a= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
a[i]= (int*) calloc(m,sizeof(int));
b= (int**) calloc(p,sizeof(int));
for (i=0;i<p; i++)
b[i]= (int*) calloc(q,sizeof(int));
c= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
c[i]= (int*) calloc(q,sizeof(int));
printf("Enter the elements of first matrix");
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
scanf("%d",&(*(*(a+i)+j)));
}
printf("Enter the elements of second matrix");
for (i=0; i<p; i++)
for (j=0; j<q; j++)
{
scanf("%d",&(*(*(b+i)+j)));
15
}
printf("Elements of first matrix aren");
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("%d",*(*(a+i)+j));
printf("t");
}
printf("n");
}
printf("Elements of second matrix aren");
for (i=0; i<p; i++)
{
for (j=0; j<q; j++)
{
printf("%d",*(*(b+i)+j));
printf("t");
}
printf("n");
}
**c=0;
for(i=0;i<n;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<m;k++)
{
*(*(c+i)+j)=*(*(a+i)+k)**(*(b+k)+j)+*(*(c+i)+j);
}
}
}
printf("Product of matrix isn");
for (i=0; i<n; i++)
{
for (j=0; j<q; j++)
16
{
printf("%dt",*(*(c+i)+j));
}
printf("n");
}
for (i=0;i<n; i++)
free (a[i]);
free (a);
for (i=0;i<p; i++)
free (b[i]);
free (b);
for (i=0;i<n; i++)
free (c[i]);
free (c);
}
else
printf("Number of columns of first matrix should be equal to number of
rows of second matrixn");
return 0;
}
Output
Enter the no of rows and columns for second matrix3
2
Enter the elements of first matrix4
5
6
2
7
8
Enter the elements of second matrix1
2
3
7
5
17
6
Elements of first matrix are
4 5 6
2 7 8
Elements of second matrix are
1 2
3 7
5 6
Product of matrix is
49 79
63 101
18
Set 1 Q7 Program to find largest and smallest element in a matrix and also print
their position in the matrix
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,m,small,large,i,j,p,q,r;
int **a,*laloc1,*laloc2,*smloc1,*smloc2;
printf("nEnter the Order of the matrix...n");
scanf("%d %d",&n,&m);
a= (int**) calloc(n,sizeof(int*));
for(i=0;i<n;i++)
a[i]=(int*)calloc(m,sizeof(int));
smloc1=(int*)malloc(m*sizeof(int));
smloc2=(int*)malloc(m*sizeof(int));
laloc1=(int*)malloc(m*sizeof(int));
laloc2=(int*)malloc(m*sizeof(int));
printf("nEnter the value of the matrix...n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&(*(*(a+i)+j)));
printf("n Entered Matrix is: n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d ",*(*(a+i)+j));
printf("n");
}
small=**a;
large=**a;
q=0;
r=0;
19
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if (small>*(*(a+i)+j))
small=*(*(a+i)+j);
if (large<*(*(a+i)+j))
large=*(*(a+i)+j);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(small==*(*(a+i)+j))
{
smloc1[q]=i;
smloc2[q]=j;
q=q+1;
}
if (large==*(*(a+i)+j))
{
laloc1[r]=i;
laloc2[r]=j;
r=r+1;
}
}
}
printf("n Smallest number is %d ",small);
for (p=0; p<q;p++)
printf ("nLocated at row - %d column - %d ",smloc1[p]+1,
smloc2[p]+1) ;
printf("n Largest number is %d " ,large );
for (p=0; p<r;p++)
printf("n Located at row - %d column - %d", laloc1[p]+1,laloc2[p]+1);
20
printf("n");
for (i=0;i<n; i++)
free (a[i]);
free (a);
free (smloc1);
free (smloc2);
free (laloc1);
free (laloc2);
return 0;
}
Output
Enter the Order of the matrix...
3
3
Enter the value of the matrix...
4
5
6
2
3
5
6
1
0
Entered Matrix is:
4 5 6
2 3 5
6 1 0
Smallest number is 0
Located at row - 3 column - 3
Largest number is 6
Located at row - 1 column - 3
Located at row - 3 column – 1
21
Set 1 Q8 Program to find the trace of a square matrix
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a,c,n,i,j;
printf("Enter the no of rows of a square matrix");
scanf("%d",&n);
a= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
a[i]= (int*) calloc(n,sizeof(int));
printf("Enter the elements of the matrix");
for (i=0; i<n; i++)
for (j=0; j<n; j++)
{
scanf("%d",&(*(*(a+i)+j)));
}
printf("Elements of first matrix aren");
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
printf("%d",*(*(a+i)+j));
printf("t");
}
printf("n");
}
c=0;
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
22
if (i==j)
c=c+*(*(a+i)+j);
}
}
printf("Trace of a square matrix is %dn ", c);
for (i=0;i<n; i++)
free (a[i]);
free (a);
return 0;
}
Output
Enter the no of rows of a square matrix3
Enter the elements of the matrix7
5
3
1
2
3
4
8
5
Elements of first matrix are
7 5 3
1 2 3
4 8 5
Trace of a square matrix is 14
23
Set 1 Q9 Program to find the sum of each column in a matrix and print its result
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a,*c,n,m,i,j;
printf("Enter the no of rows and columns for matrix");
scanf("%d %d",&n ,&m);
a= (int**) calloc(n,sizeof(int));
for (i=0;i<n; i++)
a[i]= (int*) calloc(m,sizeof(int));
c= (int*) malloc(n*sizeof(int));
printf("Enter the elements of the matrix");
for (i=0; i<n; i++)
for (j=0; j<m; j++)
{
scanf("%d",&(*(*(a+i)+j)));
}
printf("Elements of matrix aren");
for (i=0; i<n; i++)
{
for (j=0; j<m; j++)
{
printf("%d",*(*(a+i)+j));
printf("t");
}
printf("n");
}
for (j=0; j<m; j++)
{
*(c+j)=0;
for (i=0; i<n; i++)
{
24
*(c+j)=*(c+j)+*(*(a+i)+j);
}
}
printf("Sum of each column is n ");
for (j=0;j<m; j++)
{
{
printf("%d",*(c+j));
printf("t");
}
}
printf("n ");
for (i=0;i<n; i++)
free (a[i]);
free (a);
free (c);
return 0;
}
Output
Enter the no of rows and columns for matrix2
3
Enter the elements of the matrix4
5
6
1
2
3
Elements of matrix are
4 5 6
1 2 3
Sum of each column is
5 7 9

More Related Content

What's hot

C programs
C programsC programs
C programs
Vikram Nandini
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
Deyvessh kumar
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Make Mannan
 
Qprgs
QprgsQprgs
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
mohamed sikander
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
mohamed sikander
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
bhaktisagar4
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
Radha Maruthiyan
 
Arrays
ArraysArrays
Arrays
fahadshakeel
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Saket Pathak
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
mustkeem khan
 
Container adapters
Container adaptersContainer adapters
Container adapters
mohamed sikander
 

What's hot (20)

Array notes
Array notesArray notes
Array notes
 
C programs
C programsC programs
C programs
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Qprgs
QprgsQprgs
Qprgs
 
Stl algorithm-Basic types
Stl algorithm-Basic typesStl algorithm-Basic types
Stl algorithm-Basic types
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORYCS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
CS6311- PROGRAMMING & DATA STRUCTURE II LABORATORY
 
Arrays
ArraysArrays
Arrays
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Container adapters
Container adaptersContainer adapters
Container adapters
 
informatics practices practical file
informatics practices practical fileinformatics practices practical file
informatics practices practical file
 

Similar to C programs

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
vrgokila
 
Cpds lab
Cpds labCpds lab
Sorting programs
Sorting programsSorting programs
Sorting programsVarun Garg
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
Rumman Ansari
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
Prof. Dr. K. Adisesha
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
Lakshmi Sarvani Videla
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
sandeep kumbhkar
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointersvinay arora
 
Programs
ProgramsPrograms
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
Arkadeep Dey
 

Similar to C programs (20)

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
 
Programs
ProgramsPrograms
Programs
 
week-21x
week-21xweek-21x
week-21x
 
Vcs17
Vcs17Vcs17
Vcs17
 
Ds
DsDs
Ds
 
Vcs16
Vcs16Vcs16
Vcs16
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 

More from Koshy Geoji

Computer Graphics Report
Computer Graphics ReportComputer Graphics Report
Computer Graphics Report
Koshy Geoji
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
Koshy Geoji
 
C programs Set 3
C programs Set 3C programs Set 3
C programs Set 3
Koshy Geoji
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
Koshy Geoji
 
Vehicle detection in Aerial Images
Vehicle detection in Aerial ImagesVehicle detection in Aerial Images
Vehicle detection in Aerial Images
Koshy Geoji
 
Text mining
Text miningText mining
Text mining
Koshy Geoji
 
Hypothesis test based approach for change detection
Hypothesis test based approach for change detectionHypothesis test based approach for change detection
Hypothesis test based approach for change detection
Koshy Geoji
 
Seminar report
Seminar reportSeminar report
Seminar report
Koshy Geoji
 
73347633 milma-os
73347633 milma-os73347633 milma-os
73347633 milma-osKoshy Geoji
 

More from Koshy Geoji (9)

Computer Graphics Report
Computer Graphics ReportComputer Graphics Report
Computer Graphics Report
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
C programs Set 3
C programs Set 3C programs Set 3
C programs Set 3
 
C programs Set 2
C programs Set 2C programs Set 2
C programs Set 2
 
Vehicle detection in Aerial Images
Vehicle detection in Aerial ImagesVehicle detection in Aerial Images
Vehicle detection in Aerial Images
 
Text mining
Text miningText mining
Text mining
 
Hypothesis test based approach for change detection
Hypothesis test based approach for change detectionHypothesis test based approach for change detection
Hypothesis test based approach for change detection
 
Seminar report
Seminar reportSeminar report
Seminar report
 
73347633 milma-os
73347633 milma-os73347633 milma-os
73347633 milma-os
 

Recently uploaded

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
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
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
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
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
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
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
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
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
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
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
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
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
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
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 

C programs

  • 1. 1 Set 1 Q1 Program to sort the array in ascending order using Selection Sort Algorithm Program #include<stdio.h> #include<stdlib.h> int main() { int i,n,j,temp; int *a; printf("The no of elements "); scanf("%d",&n); a = (int *)malloc (sizeof(int) * n); printf("Enter the elements n" ); for (i=0 ; i<n ; i++) { scanf("%d", &(*(a+i))); } for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if (*(a+i) > *(a+j)) { temp = *(a+i); *(a+i)= *(a+j); *(a+j)=temp; } } } printf("In ascending order "); for (i=0 ; i<n ; i++) { printf("%d " , *(a+i)); } printf("n");
  • 2. 2 free (a); return 0; } Output The no of elements 10 Enter the elements 9 7 55 4 -2 0 5 5 7 30 In ascending order -2 0 4 5 5 7 7 9 30 55
  • 3. 3 Set 1 Q2 Program to sort the array in descending order using the Bubble Sort Algorithm Program #include <stdio.h> #include <stdlib.h> int main() { int *a,i,n,j,temp; printf("Enter the number of elements to be sorted"); scanf("%d",&n); a = (int*) malloc (n*sizeof (int)); printf("Enter the elements "); for(i=0;i<n;i++) { scanf ("%d",&(*(a+i))); } for (i=0; i<(n-1); i++) { for (j=0; j<(n-i-1);j++) { if (*(a+j)<*(a+j+1)) { temp=*(a+j); *(a+j)=*(a+j+1); *(a+j+1)=temp; } } } printf("In descending order: n"); for(i=0;i<n;i++) { printf("%d ",*(a+i)); } free(a); printf("n"); return 0; }
  • 4. 4 Output Enter the number of elements to be sorted10 Enter the elements 15 0 -4 7 8 0 20 15 35 10 In descending order: 35 20 15 15 10 8 7 0 0 -4
  • 5. 5 Set1 Q3 Program to search an element in an array using Linear Search Algorithm Program #include <stdio.h> #include <stdlib.h> int main() { int s, c, n, l, p; int *a,*loc; printf("Enter the number of elements in array"); scanf("%d",&n); a = (int*) malloc (n*sizeof (int)); loc = (int*) malloc (n*sizeof (int)); printf("Enter the numbers "); for (c = 0; c < n; c++) scanf("%d", &(*(a+c))); printf("Enter the number to search"); scanf("%d", &s); l=0; for (c = 0; c < n; c++) { if (*(a+c) == s) { *(loc+l) =c; l=l+1; } } if (l==0) printf("%d is not present in array n", s); else { for (p=0; p<l; p++) printf("n Number is located at position - %d ", *(loc+p)+1); }
  • 6. 6 printf("n"); free (a); free (loc); return 0; } Output 1 Enter the number of elements in array10 Enter the numbers 4 2 3 7 8 1 2 0 9 5 Enter the number to search2 Number is located at position - 2 Number is located at position – 7 Output 2 Enter the number of elements in array5 Enter the numbers 3 5 7 0 2 Enter the number to search1 1 is not present in array
  • 7. 7 Set 1 Q4 Program to search an element in an array using Binary Search Algorithm Program #include <stdio.h> #include <stdlib.h> int main() { int first, last, mid,temp, n,s,i,j,lo,pos; int *a,*loc,*b; printf("Enter number of elements "); scanf("%d",&n); a = (int*) malloc (n* sizeof(int)); b = (int*) malloc (n* sizeof(int)); loc = (int*) malloc (n* sizeof(int)); printf("Enter the elements n" ); for (i=0 ; i<n ; i++) scanf("%d", &(*(a+i))); for (i=0 ; i<n ; i++) *(b+i)=*(a+i); for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if (*(a+i) > *(a+j)) { temp = *(a+i); *(a+i)= *(a+j); *(a+j)=temp; } } } printf("Enter value to find ");
  • 8. 8 scanf("%d", &s); first = 0; last = n - 1; mid = (first+last)/2; while (first <= last) { if (*(a+mid) < s) first = mid + 1; else if (*(a+mid) == s) { lo=mid; break; } else last = mid - 1; mid = (first + last)/2; } pos=0; if (first>last) { printf("Not found! %d is not present in the listn", s); } else { for (j=0;j<n;j++) { if (*(b+j)==*(a+lo)) { *(loc+pos)=j; pos=pos+1; } } for (j=0; j<pos; j++) printf("n Number is located at position - %d ", loc[j]+1); } printf("n");
  • 9. 9 free (a); free (b); free (loc); return 0; } Output 1 Enter number of elements 5 Enter the elements 2 3 4 6 5 Enter value to find 1 Not found! 1 is not present in the list Output 2 Enter number of elements 10 Enter the elements 4 6 -2 7 8 9 6 1 7 7 Enter value to find 7 Number is located at position - 4 Number is located at position - 9 Number is located at position – 10
  • 10. 10 Set 1 Q5 Program to find the Addition of two matrices Program #include <stdio.h> #include <stdlib.h> int main() { int **a,**b,**c,n,m,p,q,i,j; printf("Enter the no of rows and columns for first matrix"); scanf("%d %d",&n ,&m); printf("Enter the no of rows and columns for second matrix"); scanf("%d %d",&p, &q); if ((n==p)&&(m==q)) { a= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) a[i]= (int*) calloc(m,sizeof(int)); b= (int**) calloc(p,sizeof(int)); for (i=0;i<p; i++) b[i]= (int*) calloc(q,sizeof(int)); c= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) c[i]= (int*) calloc(m,sizeof(int)); printf("Enter the elements of first matrix"); for (i=0; i<n; i++) for (j=0; j<m; j++) { scanf("%d",&(*(*(a+i)+j))); } printf("Enter the elements of second matrix"); for (i=0; i<p; i++) for (j=0; j<q; j++) { scanf("%d",&(*(*(b+i)+j)));
  • 11. 11 } printf("Elements of first matrix aren"); for (i=0; i<n; i++) { for (j=0; j<m; j++) { printf("%d",*(*(a+i)+j)); printf("t"); } printf("n"); } printf("Elements of second matrix aren"); for (i=0; i<p; i++) { for (j=0; j<q; j++) { printf("%d",*(*(b+i)+j)); printf("t"); } printf("n"); } for (i=0; i<n; i++) { for (j=0; j<m; j++) { *(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j); } } printf("Addition of matrix isn"); for (i=0; i<n; i++) { for (j=0; j<q; j++) { printf("%dt",*(*(c+i)+j)); } printf("n");
  • 12. 12 } for (i=0;i<n; i++) free (a[i]); free (a); for (i=0;i<p; i++) free (b[i]); free (b); for (i=0;i<n; i++) free (c[i]); free (c); } else printf("Number of columns and rows of both matrix should be equaln"); return 0; } Output Enter the no of rows and columns for first matrix2 3 Enter the no of rows and columns for second matrix2 3 Enter the elements of first matrix-5 -4 -3 -2 -1 0 Enter the elements of second matrix0 1 2 3 4 5 Elements of first matrix are -5 -4 -3
  • 13. 13 -2 -1 0 Elements of second matrix are 0 1 2 3 4 5 Addition of matrix is -5 -3 -1 1 3 5
  • 14. 14 Set 1 Q6 Program to find the Product of two matrices Program #include <stdio.h> #include <stdlib.h> int main() { int **a,**b,**c; int n,m,p,q,i,j,k; printf("Enter the no of rows and columns for first matrix"); scanf("%d %d",&n ,&m); printf("Enter the no of rows and columns for second matrix"); scanf("%d %d",&p, &q); if (m==p) { a= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) a[i]= (int*) calloc(m,sizeof(int)); b= (int**) calloc(p,sizeof(int)); for (i=0;i<p; i++) b[i]= (int*) calloc(q,sizeof(int)); c= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) c[i]= (int*) calloc(q,sizeof(int)); printf("Enter the elements of first matrix"); for (i=0; i<n; i++) for (j=0; j<m; j++) { scanf("%d",&(*(*(a+i)+j))); } printf("Enter the elements of second matrix"); for (i=0; i<p; i++) for (j=0; j<q; j++) { scanf("%d",&(*(*(b+i)+j)));
  • 15. 15 } printf("Elements of first matrix aren"); for (i=0; i<n; i++) { for (j=0; j<m; j++) { printf("%d",*(*(a+i)+j)); printf("t"); } printf("n"); } printf("Elements of second matrix aren"); for (i=0; i<p; i++) { for (j=0; j<q; j++) { printf("%d",*(*(b+i)+j)); printf("t"); } printf("n"); } **c=0; for(i=0;i<n;i++) { for(j=0;j<q;j++) { for(k=0;k<m;k++) { *(*(c+i)+j)=*(*(a+i)+k)**(*(b+k)+j)+*(*(c+i)+j); } } } printf("Product of matrix isn"); for (i=0; i<n; i++) { for (j=0; j<q; j++)
  • 16. 16 { printf("%dt",*(*(c+i)+j)); } printf("n"); } for (i=0;i<n; i++) free (a[i]); free (a); for (i=0;i<p; i++) free (b[i]); free (b); for (i=0;i<n; i++) free (c[i]); free (c); } else printf("Number of columns of first matrix should be equal to number of rows of second matrixn"); return 0; } Output Enter the no of rows and columns for second matrix3 2 Enter the elements of first matrix4 5 6 2 7 8 Enter the elements of second matrix1 2 3 7 5
  • 17. 17 6 Elements of first matrix are 4 5 6 2 7 8 Elements of second matrix are 1 2 3 7 5 6 Product of matrix is 49 79 63 101
  • 18. 18 Set 1 Q7 Program to find largest and smallest element in a matrix and also print their position in the matrix Program #include <stdio.h> #include <stdlib.h> int main() { int n,m,small,large,i,j,p,q,r; int **a,*laloc1,*laloc2,*smloc1,*smloc2; printf("nEnter the Order of the matrix...n"); scanf("%d %d",&n,&m); a= (int**) calloc(n,sizeof(int*)); for(i=0;i<n;i++) a[i]=(int*)calloc(m,sizeof(int)); smloc1=(int*)malloc(m*sizeof(int)); smloc2=(int*)malloc(m*sizeof(int)); laloc1=(int*)malloc(m*sizeof(int)); laloc2=(int*)malloc(m*sizeof(int)); printf("nEnter the value of the matrix...n"); for(i=0;i<n;i++) for(j=0;j<m;j++) scanf("%d",&(*(*(a+i)+j))); printf("n Entered Matrix is: n"); for(i=0;i<n;i++) { for(j=0;j<m;j++) printf("%d ",*(*(a+i)+j)); printf("n"); } small=**a; large=**a; q=0; r=0;
  • 19. 19 for(i=0;i<n;i++) { for(j=0;j<m;j++) { if (small>*(*(a+i)+j)) small=*(*(a+i)+j); if (large<*(*(a+i)+j)) large=*(*(a+i)+j); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(small==*(*(a+i)+j)) { smloc1[q]=i; smloc2[q]=j; q=q+1; } if (large==*(*(a+i)+j)) { laloc1[r]=i; laloc2[r]=j; r=r+1; } } } printf("n Smallest number is %d ",small); for (p=0; p<q;p++) printf ("nLocated at row - %d column - %d ",smloc1[p]+1, smloc2[p]+1) ; printf("n Largest number is %d " ,large ); for (p=0; p<r;p++) printf("n Located at row - %d column - %d", laloc1[p]+1,laloc2[p]+1);
  • 20. 20 printf("n"); for (i=0;i<n; i++) free (a[i]); free (a); free (smloc1); free (smloc2); free (laloc1); free (laloc2); return 0; } Output Enter the Order of the matrix... 3 3 Enter the value of the matrix... 4 5 6 2 3 5 6 1 0 Entered Matrix is: 4 5 6 2 3 5 6 1 0 Smallest number is 0 Located at row - 3 column - 3 Largest number is 6 Located at row - 1 column - 3 Located at row - 3 column – 1
  • 21. 21 Set 1 Q8 Program to find the trace of a square matrix Program #include <stdio.h> #include <stdlib.h> int main() { int **a,c,n,i,j; printf("Enter the no of rows of a square matrix"); scanf("%d",&n); a= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) a[i]= (int*) calloc(n,sizeof(int)); printf("Enter the elements of the matrix"); for (i=0; i<n; i++) for (j=0; j<n; j++) { scanf("%d",&(*(*(a+i)+j))); } printf("Elements of first matrix aren"); for (i=0; i<n; i++) { for (j=0; j<n; j++) { printf("%d",*(*(a+i)+j)); printf("t"); } printf("n"); } c=0; for (i=0; i<n; i++) { for (j=0; j<n; j++) {
  • 22. 22 if (i==j) c=c+*(*(a+i)+j); } } printf("Trace of a square matrix is %dn ", c); for (i=0;i<n; i++) free (a[i]); free (a); return 0; } Output Enter the no of rows of a square matrix3 Enter the elements of the matrix7 5 3 1 2 3 4 8 5 Elements of first matrix are 7 5 3 1 2 3 4 8 5 Trace of a square matrix is 14
  • 23. 23 Set 1 Q9 Program to find the sum of each column in a matrix and print its result Program #include <stdio.h> #include <stdlib.h> int main() { int **a,*c,n,m,i,j; printf("Enter the no of rows and columns for matrix"); scanf("%d %d",&n ,&m); a= (int**) calloc(n,sizeof(int)); for (i=0;i<n; i++) a[i]= (int*) calloc(m,sizeof(int)); c= (int*) malloc(n*sizeof(int)); printf("Enter the elements of the matrix"); for (i=0; i<n; i++) for (j=0; j<m; j++) { scanf("%d",&(*(*(a+i)+j))); } printf("Elements of matrix aren"); for (i=0; i<n; i++) { for (j=0; j<m; j++) { printf("%d",*(*(a+i)+j)); printf("t"); } printf("n"); } for (j=0; j<m; j++) { *(c+j)=0; for (i=0; i<n; i++) {
  • 24. 24 *(c+j)=*(c+j)+*(*(a+i)+j); } } printf("Sum of each column is n "); for (j=0;j<m; j++) { { printf("%d",*(c+j)); printf("t"); } } printf("n "); for (i=0;i<n; i++) free (a[i]); free (a); free (c); return 0; } Output Enter the no of rows and columns for matrix2 3 Enter the elements of the matrix4 5 6 1 2 3 Elements of matrix are 4 5 6 1 2 3 Sum of each column is 5 7 9