- 0
Notifications Mark All Read
- Login
- Get Prime
Program for Array of Structure
Array of Structure
We will write a C program to display array of structure for storing data and the data will be entered by the user. This will help to understand the basic structure of programming. In this program , we will display the array of structure easily by using proper syntax and algorithms.
Working of Program :
In the program, we will require some numbers from the user to display the array of structure of the given inputs.
Important Note for Palindrome:
- An array of structure can be defined as the collection of various structures variables where each variable stores information about different entities.
- An array of structures is used to stores information about various entities of various data type.
Problem 1
Write a program to display array of structures.
- Firstly, we have to enter the an input.
- Then print that array using structure.
Code
#include<stdio.h> struct student { char firstName[50]; int roll; float marks; } s[3]; int main () { int i; printf ("Enter information of students = \n"); for (i = 0; i < 3; ++i) { s[i].roll = i + 1; printf ("For roll number%d,\n", s[i].roll); printf ("Enter first name = "); scanf ("%s", s[i].firstName); printf ("Enter marks = "); scanf ("%f", &s[i].marks); } printf ("Displaying Information = \n\n"); for (i = 0; i < 3; ++i) { printf ("\nRoll number = %d\n", i + 1); printf ("First name = "); puts (s[i].firstName); printf ("Marks: %.1f", s[i].marks); printf ("\n"); } return 0; }
Output
Enter information of students = For roll number1, Enter first name = saru Enter marks = 99 For roll number2, Enter first name = salu Enter marks = 98 For roll number3, Enter first name = sanu Enter marks = 96 Displaying Information = Roll number = 1 First name = saru Marks: 99.0 Roll number = 2 First name = salu Marks: 98.0 Roll number = 3 First name = sanu Marks: 96.0
Note:
In the following program we will display the array of structure.
Problem 2
Write a program to display array using structure.
- Firstly, we have to enter the data.
- Then print that array using structure.
Code
#include<stdio.h> struct student { char name[20]; int rollno; float marks; }; int main () { int i, n; printf ("Enter how many records we want to store = "); scanf ("%d", &n); struct student stuarr[n]; printf ("Enter name, roll no. and marks Below : \n"); for (i = 0; i < n; i++) { printf ("\nEnter %d record : \n", i + 1); printf ("Enter Name = "); scanf ("%s", stuarr[i].name); printf ("Enter RollNo. = "); scanf ("%d", &stuarr[i].rollno); printf ("Enter Marks = "); scanf ("%f", &stuarr[i].marks); } printf ("\n\tName\tRollNo\tMarks\t\n"); for (i = 0; i < n; i++) printf ("\t%s\t%d\t%.2f\t\n", stuarr[i].name, stuarr[i].rollno, stuarr[i].marks); return 0; }
Output
Enter how many records we want to store = 3 Enter name, roll no. and marks Below : Enter 1 record : Enter Name = shanvi Enter RollNo. = 1 Enter Marks = 99 Enter 2 record : Enter Name = salu Enter RollNo. = 2 Enter Marks = 98 Enter 3 record : Enter Name = sanu Enter RollNo. = 3 Enter Marks = 97 Name RollNo Marks shanvi 1 99.00 salu 2 98.00 sanu 3 97.00
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment