Let Us C / Chapter 10 (Structures)

                                 Exercise [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.

Solution:


/* NOTE: since number of students to be assumed is too much ( about 450 )
I have alloted full size (about 450) to it but array has been kept empty,
if you have time then you can fill up all 450 names and can search through
them.program has been tested by me and it works accurately. you can reduce
the size of array of structure conveniently by changing the value of N */

#include<stdio.h>
#include<conio.h>
#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() */

struct students s[N];
int i,ch;

clrscr();


/* taking input of 450 students in an array of structure */

for(i=0;i<N;i++) {

printf("  Enter data of student %d\t\t\t\ttotal students: %d\n",i+1,N);
printf("****************************\n\n");

printf("enter rollnumber:  ");
scanf("%d",&s[i].rlnm);

printf("\n\nenter name:  ");
scanf(" %s",&s[i].name);

printf("\n\nenter department:  ");
scanf("%s",&s[i].dept);

printf("\n\nenter course:  ");
scanf("%s",&s[i].course);

printf("\n\nenter year of joining:  ");
scanf("%d",&s[i].year);

clrscr();

}

  /* displaying a menu */

printf("\n\tenter your choice: \n");
printf("\t**********************\n\n");

printf("1: enter year to search all students who took admission in that:\n\n");
printf("2: enter roll number to see details of that student\n\n\n");

printf("your choice:  ");     /* taking input of your choice */
scanf("%d",&ch);

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;
}


disprl(struct students *a) {        /* function to print information of a\
    student whose roll number has been \
    given. */

int k,rl;

printf("\nenter roll number: ");
scanf("%d",&rl);

for(k=0;k<N;k++)  {

if(a[k].rlnm==rl) {

printf("\n\n\t Details of roll number: %d\n",a[k].rlnm);
printf("\t***************************\n\n");
printf("           Name: %s\n",a[k].name);
printf("     Department: %s\n",a[k].dept);
printf("         Course: %s\n",a[k].course);
printf("Year of joining: %d",a[k].year);

break;
}

else {
printf("\nRoll number you entered does not exist\n\n");

break;
}

 }

return 0;
}


--------------------------------------------------------------------------------------------------------------

(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”.

Solution:



/* NOTE: since number of customers to be assumed is too much ( about 200 )

I have alloted full size (about 200) to it but array has been kept empty,

if you have time then you can fill up all 200 names and can search through

them.program has been tested by me and it works accurately. you can reduce
the size of array of structure conveniently by changing the value of N */

#include<stdio.h>
#include<conio.h>
#define N 200

struct bank {
   int acn;
   char name[20];
   int bal;         /* defined out of main() */
   };

void main() {

struct bank b[N];

int i,ch,lw=100,ch2,ac,am;

clrscr();

for(i=0;i<N;i++) {           /* inputting customer data */

printf("\tEnter information of customers \n");
printf("\t******************************\n\n");

printf("enter account no.: ");
scanf("%d",&b[i].acn);

printf("\n\nenter customer name: ");
scanf("%s",&b[i].name);

printf("\n\nenter balance: ");
scanf("%d",&b[i].bal);

clrscr();

}

clrscr();

printf("\tEnter your choice\n");    /* further processing of transaction */
printf("\t*****************\n\n");

printf("1: to know whose balance is below 100.\n\n");
printf("2: to process request or withdrawl.\n\n\n");


scanf("%d",&ch);

switch(ch) {

case 1:

      clrscr();

      disp(&b);         /* displaying whose balance is below 100 */

      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) {

  printf("\n\nyou don't have enough balance for withdrawl.\n");


  }


  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;

 }


-----------------------------------------------------------------------------------------------------------------

(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.

Solution:


#include<stdio.h>

#include<conio.h>

struct automob {

char name[5];
int year;
char mtr[10];
int qty;
};
void main() {

struct automob a[54]={{"AA1",2009,"AAA",10},
{"AA2",2009,"AAA",10},
{"AA3",2009,"AAA",10},
{"AA4",2009,"AAA",10},
{"AA5",2009,"AAA",10},
{"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();


printf("Information of parts between BB1 to CC6:\n\n");

printf("\n\nName\t\tyear(manu.)\t\tmaterial\tquantity\n\n\n");

for(i=0;i<54;i++) {


if(i>=9 && i<=23) {
printf("%3s\t\t%4d\t\t\t%5s\t\t%4d\n",a[i].name,a[i].year,a[i].mtr,a[i].qty);


}
}


getch();
}

-----------------------------------------------------------------------------------------------------------------

(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.

Solution:



#include<stdio.h>

#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++) {

if(c[i].avrn > c[j].avrn) {

temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}

printf("Sorted records:\n\n");

for(i=0;i<20;i++) {

printf("%d\t%s\t%d\t%d\t%f\n\n\n",i+1,c[i].name,c[i].age,c[i].match,c[i].avrn);

}

getch();
}

linkfloat() {

float a=0,*b;

b=&a;
a=*b;

return 0;
}

-----------------------------------------------------------------------------------------------------------------

(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.

Solution:


#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();
}

-----------------------------------------------------------------------------------------------------------------

(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.


Solution:



#include<stdio.h>

#include<conio.h>

#include<dos.h>


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) {       /* initialized an indefinite loop */

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:       /* adding book information over current information */

clrscr();

printf("\t\t1: Add book information:\n\n\n");

printf("enter book information:\n\n");

printf("Accesion number of book to be modified:  ");
scanf("%d",&j); fflush(stdin);

for(i=0;i<5;i++) {

if(j==a[i].acn) {

printf("\n\nenter new information:\n\n");
printf("accesion no.: ");
scanf("%d",&temp.acn); fflush(stdin);
printf("\n\ntitle: ");
scanf("%s",&temp.title);  fflush(stdin);
printf("\n\nauthor: ");
scanf("%s",&temp.auth);  fflush(stdin);
printf("\n\nprice: ");
scanf("%f",&temp.auth); fflush(stdin);
printf("\n\nflag(1/0): ");
scanf("%d",&temp.flag); fflush(stdin);

}
}

a[j]=temp;   /* overwriting current information with new one  */

clrscr();

printf("\n\nInformation added succesfully!\n\n");

delay(2000);

break;


case 2:    /* displaying current book information */


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;


case 3: /* listing all books of given author */


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:    /* displaying title of given book */


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:    /* counting total books in library */


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;


case 6:   /* sorting the books by their accesion numbers */


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:      /* condition for going out */

      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;
}

------------------------------------------------------------------------------------------------------------------

(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".


Solution:


#include<stdio.h>
#include<conio.h>

void main() {

struct date {
   int date;
   int month;
   int year;
   }d1;

int d,m,y,i;
clrscr();

printf("enter the date to save in structure: \n\n");

printf("Enter the date: ");
scanf("%d",&d1.date);

printf("\n\nEnter the month: ");
scanf("%d",&d1.month);

printf("\n\nEnter the year: ");
scanf("%d",&d1.year);

clrscr();

printf("\nenter the date to compare with structure:\n\n");

printf("Enter the date: ");
scanf("%d",&d);

printf("\n\nEnter the month: ");
scanf("%d",&m);

printf("\n\nEnter the year: ");
scanf("%d",&y);

if(d==d1.date && m==d1.month && y==d1.year) {

printf("\n\ndates are equal\n\n");

}

else {

printf("\n\ndates are unequal\n\n");

}

getch();

}


-----------------------------------------------------------------------------------------------------------------

(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.

Solution:


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<ctype.h>

struct linklist {
int data;
linklist *link;
};

void main() {

struct linklist *first, *temp;

char choice='y';

int count=0;

clrscr();


/* taking the input of first element */


printf("Enter element %d: ",++count);

first=(linklist *)malloc(sizeof(linklist));

scanf("%d",&first->data);

first->link=NULL;

printf("\n\nEnter another element(Y/N): ");

choice=getche();

tolower(choice); /* converting to lowercase to match the condition of switch */

switch(choice) {

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;


printf("\n\n\nEnter another elements(Y/N): ");
choice=getche();

clrscr();

}

break;


case 'n':

break;

}



printf("\nTotal elements = %d\n\n",count);


/* printing all elements */


for(temp=first; temp!=NULL; temp=temp->link) {

printf("%d ",temp->data);

}


getch();

}


-----------------------------------------------------------------------------------------------------------------

(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.

Solution:

NOTE: Topic not discussed in the book. I am learning from other resources.

-----------------------------------------------------------------------------------------------------------------

(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


Solution:

NOTE: Topic not discussed in the book. I am learning from other resources.
_______________________________________________________________________

5 comments:

Let Us C / Chapter 7 (The C Pre-processor)

                               Exercise [C] (a) Write down macro definitions for the following: 1. To test whether a character ente...