Sei sulla pagina 1di 9

EXPERIMENT NO-08 PROGRAM on ARRAY of STRUCTURES

Aim: Program demonstrating array of structures (operations like storing

and retrieving of data)


Description:
A structure is a collection of one or more variables, possibly of different data types, grouped together under a single name for convenient handling.

Structure declaration Structures are declared with the struct keyword. The specifier keyword is followed by an optional identifier name, which is used to identify the form of the structure . The identifier is followed by the declaration of the structure body: a list of member declarations, contained within curly braces, with each declaration terminated by a semicolon. Finally, the declaration concludes with an optional list of identifier names, which are declared as instances of the structure. For example, the following statement declares a structure named s that contains three members; it will also declare an instance of the structure known as t:
struct s { int x; float y; char *z; } t;

Once a structure body has been declared and given a name, it can be considered a new data type using the specifier struct, as appropriate, and the name. For example, the following statement, given the above structure declaration, declares a new instance of the structure s named r:
struct s r;

Accessing members Members are accessed using the name of the instance of a structure , a period (.), and the name of the member. For example, given the declaration of t from above, the member known as y (of type float) can be accessed using the following syntax: t.y Initialization A structure can be initialized in its declarations using an initializer list, similar to arrays. If a structure is not initialized, the values of its members are

undefined until assigned. The components of the initializer list must agree, in type and number, with the components of the structure itself. The following statement will initialize a new instance of the structure s from above known as pi:
struct s pi = { 3, 3.1415, "Pi" };

Designated initializers allow members to be initialized by name. The following initialization is equivalent to the previous one.
struct s pi = { .z = "Pi", .x = 3, .y = 3.1415 };

Members may be initialized in any order, and those that are not explicitly mentioned are set to zero. Assignment Assigning values to individual members of structure is syntactically identical to assigning values to any other object. The only difference is that the lvalue of the assignment is the name of the member, as accessed by the syntax mentioned above. For example, the following statement assigns the value of 74 (the ASCII code point for the letter 't') to the member named x in the structure t, from above:
t.x = 74;

Arrays of structures : An array of structures is declared in the usual way: struct student { int roll_no; char name[50]; float age; float per_marks; } stu[10];

Algorithm:
Start program Declare the array of structure . Take the input from the user. Perform Operation. Print output at screen. End program.

Flowchart:

Start

Take Input

Process

Print output

End

Program: Ex 1) /* program for simple structure ,Take i/p data and display data*/

#include<stdio.h> #include<conio.h> void main() { struct book { char name[20]; int pages; float price; }; struct book b1,b2,b3; clrscr(); printf(\n Enter the names,prices &pages of three books); scanf(%s%f%d,b1.name,&b1.price,&b1.pages); scanf(%s%f%d,b2.name,&b2.price,&b2.pages); scanf(%s%f%d,b3.name,&b3.price,&b3.pages); printf(\n display structure data); printf(\n%s\t%f\t%d,b1.name,b1.price,b1.pages); printf(\n%s\t%f\t%d,b2.name,b2.price,b2.pages); printf(\n%s\t%f\t%d,b3.name,b3.price,b3.pages); getch(); }

Output:
Enter the names,prices &pages of three books Swati 55.0 300 Meena 45.66 100 Pawar 45.00 100 display structure data swati 55.000000 300 meena 45.660000 100 pawar 45.000000 100

Ex 2) /* program for book data*/


#include<stdio.h> #include<conio.h> void main() { struct book { char name; int pages; float price; }; struct book b1,b2,b3; clrscr(); printf(\n Enter the names,prices &pages of three books); scanf( %c%f%d,&b1.name,&b1.price,&b1.pages); scanf( %c%f%d,&b2.name,&b2.price,&b2.pages); scanf( %c%f%d,&b3.name,&b3.price,&b3.pages); printf(\n display structure data); printf(\n %c\t%f\t%d,b1.name,b1.price,b1.pages); printf(\n %c\t%f\t%d,b2.name,b2.price,b2.pages); printf(\n %c\t%f\t%d,b3.name,b3.price,b3.pages); getch(); }

Output: Enter the names,prices &pages of three books


S 55.0 300 M 45.66 100 P 45.00 100 display structure data s 55.000000 300 m 45.660000 100 p 45.000000 100

Ex 3) /* program for addition of two time*/ struct Time { int hour; int min; int hr1,hr2; int min1,min2; }time; void main() { printf(\n plz enter the 1st time in HH:MM format); scanf(%d,&time.hr1); printf(:); scanf(%d,time.min1); printf(\n plz enter the 2nd time in HH:MM format); scanf(%d,&time.hr2); printf(:); scanf(%d,time.min2); time.hour=time.hr1+time.hr2; time.min=time.min1+time.min2; if(time.min>60) { time.hour=time.hour+1;

time.min=time.min-60; } printf(\n HH:MM=%d:%d,time.hour,time.min); getch(); } Output: plz enter the 1st time in HH:MM format 10:10 plz enter the 2nd time in HH:MM format 12.20 HH:MM=22:30 Ex 4)/* program for array of structure*/ #include<stdio.h> #include<conio.h> void main() { struct book { char name; int pages; float price; }; struct book b[3]; clrscr(); printf(\n plz enter the name,price,and pages of book); for(i=0;i<3;i++) { scanf( %c%f%d,&b[i].name,&b[i].price,&b[i].pages); } printf(\n display book information); for(i=0;i<3;i++) { printf( %c%f%d,&b[i].name,&b[i].price,&b[i].pages); } getch() }

linkfloat() { float a=0,*b; b=&a; a+*b; }

Output: plz enter the name,price,and pages of book S 55.0 300 M 45.66 100 P 45.00 100 display book information s 55.000000 300 m 45.660000 100 p 45.000000 100

Conclusion:
Thus we have studied manipulation of two dimensional arrays on the matrices multiplication, subtraction, symmetric and addition of two given matrices.

Potrebbero piacerti anche