We can use structure to store the value of one particular object but if we want to store the value of 100 such objects we need array of structure.
For eg: If we want to store the value of details of book like author, no of pages and price. We can define such structure as follows:
struct bookinfo{
char[20] author;
int pages;
float price;
};
Now to define three similar structure we can define an array of the above defined structure as follows:
struct bookinfo record[3];
Lets's see this using a C program:
#include
struct bookinfo{
char author[20];
int pages;
float price;
};
int main(){
int i;
struct bookinfo record[3];
char suffix[3][3]={"st","nd","rd"};
for(i=0;i<3 i="" p=""> {
printf("Enter the author of first book: ");
scanf("%s",record[i].author);
printf("Enter the number of pages in the book: ");
scanf("%d",&record[i].pages);
printf("Enter the price of the book: ");
scanf("%f",&record[i].price);
}
for(i=0;i<3 i="" p=""> {
printf("\nThe author of %d%s book is: %s \n",i+1,suffix[i],record[i].author);
printf("The number of pages in %d%s book is: %d \n",i+1,suffix[i],record[i].pages);
printf("The price of %d%s book is: %f \n",i+1,suffix[i],record[i].price);
}
return 0;
}3>3>
struct bookinfo{
char author[20];
int pages;
float price;
};
int main(){
int i;
struct bookinfo record[3];
char suffix[3][3]={"st","nd","rd"};
for(i=0;i<3 i="" p=""> {
printf("Enter the author of first book: ");
scanf("%s",record[i].author);
printf("Enter the number of pages in the book: ");
scanf("%d",&record[i].pages);
printf("Enter the price of the book: ");
scanf("%f",&record[i].price);
}
for(i=0;i<3 i="" p=""> {
printf("\nThe author of %d%s book is: %s \n",i+1,suffix[i],record[i].author);
printf("The number of pages in %d%s book is: %d \n",i+1,suffix[i],record[i].pages);
printf("The price of %d%s book is: %f \n",i+1,suffix[i],record[i].price);
}
return 0;
}3>3>
OUTPUT:
Enter the author of first book: fasdf
Enter the number of pages in the book: 32
Enter the price of the book: 34
Enter the author of first book: phy
Enter the number of pages in the book: 45
Enter the price of the book: 56.5
Enter the author of first book: math
Enter the number of pages in the book: 54
Enter the price of the book: 45.7
The author of 1st book is: fasdf
The number of pages in 1st book is: 32
The price of 1st book is: 34.000000
The author of 2nd book is: phy
The number of pages in 2nd book is: 45
The price of 2nd book is: 56.500000
The author of 3rd book is: math
The number of pages in 3rd book is: 54
The price of 3rd book is: 45.700001
Enter the number of pages in the book: 32
Enter the price of the book: 34
Enter the author of first book: phy
Enter the number of pages in the book: 45
Enter the price of the book: 56.5
Enter the author of first book: math
Enter the number of pages in the book: 54
Enter the price of the book: 45.7
The author of 1st book is: fasdf
The number of pages in 1st book is: 32
The price of 1st book is: 34.000000
The author of 2nd book is: phy
The number of pages in 2nd book is: 45
The price of 2nd book is: 56.500000
The author of 3rd book is: math
The number of pages in 3rd book is: 54
The price of 3rd book is: 45.700001
No comments:
Post a Comment