How to use array of structure in C Linux Code.

How to enter details of 10 (multiple) employee ? Answer is array of employee structures.
#include<stdio.h>
#include<memory.h>
struct employee
{
int id;
char name[20];
//int age;
};

int main()
{
int i;
struct employee arr[3];
for(i=0;i<3;i++)
{
printf("Enter details of employess:");
scanf("%d%s",&arr[i],arr[i].name);
}
printf("Employee details:\n");
for(i=0;i<3;i++)
{
printf("id=%d, name=%s\n",arr[i].id, arr[i].name);
}
return 0;
}
/*******OUTPUT
Enter details of employess:1
Leal
Enter details of employess:2
Mitra
Enter details of employess:333
Jesus
Employee details:
id=1, name=Leal
id=2, name=Mitra
id=333, name=Jesus
*****************/
//Display structure details based on search conditions
//students's name and roll number should be stored in structure
//array of 10 student structure
//take input and display
//displaye the student detail whose roll number is entered
#include<stdio.h>
#define SIZE 2
struct student
{
char name[20];
int roll;
};
struct student arr[SIZE];//needs global
int i;
int rollSearch;

void add();
void disp();
void search();
int main()
{
//Array of 5 students
//struct student arr[5];//needs global
//enter student details
add();
//display students details
disp();
//display details of student whose roll number is entered
search();
return 0;
}
void add()
{
for(i=0;i<SIZE;i++)
{
printf("Enter name of student %d:",i+1);
scanf("%s",arr[i].name);
printf("Enter roll of student %d:",i+1);
scanf("%d",&arr[i].roll);
}
}
void disp()
{
printf("*************Student Details***************\n");
for(i=0;i<SIZE;i++)
{
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
void search()
{
int flag=0;
printf("*************Search Result***************\n");
printf("Enter Roll of whose details you want:");
scanf("%d",&rollSearch);
for(i=0;i<SIZE;i++)
{
if(arr[i].roll==rollSearch)//mind the syntax roll[i].roll
{
flag=1;
printf("Name of student %d is:%s\n",i+1,arr[i].name);
printf("Roll of student %d is:%d\n",i+1,arr[i].roll);
}
}
if(flag==0)
printf("Roll Number does not exist.\n");
}
/*Output:
Enter name of student 1:Leal
Enter roll of student 1:100
Enter name of student 2:Mitra
Enter roll of student 2:200
Enter name of student 3:Ram
Enter roll of student 3:300
Enter name of student 4:Raheem
Enter roll of student 4:400
Enter name of student 5:yeshu
Enter roll of student 5:500
*************Student Details***************
Name of student 1 is:Leal
Roll of student 1 is:100
Name of student 2 is:Mitra
Roll of student 2 is:200
Name of student 3 is:Ram
Roll of student 3 is:300
Name of student 4 is:Raheem
Roll of student 4 is:400
Name of student 5 is:yeshu
Roll of student 5 is:500
*************Search Result***************
Enter Roll of whose details you want:200
Name of student 2 is:Mitra
Roll of student 2 is:200
*/

No comments:

Post a Comment