Tutorial Outlines:
Illustrative Example of a C program of how to declare a structure, initialization and
accessing its element:
Solution:
#include <stdio.h>
void main()
{
typedef struct //Definition of a structure
{
int no;
char acc_name[15];
float balance;
} account;
account a1,a2; // Structure Declaration
account a3={467,"sameer",2456}; // Structure can be initialized like that
printf("\n Enter account number, names and balance :\n");
scanf("%d%s%f",&a1.no,a1.acc_name,&a1.balance);
printf("\n Enter account number, names and balance :\n");
scanf("%d%s%f",&a2.no,a2.acc_name,&a2.balance);
printf("A/C No.=%d\tName = %s\tBalance = %f\n",a1.no,a1.acc_name,a1.balance);
printf("A/C No.=%d\tName = %s\tBalance = %f\n",a2.no,a2.acc_name,a2.balance);
printf("A/C No.=%d\tName = %s\tBalance = %f\n",a3.no,a3.acc_name,a3.balance);
}
Assign. 9A:
Write a C program for addition of two complex numbers using structure.
Test Run:
Input Output
2+3i, 5+2i 7+5i
4+7i, 7+8i 11+15i
Tutorial 9B (Array of structure as pointer):
Illustrative example of how to pass array of structures as pointers to a function.
Write a C function to display an array of structure.
#include <stdio.h>
#include <malloc.h>
typedef struct //Definition of a structure
{
int no;
float balance;
} account;
void disp(account *acc_ptr, int n);
void main()
{
int i,n;
account *acc_ptr;//Pointer to the structure used as array
printf("Enter the number of records :");
scanf("%d",&n);
acc_ptr=malloc(n*sizeof(account));// Dynamic memory allocation
for (i=0;i<n;i++)
{
printf("\nEnter the %d number Record (A/C No. and Balance):\n",i);
scanf("%d%f",&acc_ptr[i].no,&acc_ptr[i].balance);//Storing the array of //structure
}
disp(acc_ptr,n);//Passing the pointer to structure to a function
}
void disp(account *acc_ptr, int n)
{
int i;
printf("\nThe records are: \n\n");
for (i=0;i<n;i++)
printf("%d\t%f\n\n",(acc_ptr+i)->no,(acc_ptr+i)->balance); //Accessing//data from array of structure
}
Write a C function to sort the array of structure with respect to
a) marks
b) d.o.b.
c) name (in lexical order)
d) roll no.
Hint: You can use the following structure
struct student
{
int Roll_no;
struct dob
{
int day;
int month;
int year;
};
char name[20];
int marks;
};
Tutorial 9C (Self Referential Structure: Lined Lists):
Write a C program for implementing a linked list with functions as append, display and count.
append : It appends a value as the last element of the list.
display : It shows the elements of the linked list.
count: It counts the number of elements presents in the linked list.
Solution:
#include <stdio.h>
#include <malloc.h>
struct node //Definition of a self referential structure
{
int d;
struct node *link; //Note that link is pointer to the same structure (self reference)
};
void append(struct node **q, int n);
void display(struct node *q);
int count(struct node *q);
void main()
{
struct node *ptr; //ptr is a pointer to the structure
int n,i,data;
ptr=NULL; //empty linked list
printf("\nEnter the number of elements you want to append to the list :\n");
scanf("%d",&n);
for (i=0;i<n;i++) //Reading
{
printf("\nEnter the elements to be appended :\n");
scanf("%d",&data);
append(&ptr,data);
}
display(ptr);
printf("\n\nNo. of elements in the Linked List = %d\n\n",count(ptr));
}
void append(struct node **q, int n)// Note that q is pointer to the pointer to the structure
{
struct node *t;
t=*q;
if (*q==NULL) //Create the first node when the list is empty
{
*q=malloc(sizeof(struct node));
t=*q;
}
else
{
while (t->link!=NULL) //go to last node
t=t->link;
t->link=malloc(sizeof(struct node)); //dynamic memory allocation of a node
t=t->link;
}
t->d=n;
t->link=NULL;
}
void display(struct node *q)
{
printf("\n\nThe elements of the Linked List :\n\n");
while (q!=NULL) //traverse the entire linked list
{
printf("%d\t",q->d);
q=q->link;
}
}
int count(struct node *q)
{
int count=0;
while (q!=NULL) //traverse the entire linked list
{
count++;
q=q->link;
}
return count;
}
Assign 4C: Extend the program in Tutorial 9C to implement the following functions:
a) addbeg: Add a new node at the beginning of the list
b) addafter: Insert a new node after the ith node from the root
c) delete: Delete a node which holds a specific value
Hints: First search
the specified
node and delete it.
addbeg (node with data=78)
addafter (2nd node)
delete (node where data=32)