Chapter: Structure | Let us C solution with details description and tutorials | yashwant kanetkar
[D] Attempt the following:
(a) 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.
Show Solutions
Hide Solutions
/* d (a) 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.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
int main() //main function
{
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,"A","Science","Physics",2011},
{2,"B","Science","Maths",2012},
{3,"C","Arts","History",2010},
{4,"D","Arts","History",2009},
{5,"E","Science","Maths",2011}
};
while(1)
{
printf("\n Enter a year \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;
}
getch();
return 0; // return int value
}
(b) Create a structure to specify data of customers in a bank. The data to be stored is: Account number, Name, Balance in account. Assume maximum of 200 customers in the bank.
(a) Write a function to print the Account number and name of each customer with balance below Rs. 100.
(b) If a customer request for withdrawal or deposit, it is given in the form:
Acct. no, amount, code (1 for deposit, 0 for withdrawal) Write a program to give a message, "The balance is insufficient for the specified withdrawal".
Show Solutions
Hide Solutions
Contents goes here...
(c) An automobile company has serial number for engine parts starting from AA0 to FF9. The other characteristics of parts to be specified in a structure are: Year of manufacture, material and quantity manufactured.
(a) Specify a structure to store information corresponding to a part.
(b) Write a program to retrieve information on parts with serial numbers between BB1 and CC6.
Show Solutions
Hide Solutions
/* d (c) An automobile company has serial number for engine parts starting from AA0 to FF9.
The other characteristics of parts to be specified in a structure are: Year of
manufacture, material and quantity manufactured.
(a) Specify a structure to store information corresponding to a part.
(b) Write a program to retrieve information on parts with serial numbers between BB1 and CC6.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
int i,j,letter,a,b,c,d;
char m[3],n[3],q[1];
struct parts
{
int year;
char material[20];
int qty;
};
struct parts p[27]={
{2009,"blade",5},{2006,"blade",6},{2007,"steel",7},{1997,"bamboo",8},{1993,"plastic",9},{1992,"bamboo",6},{2004,"steel",6},{1992,"blade",7},{2001,"bamboo",2},
{2007,"steel",6},{2005,"bamboo",4},{2007,"blade",5},{2007,"bamboo",2},{2004,"plastic",8},{2008,"plastic",9},{2009,"steel",8},{2001,"steel",8},{2002,"bamboo",5},
{2005,"bamboo",8},{2012,"blade",7},{2007,"steel",2},{2008,"bamboo",1},{2009,"plastic",8},{2005,"bamboo",6},{2010,"blade",3},{2004,"plastic",9},{2009,"bamboo",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)
{
printf("\n Key in the part number to get the information from. \n");
scanf("%s",m);
printf("\n Key the part number until 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("\n a is %d,\n b is %d,\n m[0] is %d, n[0] is %d.",a,b,m[0],n[0]);
c=a;
d=a;
while(c>=9)
{
c-=9;
}
printf("\n c 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("\n Part: %c%c%d, Year: %d, Material: %s, Quantity: %d",m[0],m[0],d+1,p[i].year,p[i].material,p[i].qty);
printf("\n Key q to quit or any other key to continue\n");
scanf("%s",q);
if(q[0]==113) // q ascii value
break;
}
}
getch();
return 0; // return int value
}
(d) A record contains name of cricketer, his age, number of test matches that he has played and the average runs that he has scored in each test match. Create an array of structure to hold records of 20 such cricketer and then write a program to read these records and arrange them in ascending order by average runs. Use the qusort( ) standard library function.
Show Solutions
Hide Solutions
/* d (d) A record contains name of cricketer, his age, number of test matches
that he has played and the average runs that he has scored in each test
match. Create an array of structure to hold records of 20 such cricketer
and then write a program to read these records and arrange them in ascending
order by average runs. Use the qusort( ) standard library function.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
// structure:
struct record {
char name[20];
int age;
int test;
int run;
};
funcsort(struct record *a,int j) { // functions
int i,k;
struct record a2[2];
for (i=0; i<j ; i++)
{
for(k=i+1;k<j;k++)
{
if(a[i ].run>a[k].run) {
a2[0]=a[i];
a2[1]=a[k];
a[i]=a2[1];
a[k]=a2[0];
} // end if statement
} // inner for loop
} // outer for loop
}
int main() //main function
{
int i;
struct record a[5]={
{"A",22,15,8}, {"B",22,33,2}, {"C",33,234,7}, {"D",22,130,6}, {"E",90,1001,11}
};
funcsort(&a,5);
printf("\n");
for(i=0;i<5;i++)
printf("\n Average Runs = %d, Name = %s, Age = %d, Number of test matches = %d",a[i].run,a[i].name,a[i].age,a[i].test);
getch();
return 0; // return int value
}
(e) There is a structure called employee that holds information like employee code, name, date of joining. Write a program to create an array of the structure and enter some data into it. Then ask the user to enter current date. Display the names of those employees whose tenure is 3 or more than 3 years according to the given current date.
Show Solutions
Hide Solutions
/* d (e) There is a structure called employee that holds information like employee
code, name, date of joining. Write a program to create an array of the structure
and enter some data into it. Then ask the user to enter current date. Display the
names of those employees whose tenure is 3 or more than 3 years according to the
given current date.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
struct employee
{
char name[40];
int code,doj,moj,yoj;
};
struct employee e[3];
int i,d,m,y,yr;
for(i=0;i<3;i++)
{
printf("Enter employee name & code\n");
gets(e[i].name);
scanf("%d",&e[i].code);
printf("Enter date of joining dd/mm/yy\n");
scanf("%d/%d/%d",&e[i].doj,&e[i].moj,&e[i].yoj);
printf("Enter current date dd/mm/yy\n");
scanf("%d/%d/%d",&d,&m,&y);
//d=15; m=1; y=12;
yr=y-e[i].yoj;
if(yr>3)
{
printf("%s",e[i].name);
continue;
}
if(yr==3)
if(e[i].moj>m)
{
printf("%s",e[i].name);
continue;
}
if(e[i].moj==m)
if(e[i].doj>=d)
printf("%s",e[i].name);
}
getch();
return 0; // return int value
}
(f) Write a menu driven program that depicts the working of a library. The menu options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the book, author name, price of the book, and flag indicating whether book is issued or not.
Show Solutions
Hide Solutions
/* d (f) Write a menu driven program that depicts the working of a library. The menu
options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number
7. Exit
Create a structure called library to hold accession number, title of the book,
author name, price of the book, and flag indicating whether book is issued or not.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
struct lib
{
int accession,flag;
float price;
char name[20],authname[20];
};
struct lib l[20];
int ch,i=0,acc,j=0,x;
char author[20];
while(x=1)
{
printf("What do u want to do?.\n");
printf("1.Add book information.\n");
printf("2.Display book information.\n");
printf("3.List all books of given author.\n");
printf("4.List the title of specified book.\n");
printf("5.List the count of books in the library.\n");
printf("6.List the books in the order of accession no.\n");
printf("7.Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the name of the book.\n");
fflush(stdin);
gets(l[j].name);
printf("Enter the author name.\n");
fflush(stdin);
gets(l[j].authname);
printf("Enter price.\n");
scanf("%f",&l[j].price);
printf("press 0 if book is issued and 1 if available.\n");
scanf("%d",&l[j].flag);
printf("Record added successfully.\n");
l[j].accession=j;
j++;
break;
}
case 2:
{
for(i=0;i<j;i++)
puts(l[i].name);
printf(" ");
puts(l[i].authname);
printf("%f ",l[i].price);
if(l[i].flag==0)
printf("Book available.\n");
else
printf("Book not available.\n");
break;
}
case 3:
{
printf("Enter the name of author\n");
gets(author);
for(i=0;i<j;i++)
{
if(l[i].authname==author)
{
puts(l[i].name);
printf("\n");
}
}
break;
}
case 4:
{
printf("Please enter the accession no. of the book \n");
scanf("%d",&acc);
for(i=0;i<j;i++)
if(l[i].accession==acc)
{
puts(l[i].name);
printf("\n");
}
break;
}
case 5:
{
printf("%d",j);
break;
}
case 6:
{
for(i=0;i<j;i++)
{
puts(l[i].name);
printf("\n");
}
break;
}
case 7:
{
x=2;
break;
}
}
}
getch();
return 0; // return int value
}
(g) Write a program that compares two given dates. To store date use structure say date that contains three members namely date, month and year. If the dates are equal then display message as "Equal" otherwise "Unequal".
Show Solutions
Hide Solutions
/* d (g) Write a program that compares two given dates. To store date use structure
say date that contains three members namely date, month and year. If the dates
are equal then display message as "Equal" otherwise "Unequal".
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
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("\n The dates are equal.");
else
printf("\n The dates are not equal.");
getch();
return 0; // return int value
}
(h) Linked list is a very common data structure often used to store similar data in memory. While the elements of an array occupy contiguous memory locations, those of a linked list are not constrained to be stored in adjacent location. The individual elements are stored "somewhere" in memory, rather like a family dispersed, but still bound together. The order of the elements is maintained by explicit links between them. Thus, a linked list is a collection of elements called nodes, each of which stores two item of information - an element of the list, and a link, i.e., a pointer or an address that indicates explicitly the location of the node containing the successor of this list element. Write a program to build a linked list by adding new nodes at the beginning, at the end or in the middle of the linked list. Also write a function display() which display all the nodes present in the linked list.
Show Solutions
Hide Solutions
/* d (h) Linked list is a very common data structure often used to store similar
data in memory. While the elements of an array occupy contiguous memory
locations, those of a linked list are not constrained to be stored in adjacent
location. The individual elements are stored "somewhere" in memory, rather
like a family dispersed, but still bound together. The order of the elements
is maintained by explicit links between them. Thus, a linked list is a collection
of elements called nodes, each of which stores two item of information - an element
of the list, and a link, i.e., a pointer or an address that indicates explicitly
the location of the node containing the successor of this list element. Write a
program to build a linked list by adding new nodes at the beginning, at the end
or in the middle of the linked list. Also write a function display() which display
all the nodes present in the linked list.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
// source (copied from : http://tacklemantra.com/2012/08/28/linked-list/ )
// refer http://tacklemantra.com/2012/08/28/linked-list/ for understanding problem....
// for other problems visit: http://peaceinf.com/cat/prog/letusc/
void display (node *head) // display function
{
int count = 1;
node *p;
p = head;
while (p != NULL)
{
printf (“\nNode %d: %d %s %d”, count,
p->roll, p->name, p->age);
count++;
p = p->next;
}
printf ("\n");
}
node *create_list() // function for creating node
{
int k, n;
node *p, *head;
printf (“\n How many elements to enter?”);
scanf (“%d”, &n);
for (k=0; k {
if (k == 0) {
head = (node *) malloc(sizeof(node));
p = head;
}
else {
p->next = (node *) malloc(sizeof(node));
p = p->next;
}
scanf (“%d %s %d”, &p->roll, p->name, &p->age);
}
p->next = NULL;
return (head);
}
void insert (node **head) // function for inserting into link list
{
int k = 0, rno;
node *p, *q, *new;
new = (node *) malloc(sizeof(node));
printf (“\nData to be inserted: “);
scanf (“%d %s %d”, &new->roll, new->name, &new->age);
printf (“\nInsert before roll (-ve for end):”);
scanf (“%d”, &rno);
p = *head;
if (p->roll == rno) /* At the beginning */
{
new->next = p;
*head = new;
}
else
{
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
}
if (p == NULL) /* At the end */
{
q->next = new;
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
void delete (node **head) // delete function for link list
{
int rno;
node *p, *q;
printf (“\nDelete for roll :”);
scanf (“%d”, &rno);
p = *head;
if (p->roll == rno)
/* Delete the first element */
{
*head = p->next;
free (p);
}
else
{
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
}
if (p == NULL) /* Element not found */
printf (“\nNo match :: deletion failed”);
else if (p->roll == rno)
/* Delete any other element */
{
q->next = p->next;
free (p);
}
}
}
void insert (node **head) // function to insert at the middle
{
int k = 0, rno;
node *p, *q, *new;
new = (node *) malloc(sizeof(node));
printf (“\nData to be inserted: “);
scanf (“%d %s %d”, &new->roll, new->name, &new->age);
printf (“\nInsert before roll (-ve for end):”);
scanf (“%d”, &rno);
p = *head;
if (p->roll == rno) /* At the beginning */
{
new->next = p;
*head = new;
}
else
{
while ((p != NULL) && (p->roll != rno))
{
q = p;
p = p->next;
}
if (p == NULL) /* At the end */
{
q->next = new;
new->next = NULL;
}
else if (p->roll == rno)
/* In the middle */
{
q->next = new;
new->next = p;
}
}
}
int main() //main function
{
head = (node *) malloc(sizeof(node));
getch();
return 0; // return int value
}
(i) A stack is a data structure in which addition of new element or deletion of existing element always takes place at the same end. This end is often known as 'top' of stack. This situation can be compared to a stack of plates in a cafeteria where every new plate taken off the stack is also from the 'top' of the stack. There are several application where stack can be put to use. For example, recursion, keeping track of function calls, evaluation of expressions, etc. Write a program to implement a stack using a linked list.
Show Solutions
Hide Solutions
/* d (i) A stack is a data structure in which addition of new element or deletion
of existing element always takes place at the same end. This end is often known
as 'top' of stack. This situation can be compared to a stack of plates in a
cafeteria where every new plate taken off the stack is also from the 'top' of
the stack. There are several application where stack can be put to use.
For example, recursion, keeping track of function calls, evaluation of
expressions, etc. Write a program to implement a stack using a linked list.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
struct node // defining structure
{
int data;
struct node *link;
};
struct node *top=NULL,*temp;
int main() //main function
{
int choice,data;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnter ur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
temp=(struct node *)malloc(sizeof(struct node));
printf("Enter a node data :");
scanf("%d",&data);
temp->data=data;
temp->link=top;
top=temp;
break;
case 2:
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
break;
case 3:
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
while(temp!=NULL)
{
printf("->%d->",temp->data);
temp=temp->link;
}
break;
case 4:
exit(0);
}
}
getch();
return 0; // return int value
}
(j) Unlike a stack, in a queue the addition of new element takes place at the end (called 'rear' of queue) whereas deletion takes place at the other end (called 'front' of queue). Write a program to implement a queue using a linked list.
Show Solutions
Hide Solutions
/* d (j) Unlike a stack, in a queue the addition of new element takes place at the
end (called 'rear' of queue) whereas deletion takes place at the other end
(called 'front' of queue). Write a program to implement a queue using a linked list.
*/
#include <stdio.h> //header file
#include <conio.h> //header file
int main() //main function
{
// refer Go to Structure chapter and D [i] for this problem.
getch();
return 0; // return int value
}
Comments
Post a Comment