Wednesday, September 17, 2014

write a structure for student data, also define functions to display and search that data


Problem

Student structure contains the following contents
Roll number (int), Name (string), Department (string), Course (string), Year of joining (string), cgpa (float)
Write the definition of the functions whose prototype is given below:
  1. void display(struct strudent s);
    This function display the content of struct student s
  2. void display(struct strudent *p);
    This function displays the content of a structure pointed by pointer *p
  3. void search(struct strudent *p, int roll_no);
    The first parameter is the base address of an array of type struct student and second parameter is the roll no of the structure. This function search's the record from array of structure (first parameter) whose Roll number is same as roll_no (second parameter)

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct student
{
 char name[30];
 int roll_no;
 char department[30];
 char course[30];
 int year;
}s[5];
int i;
void searchstudent (struct student *, int );
void disp();
void display (struct student *);
main()
{
 int rl,p;

 for (i=0;i<5;i++)
 {
  printf ("Enter name, ID, department, course, year : ");
  scanf("%s%d%s%s%d",&s[i].name,&s[i].roll_no,&s[i].department,&s[i].course,&s[i].year);
 }

 disp();
 display(s);

 printf ("Enter number you want to search : ");
 scanf("%d",&rl);

 searchstudent (s ,rl);

 system("pause");
}
//************
void disp()
{
 for (i=0;i<5;i++)
 {
  printf("And here is whhat you have entered.\n\n");
  printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",s[i].name,s[i].roll_no,s[i].department,s[i].course,s[i].year);
 }
}
//************************
void display (struct student *p)
{
 printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
}
void searchstudent (struct student *p, int roll_no)
{
 int i,found = 0;
 for(i=0;i<5;i++)
 {
  if(p->roll_no == roll_no)
  {
   printf("record found");
   printf("Name = %s\n\nRoll No. = %d\n\nDepartment = %s\n\nCopurse = %s\n\nYear = %d\n\n",p->name,p->roll_no,p->department,p->course,p->year);
   found = 1;
  }
 }
 if (found == 0)
  printf("Not found");
}

Sample Output


display & search data using structure

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact