C programming:- Arrays,Strings,Structures,Unions - Part 3
C program to calculate determinant
of a 3X3 matrix
#include<stdio.h>
int
main(){
int a[3][3],i,j;
int determinant=0;
printf("Enter the 9 elements of matrix: ");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++)
printf("%d\t",a[i][j]);
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] -
a[1][(i+2)%3]*a[2][(i+1)%3]));
printf("\nDeterminant of matrix is: %d",determinant);
return 0;
}
C code for Determinant of 2X2
matrix:
#include<stdio.h>
int
main(){
int a[2][2],i,j;
long determinant;
printf("Enter the 4 elements of matrix: ");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("\nThe matrix is\n");
for(i=0;i<2;i++){
printf("\n");
for(j=0;j<2;j++)
printf("%d\t",a[i][j]);
}
determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];
printf("\nDeterminant of 2X2 matrix: %ld",determinant);
return 0;
}
/*
WAP
to read a nXm matrix and find:
a. The
average of each row.
b. Average
of each column
c. Average
of all mn entries.
*/
#include<stdio.h>
main()
{
int a[10][10];
int n,m,i,j,k,sum,sumr,sumc;
printf("enter order of a
matrix:");
scanf("%d%d",&m,&n);
printf("enter elements:\n");
sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
sum=sum+a[i][j];
}
}
printf("total sum of al mn
entries:%d avg=%d\n",sum,sum/(m*n));
for(i=0;i<m;i++)
{sumr=0;
for(j=0;j<n;j++)
{
sumr=sumr+a[i][j];
}
printf("row %d sum=%d
row avg= %d\n",i+1,sumr,sumr/n);
}
for(i=0;i<m;i++)
{sumc=0;
for(j=0;j<n;j++)
{
sumc=sumc+a[j][i];
}
printf("col %d sum=%d
col avg=%d\n",i+1,sumc,sumc/m);
}
}
/*OUTPUT
enter
order of a matrix:3 3
enter
elements:
1
2 3
4
5 6
7
8 9
total
sum of al mn entries:45 avg=5
row
1 sum=6 row avg= 2
row
2 sum=15 row avg= 5
row
3 sum=24 row avg= 8
col
1 sum=12 col avg=4
col
2 sum=15 col avg=5
col
3 sum=18 col avg=6
*/
/* sort letters in a given string
*/
#include<stdio.h>
#include<string.h>
main()
{
int
i,j,n;
char ch[30],tmp;
printf("Enter
string : ");
gets(ch);
n=strlen(ch);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(
ch[j] < ch[j+1])
{
tmp = ch[j];
ch[j] = ch[j+1];
ch[j+1]
= tmp;
}
}
}
printf("\n\nAfter
Sorting in descending order\n");
printf("\n%s",ch);
}
/*OUTPUT
Enter
string : hello
After
Sorting in descending order
ollhe
*/
Define a structure called STUDENT
that will describe the following
information:Std_name,class,reg_no,sub_marks,and total. Using STUDENT, declare
an array stu_list with 60 elements. WAP to read the information about all the
60 students and to display the information.
#include<stdio.h>
struct
student
{
char stdname[20];
char class[10];
int regno;
int submarks[6];
int total;
}stu_list[60];
main()
{
int i,j,n;
printf("enter limit of
students:");
scanf("%d",&n);
printf("enter students
details\n");
for(i=0;i<n;i++)
{
printf("enter name
, class
,regno ");
scanf("%s%s%d",stu_list[i].stdname,stu_list[i].class,&stu_list[i].regno);
printf("enter 6 subjects
marks:");
stu_list[i].total=0;
for(j=0;j<6;j++)
{
scanf("%d",&stu_list[i].submarks[j]);
stu_list[i].total=stu_list[i].total+stu_list[i].submarks[j];
}
}
printf("\nstudent
Record\n");
for(i=0;i<n;i++)
{
printf("student %d
details \n",i+1);
printf("name=%s\tclass=
%s\tregno=%d\n",stu_list[i].stdname,stu_list[i].class,stu_list[i].regno);
printf("6 subjects
marks:");
for(j=0;j<6;j++)
{
printf("\nsubject
%d= %d\n
",j+1,stu_list[i].submarks[j]);
}
printf("total
marks:%d",stu_list[i].total);
}
}
/*
enter
limit of students:1
enter
students details
enter
name , class ,regno
aa
s3 111
enter
6 subjects marks:
23
24 25 26 27 28
student
Record
student
1 details
name=aa class= s3 regno=111
6
subjects marks:
subject
1= 23
subject
2= 24
subject
3= 25
subject
4= 26
subject
5= 27
subject
6= 28
total marks:153student@ubuntu:~$
*/
/*
7. Define
a structure that describes the set of books in a library. For each book, the
members are name of author,publisher,rate and branch of information. WAP to
print i) a list of books supplied by a publisher ii) a list of books in a
particular branch
*/
#include<stdio.h>
#include<stdlib.h>
struct
library
{
char aname[20];//author name
char pub[10];//publisher name
char bname[30];//book name
char bookbranch[20];
int rate;
}book[30];
main()
{
int i,c,n,l=0;
char pname[20],bnam[20];
printf("enter no of books:");
scanf("%d",&n);
printf("enter books
details\n");
for(i=0;i<n;i++)
{
printf("enter author
name,publisher,bookname,bookbranch,rate\n");
scanf("%s%s%s%s%d",book[i].aname,book[i].pub,book[i].bname,book[i].bookbranch,&book[i].rate);
}
printf("\nBook Record\n");
for(i=0;i<n;i++)
{
printf("book %d details
\n",i+1);
printf("author\tpublisher\tbookname\tbookbranch\trate\n");
printf("%s\t%s\t%s\t%s\t%d\n",book[i].aname,book[i].pub,book[i].bname,book[i].bookbranch,book[i].rate);
}
do
{
printf("MENU\n 1) search by
publisher \n 2) search by branch\n 3)exit\n enter choice:");
scanf("%d",&c);
switch(c)
{
case 1: //a list of books
supplied by a publisher
printf("enter
publisher name:");
scanf("%s",pname);
for(i=0;i<n;i++)
{
if(strcmp(book[i].pub,pname)==0)
{
printf("author\tpublisher\tbookname\tbookbranch\trate\n");
printf("%s\t%s\t%s\t%s\t%d\n",book[i].aname,book[i].pub,book[i].bname,book[i].bookbranch,book[i].rate);
}
}
break;
case 2: // a list of books in
a particular branch
printf("enter
branch name:");
scanf("%s",bnam);
for(i=0;i<n;i++)
{
if(strcmp(book[i].bookbranch,bnam)==0)
{
printf("author\tpublisher\tbookname\tbookbranch\trate\n");
printf("%s\t%s\t%s\t%s\t%d\n",book[i].aname,book[i].pub,book[i].bname,book[i].bookbranch,book[i].rate);
}
}
break;
case 3: exit(0);
default:printf("invalid
choice");
}
printf("press 1 to
continue:");
scanf("%d",&l);
}while(l==1);
}
/*enter
no of books:3
enter
books details
enter
author name,publisher,bookname,bookbranch,rate
^C
student@ubuntu:~$
./a.out
enter
no of books:3
enter
books details
enter
author name,publisher,bookname,bookbranch,rate
balaguruswamy pearson pgmmininc computer 200
enter
author name,publisher,bookname,bookbranch,rate
byron pear pscp computer 300
enter
author name,publisher,bookname,bookbranch,rate
yashwanth pearson cp computer 340
Book
Record
book
1 details
author publisher bookname bookbranch rate
balaguruswamy pearson pgmmininc computer 200
book
2 details
author publisher bookname bookbranch rate
byron pear pscp computer 300
book
3 details
author publisher bookname bookbranch rate
yashwanth pearson cp computer 340
MENU
1) search by publisher
2) search by branch
3)exit
enter choice:1
enter
publisher name:pearson
author publisher bookname bookbranch rate
balaguruswamy pearson pgmmininc computer 200
author publisher bookname bookbranch rate
yashwanth pearson cp computer 340
press
1 to continue:1
MENU
1) search by publisher
2) search by branch
3)exit
enter choice:2
enter
branch name:computer
author publisher bookname bookbranch rate
balaguruswamy pearson pgmmininc computer 200
author publisher bookname bookbranch rate
byron pear pscp computer 300
author publisher bookname bookbranch rate
yashwanth pearson cp computer 340
*/
Given two one-dimensional arrays A
& B. Read them and sort in ascending order. Then merge them in to a single
sorted array C that contains every item from arrays A and B in ascending order.
#include<stdio.h>
void
sort(int p[30],int q);
void
disp(int p[30],int q);
void
merge(int p[30],int r[30],int s1,int s2);
int
a[30],b[30],c[60];
main()
{
int i,j,n1,n2,m;
printf("enter array 1
size:");
scanf("%d",&n1);
printf("enter array 2
size:");
scanf("%d",&n2);
printf("enter array 1
elements:");
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("enter array 2
elements:");
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
sort(a,n1);
printf("array 1 elements after
sorting\n");
disp(a,n1);
sort(b,n2);
printf("\narray 2 elements after
sorting\n");
disp(b,n2);
merge(a,b,n1,n2);
printf("\narray 3 elements after
merging\n");
disp(c,n1+n2);
sort(c,n1+n2);
printf("\narray 3 elements after
sorting\n");
disp(c,n1+n2);
}
void
sort(int p[30],int q)
{
int i,j,t;
for(i=0;i<q-1;i++)
{
for(j=0;j<q-1;j++)
{
if(p[j]>p[j+1])
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
}
}
void
disp(int p[30],int q)
{
int i;
for(i=0;i<q;i++)
printf("%d ",p[i]);
}
void
merge(int p[30],int r[30],int s1,int s2)
{
int i,j,s;
s=s1+s2;
j=0;
for(i=0;i<s;i++)
{
if(i<s1)
c[i]=p[i];
else
{c[i]=r[j];j++;}
}
}
/*
OUTPUT
student@ubuntu:~$
./a.out
enter
array 1 size:3
enter
array 2 size:3
enter
array 1 elements:1 55 33
enter
array 2 elements:3 77 44
array
1 elements after sorting
1
33 55
array
2 elements after sorting
3
44 77
array
3 elements after merging
1
33 55 3 44 77
array
3 elements after sorting
1
3 33 44 55 77
*/
Read the employee number,name and
salary of 500 employees in a firm. Prepare the payroll in the ascending order
of salaries and print it. .(MG University NOV 2010)
/*Read
the employee number,name and salary of 500 employees in a firm. Prepare the
payroll in the ascending order of salaries and print it. .(NOV 2010)*/
#include<stdio.h>
#include<string.h>
struct
employee
{
int empno;
char name[30];
int sal;
}emp[500],t;
main()
{
int n,i,j;
printf("enter emp limit:");
scanf("%d",&n);
printf("enter %d employees
empno,emp name and salary:",n);
for(i=0;i<n;i++)
{
scanf("%d%s%d",&emp[i].empno,emp[i].name,&emp[i].sal);
}
printf("employee database based
on salary order:\n");
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(emp[j].sal>emp[j+1].sal)
{
t=emp[j];
emp[j]=emp[j+1];
emp[j+1]=t;
}
}
}
printf("
empno\tempname\tempsal\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\n",emp[i].empno,emp[i].name,emp[i].sal);
}
printf("employee database based
on name order:\n");
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(strcmp(emp[j].name,emp[j+1].name)>0)
{
t=emp[j];
emp[j]=emp[j+1];
emp[j+1]=t;
}
}
}
printf("
empno\tempname\tempsal\n");
for(i=0;i<n;i++)
{
printf("%d\t%s\t%d\n",emp[i].empno,emp[i].name,emp[i].sal);
}
}
/*
enter
emp limit:5
enter
5 employees empno,emp name and salary:
111
arun 4500
123
anu 3000
231
athira 200
34 arjun 2300
45 basil 23000
employee
database based on salary order:
empno empname empsal
231 athira 200
34 arjun 2300
123 anu 3000
111 arun 4500
45 basil 23000
employee
database based on name order:
empno empname empsal
123 anu 3000
34 arjun 2300
111 arun 4500
231 athira 200
45 basil 23000
Comments
Post a Comment