C Chapter 10
C Chapter 10
Create a structure to specify data on students given below: Roll number, Name,
Department, Course, Year of joining Assume that there are not more than 450
students in the collage. (a) Write a function to print names of all students who
joined in a particular year. (b) Write a function to print the data of a student
whose roll number is given.
void main()
{
char j[1];
int yearc,rollc,i;
struct book
{
int roll;
char name[20];
char department[20];
char course[20];
int year;
};
struct book b[5]={
{1,"MrA","Science","Physics",2009},
{2,"MrB","Science","Maths",2008},
{3,"MrC","Arts","History",2009},
{4,"MrsD","Arts","History",2010},
{5,"MrsE","Science","Maths",2009}
};
while(1)
{
clrscr();
printf("\nEnter a year(2000 or 2001)\n");
scanf("%d",&yearc);
for(i=0;i<5;i++)
{
if(yearc==b[i].year)
printf("\nName: %s Year: %d",b[i].name,b[i].year);
}
printf("\nEnter a roll number(1-5)\n");
scanf("%d",&rollc);
for(i=0;i<5;i++)
{
if(rollc==b[i].roll)
{
printf("\nRoll number: %d, Name: %s, Department: %s\nCourse: %s,Year:
%d",b[i].roll,b[i].name,b[i].department,b[i].course,b[i].year);
break;
}
}
printf("\nPress q to quit or any key to continue\n");
scanf("%s",j);
if(j[0]==113)
break;
}
}
linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
{
int year;
char material[20];
int qty;
};
struct parts p[27]={
{1991,"rubber",5},{1992,"rubber",6},{1991,"metal",7},{1997,"wood",8},
{1993,"plastic",9},{1992,"wood",6},{1997,"metal",6},{1992,"rubber",7},
{1991,"wood",2},
{1992,"metal",6},{1995,"wood",4},{1992,"rubber",5},{1998,"wood",2},
{1997,"plastic",8},{1998,"plastic",9},{1998,"metal",8},{1991,"metal",8},
{1992,"wood",5},
{1993,"wood",8},{1993,"rubber",7},{1997,"metal",2},{1998,"wood",1},
{1999,"plastic",8},{1995,"wood",6},{1990,"rubber",3},{1997,"plastic",9},
{1998,"wood",7}
};
/* for(i=0,letter=65;i<2;i++,letter++)
{
for(j=0;j<9;j++)
{
printf("\Key in values for year,material and quantity respectively for part %c%c
%d\n",letter,letter,j+1);
scanf("%d%s%d",&p[i][j].year,&p[i][j].material,&p[i][j].qty);
}
} */
while(1)
{
clrscr();
printf("\nKey in the part number to retrieve the information from\n");
scanf("%s",m);
printf("\nKey the part number till the information is retrieved(End)\n");
scanf("%s",n);
a=((m[0]-65)*9+(m[2]-49));
b=((n[0]-65)*9+(n[2]-49));
printf("\na is %d,\nb is %d,\nm[0] is %d, n[0] is %d",a,b,m[0],n[0]);
c=a;
d=a;
while(c>=9)
{
c-=9;
}
printf("\nc after while is %d",c);
while( d>9)
d-=9;
for(i=a;i<=b;i++,c++,d++)
{
if(c==9)
{
m[0]+=1;
c=0;
}
if(d==9)
{
d=0;
}
printf("\nPart: %c%c%d, Year: %d, Material: %s, Quantity:
%d",m[0],m[0],d+1,p[i].year,p[i].material,p[i].qty);
}
This program doesn't check whether your date is correct so even if you put 99990001 it is
still valid.
void main()
{
int i,k;
char date[8];
char j[1];
struct em
{
int code;
char name[20];
char datej[8];
};
struct em e[5];
for(i=0;i<5;i++)
{
printf("\nKey in code, name and date(ddmmyyy) of employment for %d) employee\n",i+1);
scanf("%d%s%s",&e[i].code,e[i].name,e[i].datej);
if(strlen(e[i].datej)!=8)
{
printf("\nYou keyed the date wrongly, key again");
i--;
continue;
}
}
while(1)
{
clrscr();
printf("\nEnter current date\n");
scanf("%s",date);
for(i=0;i<5;i++)
{
k=0;
k=funccheck(date,e[i].datej);
if(k==1)
printf("\nEmployee code= %d Employee name= %s has tenure more than or equal 3
years",e[i].code,e[i].name);
}
printf("\nKey q to quit or any other key to continue\n");
scanf("%s",j);
if(j[0]==113)
break;
}
}
funccheck(char *date,char *datej)
{
int a,a1,i,t;
for(i=7,t=1,a=0;i>3;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=7,t=1,a1=0;i>3;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>3)
return(1);
else if(a-a1<0)
return(0);
for(i=3,a=0,t=1;i>1;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=3,a1=0,t=1;i>1;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>0)
return(1);
else if(a-a1<0)
return(0);
for(i=1,a=0,t=1;i>=0;i--,t*=10)
{
a=a+((*(date+i)-48)*t);
}
for(i=1,a1=0,t=1;i>=0;i--,t*=10)
{
a1=a1+((*(datej+i)-48)*t);
}
if(a-a1>=0)
return(1);
else if(a-a1<0)
return(0);
}
void main()
{
int i,f=0;
struct date
{
int date;
int month;
int year;
};
struct date d[2];
for(i=0;i<2;i++)
{
printf("\nEnter day for the %d) date\n",i+1);
scanf("%d",&d[i].date);
printf("\nEnter the month for the %d) date\n",i+1);
scanf("%d",&d[i].month);
printf("\nEnter the year for the %d) date\n",i+1);
scanf("%d",&d[i].year);
}
if(d[0].date==d[1].date)
{
if(d[0].month==d[1].month)
{
if(d[0].year==d[1].year)
{
f=1;
}
}
}
if(f==1)
printf("\nThe dates are equal");
else
printf("\nThe dates are not equal");
}
#define N 450
struct students {
int rlnm;
char name[25];
char dept[25]; /* structure defined outside of main(); */
char course[25];
int year;
};
void main() {
/* main() */
clrscr();
switch(ch) {
case 1:
clrscr();
dispyr(&s); /* function call to display names of students who joined in\
a particular year */
break;
case 2:
clrscr();
disprl(&s); /* function call to display information of a student \
whose roll number is given */
break;
default:
printf("\n\nerror! wrong choice");
}
getch();
}
/******************* main() ends **************/
dispyr(struct students *a) { /* function for displaying names of students\
who took admission in a particular year */
int j,yr;
printf("\nenter year: ");
scanf("%d",&yr);
printf("\n\nthese students joined in %d\n\n",yr);
for(j=0;j<N;j++) {
if(a[j].year==yr) {
printf("\n%s\n",a[j].name);
}
}
return 0;
}
return 0;
}
--------------------------------------------------------------------------------------------------------------
void main() {
struct bank b[N];
int i,ch,lw=100,ch2,ac,am;
clrscr();
for(i=0;i<N;i++) {
scanf("%d",&ch);
switch(ch) {
case 1:
clrscr();
disp(&b);
break;
case 2:
clrscr();
printf("enter your account number: ");
scanf("%d",&ac);
for(i=0;i<N;i++) {
if((b[i].acn)==ac) {
clrscr();
printf("\tHello %s\n",b[i].name);
printf("\n\n");
printf("\n\nenter your choice\n");
printf("\n1: deposite:\n");
printf("\n0: withdrawl:\n\n");
scanf("%d",&ch2);
switch(ch2) {
case 0:
clrscr();
if(b[i].bal<lw) {
printf("\n\nsorry! account balance is too low for withdrawl.\n");
break;
}
else {
printf("\n\nenter amount for withdrawl: ");
scanf("%d",&am);
}
if(b[i].bal<am) {
else {
b[i].bal=b[i].bal+am;
printf("\n\nwithdrawl was successful.\n");
}
break;
case 1:
clrscr();
printf("\n\nenter amount to deposite: ");
scanf("%d",&am);
b[i].bal=b[i].bal+am;
printf("\n\ncash deposited successfully.\n\n");
break;
}
}
}
}
getch();
}
disp(struct bank *a) {
int k;
printf("\tCustomers whose balance is below 100:\n");
printf("\t*************************************\n\n");
for(k=0;k<N;k++) {
if((a[k].bal)<100) {
printf("%2d\t%s\n",a[k].acn,a[k].name);
}
}
return 0;
}
-----------------------------------------------------------------------------------------------------------------
{"AA6",2009,"AAA",10},
{"AA7",2009,"AAA",10},
{"AA8",2009,"AAA",10},
{"AA9",2009,"AAA",10},
{"BB1",2010,"BBB",11},
{"BB2",2010,"BBB",11},
{"BB3",2010,"BBB",11},
{"BB4",2010,"BBB",11},
{"BB5",2010,"BBB",11},
{"BB6",2010,"BBB",11},
{"BB7",2010,"BBB",11},
{"BB8",2010,"BBB",11},
{"BB9",2010,"BBB",11},
{"CC1",2011,"CCC",12},
{"CC2",2011,"CCC",12},
{"CC3",2011,"CCC",12},
{"CC4",2011,"CCC",12},
{"CC5",2011,"CCC",12},
{"CC6",2011,"CCC",12},
{"CC7",2011,"CCC",12},
{"CC8",2011,"CCC",12},
{"CC9",2011,"CCC",12},
{"DD1",2012,"DDD",13},
{"DD2",2012,"DDD",13},
{"DD3",2012,"DDD",13},
{"DD4",2012,"DDD",13},
{"DD5",2012,"DDD",13},
{"DD6",2012,"DDD",13},
{"DD7",2012,"DDD",13},
{"DD8",2012,"DDD",13},
{"DD9",2012,"DDD",13},
{"EE1",2013,"EEE",14},
{"EE2",2013,"EEE",14},
{"EE3",2013,"EEE",14},
{"EE4",2013,"EEE",14},
{"EE5",2013,"EEE",14},
{"EE6",2013,"EEE",14},
{"EE7",2013,"EEE",14},
{"EE8",2013,"EEE",14},
{"EE9",2013,"EEE",14},
{"FF1",2014,"FFF",15},
{"FF2",2014,"FFF",15},
{"FF3",2014,"FFF",15},
{"FF4",2014,"FFF",15},
{"FF5",2014,"FFF",15},
{"FF6",2014,"FFF",15},
{"FF7",2014,"FFF",15},
{"FF8",2014,"FFF",15},
{"FF9",2014,"FFF",15}};
int i,j,k;
clrscr();
}
}
getch();
}
-----------------------------------------------------------------------------------------------------------------
#include<conio.h>
#include<string.h>
void main() {
struct cricketer {
char name[50];
int age;
int match;
float avrn;
};
struct cricketer c[20],temp;
int i,j;
clrscr();
for(i=0;i<20;i++) {
printf("Enter data of cricketer %d\n\n\n",i+1);
fflush(stdin);
printf("Name: ");
gets(c[i].name);
printf("\n\nAge: ");
scanf("%d",&c[i].age);
printf("\n\nMatches: ");
scanf("%d",&c[i].match);
printf("\n\nAverage runs: ");
scanf("%f",&c[i].avrn);
clrscr();
}
/*******************/
/* sorting records */
/*******************/
for(i=0;i<20;i++) {
for(j=i+1;j<20;j++) {
#include<stdio.h>
#include<conio.h>
#define N 3
void main() {
struct employee {
int code;
char name[20];
int dd,mm,yy;
} e[N];
int d,m,y,i;
clrscr();
for(i=0;i<N;i++) {
printf("\tEnter employee data:\n");
printf("\t*********************\n");
printf("\nEnter employee code: ");
scanf("%d",&e[i].code);
printf("\n\nEnter employee name: ");
scanf("%s",&e[i].name);
printf("\n\nEnter date of joining (dd mm yy): ");
scanf("%d%d%d",&e[i].dd,&e[i].mm,&e[i].yy);
clrscr();
}
clrscr();
printf("\nenter current date(dd mm yy): \n\n");
scanf("%d%d%d",&d,&m,&y);
clrscr();
for(i=0;i<N;i++) {
if((y-(e[i].yy))>=3) {
printf("%s\n\N",e[i].name);
}
getch();
}
-----------------------------------------------------------------------------------------------------------------
void main() {
struct library {
int acn;
char title[25];
char auth[25];
float price;
int flag;
};
struct library a[5]={
{2,"AAA","AAA",154.8,1},
{1,"BBB","BBB",124.6,0},
{5,"EEE","EEE",234.3,0},
{3,"CCC","CCC",232.3,1},
{4,"DDD","DDD",121.3,0}
};
struct library temp; /* temporary structure to overwrite information /
and for assistance in sorting the whole structure */
int i,ch,k,flag,j=0;
char author[10];
clrscr();
while(1) {
clrscr();
printf("\t1: Add book information\n");
printf("\t2: Display book information\n");
printf("\t3: List all books of given author\n");
printf("\t4: List the title of specified book\n");
printf("\t5: List the count of books in the library\n");
printf("\t6: List the books in order of accesion number\n");
printf("\t7: Exit\n\n\n");
printf("Choice: ");
scanf("%d",&ch);
switch(ch) {
case 1:
clrscr();
printf("\t\t1: Add book information:\n\n\n");
case 2:
clrscr();
printf("\t\t2: Display book information:\n\n\n");
printf("Enter accesion number: ");
scanf("%d",&k);
for(i=0;i<5;i++) {
if(k==a[i].acn) {
clrscr();
printf("Book information: \n\n");
printf("\n\nAccesion Number: %d",a[i].acn);
printf("\nTitle: %s",a[i].title);
printf("\nAuthor: %s",a[i].auth);
printf("\nPrice: %f",a[i].price);
if(a[i].flag==0) {
printf("\nFlag: Not issued\n\n");
}
else{
printf("\nFlag: Issued\n\n");
}
}
}
delay(1000);
break;
clrscr();
printf("\t\t3: List all books of given author:\n\n\n");
printf("Enter author name: ");
scanf("%s",&author); fflush(stdin);
printf("\n\nbooks of %s\n\n",author);
for(i=0;i<5;i++) {
k=strcmp(author,a[i].auth);
if(k==0) {
j=j+1;
printf("%d.\t%s\n",j,a[i].title);
}
}
delay(2000);
break;
case 4:
clrscr();
printf("\t\t4: Title of the specified book:\n\n\n");
printf("Enter the accesion number: ");
scanf("%d",&k);
printf("\n\n");
for(i=0;i<5;i++) {
if(k==a[i].acn) {
printf("Title: %s\n",a[i].title);
}
}
delay(2000);
break;
case 5:
clrscr();
printf("\t\t5: List the count of books in the library: \n\n\n");
for(i=0;i<5;i++) {
j=j+1;
}
printf("\tTotal books: %2d",j);
delay(2000);
break;
clrscr();
printf("6: List the books in order of accesion number: \n\n\n");
for(i=0;i<5;i++) {
for(j=i+1;j<4;j++) {
if(a[i].acn>a[j].acn) {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Access no.\tTitle\tAuthor\tFlag\n\n\n");
for(i=0;i<5;i++) {
printf("%4d\t\t%5s\t%5s\t",a[i].acn,a[i].title,a[i].auth);
if(a[i].flag==0)
printf("not issued\n\n");
else
printf("issued\n\n");
}
delay(2000);
break;
case 7:
exit();
}
}
}
linkfloat() { /* special function to solve compilar error */
float a=0,*b; /* not used withing the program but still defined */
b=&a;
a=*b;
return 0;
}
------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------
case 'y':
clrscr();
while(choice=='y' || choice=='Y') {
printf("\nEnter element %d: ",++count);
temp=(linklist *)malloc(sizeof(linklist));
scanf("%d",&temp->data);
temp->link=first;
first=temp;
case 'n':
break;
}
getch();
}
-----------------------------------------------------------------------------------------------------------------