Sie sind auf Seite 1von 4

Lab – 1 Structures & Unions

Objectives:

 To learn advantages of structures over normal arrays as a user-defined data type.

 To enhance programming skills using structures, array of structures and nested structures.

 To understand the concept of unions and distinguish between structures & unions.

 To identify the significance & limitations of bit field specifiers.

Sample Program:

main( )

int i;

struct employee

int id_no;

char sex;

float height, weight;

};

struct employee emp[3]={ { 121,’m’,5.7,59.8},{122,’f’,6.0,65.2},{123,’m’,6.2,75.5} };

printf(“The initialized contents are:”);

for(i=0;i<=2;i++)

printf(“In Record No: %d”,i);

printf(“\n ID=%d \n Sex=%c \n Height=%f \n weight=%f”,


emp[i].id_no,emp[i].sex,emp[i].height,emp[i].weight);

}
}
Modifications:

1. Make provisions for reading the data input from the user.

2. Write a function and use the same function for reading and printing the data by passing
structure.

3. Add a field as DOB, making the use of nested structure.

Exercises:

1. Define a structure called cricket that will describe the following information:

Player name, team name, batting average

Using cricket, declare an array players with 50 elements and write a program to read the
information about all the 50 players. Also print a team wise list containing names of players
with their batting averages.

2. Model a bookshop inventory program to maintain the inventory of books. The list includes
details such as author, title, price, publisher, stock position etc. Whenever the customer
wants a book, the shopkeeper inputs the title and the author name of the book and the
system replies whether it is in the list or not.

If it is in the list, then the system displays the book details and asks for the number of
copies. If the requested copies are available, the total cost of the books is displayed. Use
the concept of passing structures to functions. For publication date use the concept of
nested structures.

3. Define the structure for railway ticket and create an array of 10 tickets. Write a program

to perform following operations.

 To store the ticket information to access the details of ticket i.e. train name, departure
time, date, list of passengers and ticket number.

 Given a ticket number the status of the ticket is printed. i.e. waiting, confirm ,etc .if
confirm print passenger list with ticket number.
Lab – 2

Introduction to Pointers

Objective:

 To understand the methods of declaration, initialization of pointers and accessing


variables using pointers.

Sample Program:

main( )
{
int *p,*q;
int x,y,a;
x = 20; y =10;
p = &x;
q = &y;
a = *p + *q;
printf("value of a = %d\n",a);
}

Modification:

1. Modify the above program to calculate arithmetic operations using pointers and also read
input values from keyboard.

Exercise:

1. Write a program to convert decimal number entered by used in its equivalent binary number
using pointer.

Das könnte Ihnen auch gefallen