Thursday, 11 July 2019

C Program for Array of Structures

Create a database using an array of structures and perform the following operations on it
1. Create 2. Add 3. Search 4. Update 5. Delete


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct student
{
      int rno,marks,del;
      char name[20];
};

void create(int,struct student []);
void add(int,struct student []);
void display(int,struct student []);
void search(int,int,struct student []);
void update(int,int,struct student []);
void delet(int,int,struct student []);

 int main()
{
      setbuf(stdout, NULL);
      int n,value,choice;

      struct student s[10];
      setbuf(stdout, NULL);
      printf("\n\t\t**STUDENT DATABASE**\n");
      printf("\n\tEnter the number of students >");
      scanf("%d",&n);
      create(n,s);

      do
      {
      printf("\n\n----------------------------MENU----------------------------");
      printf("\n\t1:ADD\n\t2:Display\n\t3:Search\n\t4:Update\n\t5:Delete\n\t\n\n\tEnter your choice\t>>");
      scanf("%d",&choice);
      printf("------------------------------------------------------------  \n");

      switch(choice)
      {
      case 1:
            n=n+1;
            add(n,s);
            break;
      case 2:
            printf("\n\tRollNo\tNAME\tMARKS \n");
            display(n,s);
            break;
      case 3:
            printf("\n\tEnter the RollNo. to Search   >");
            scanf("%d",&value);
            search(n,value,s);
            break;
      case 4:
            printf("\n\tEnter the RollNo. to Update   >");
            scanf("%d",&value);
            update(n,value,s);
            break;
      case 5:
            printf("\n\tEnter the RollNo. to Delete   >");
            scanf("%d",&value);
            delet(n,value,s);
            break;

      }
      }while(choice!=0);

      printf("\n");
      return 0;
}

void create(int n,struct student s[10])
{
      int i,j;
      for (i=0;i<n;i++)
      {
      printf("\n\tEnter the RollNo. >>");
      scanf("%d",&s[i].rno);
      printf("\tEnter NAME  >>");
      scanf("%s",s[i].name);
      printf("\tEnter Marks  >>");
      scanf("%d",&s[i].marks);
      s[i].del=0;
      }
}
void add(int n,struct student s[10])
{
      int i,j;
      for (i=n-1;i<n;i++)
      {
      printf("\n\tEnter the RollNo. >>");
      scanf("%d",&s[i].rno);
      printf("\tEnter NAME  >>");
      scanf("%s",s[i].name);
      printf("\tEnter Marks  >>");
      scanf("%d",&s[i].marks);
      s[i].del=0;
      }    
}
void display(int n,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if(s[i].del==0)
                  printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);
      }
}
void search(int n,int value,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\tRollNo\tMARKS\tNAME ");
            printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);
            break;
            }
      }
      if (i==n)
          printf("\n\tNO RECORD FOUND");
}
void update(int n,int value,struct student s[10])
{
      int i,j;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\tEnter the RollNo. & NAME & MARKS of student  \n\t");
            scanf("%d%s%d",&s[i].rno,s[i].name,&s[i].marks);
            break;
            }
      }
      if (i==n)
          printf("\n\tNO RECORD TO UPDATE ");  
}
void delet(int n,int value,struct student s[10])
{
      int i;
      for (i=0;i<n;i++)
      {
            if (value==s[i].rno)
            {
            printf("\n\t----------------------");
            printf("\n\tRollNo\tMARKS\tNAME ");
            printf("\n\t%d\t%s\t%d",s[i].rno,s[i].name,s[i].marks);

            printf("\n\t----------------------");
            printf("\n\tSure to delete: 1=YES/2=NO  >");
            scanf("%d",&s[i].del);
            break;
            }
      }
}

Output:
**STUDENT DATABASE** Enter the number of students >2 Enter the RollNo. >>1 Enter NAME  >>a Enter Marks  >>11 Enter the RollNo. >>2 Enter NAME  >>b Enter Marks  >>22
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>1------------------------------------------------------------   Enter the RollNo. >>3 Enter NAME  >>c Enter Marks  >>33
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22 3 c 33----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>3------------------------------------------------------------   Enter the RollNo. to Search >2 RollNo MARKS NAME  2 b 22----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>4------------------------------------------------------------   Enter the RollNo. to Update >3 Enter the RollNo. & NAME & MARKS of student   3cc33
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22 3 cc 33----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>5------------------------------------------------------------   Enter the RollNo. to Delete >3 ---------------------- RollNo MARKS NAME  3 cc 33 ---------------------- Sure to delete: 1=YES/2=NO  >1
----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>2------------------------------------------------------------   RollNo NAME MARKS  1 a 11 2 b 22----------------------------MENU---------------------------- 1:ADD 2:Display 3:Search 4:Update 5:Delete Enter your choice >>

No comments:

Post a Comment

Stack and Queue as ADT in C++

Stack as ADT The functions associated with stack are: empty()  – Returns whether the stack is empty size()  – Returns the size o...