Pages

Wednesday, September 14, 2011

Struct in C


A struct in C programming language is a structured (record) type that aggregates a fixed set of labelled objects, possibly of different types, into a single object.
A struct declaration consists of a list of fields, each of which can have any type. The total storage required for a struct object is the sum of the storage requirements of all the fields, plus any internal padding.

/* Objective: To prepere a menu which offer following facilities:
        -Register a student
        -delete record
        -search record and print its average
        -display list of student as per percentage obtained     */



        // Source Code  //


#include<stdio.h>            //including header files//
#include<string.h>
#include<conio.h>
struct info      
      //declaration of structure//
{
char name[20];
char branch[20];
int year;
int marks[5];
float percent;
char eno[20];
}student[50];

//Function Declaration//
void enter(int);
void del(int);
void search(int);
void display(int);
int main()
{
int i,n;
char choice;

printf("enter the no of students u want to register\t");
scanf("%d",&n);         // taking no of records to enter//
enter(n);               // calling enter function//
do
{
printf("enter\n 1:Delete \n 2:search \n 3:Display");
scanf("%d",&i);         //taking user's choice//
switch(i)
{
case 1:
del(n);                  // calling del function//
break;
case 2:
search(n);              // calling search function//
break;
case 3:
display(n);            // calling display function//
break;

default:
printf("invalid choice");
break;
}
printf("do u wanna continue press y or n");
scanf("%c",&choice);
}
while(choice=='y' || choice =='Y');
getch();
}
void enter(int n)      // function definition//
{
int i,j;

for(i=0;i<n;i++)
{
printf("Enter name,branch,year,enrolement no. of student");
scanf("%s %s %d %s",student[i].name,student[i]
.branch,&student[i].year,student[i].eno);
         //taking student info//
printf("enter marks of 5 subject");
for(j=0;j<5;j++)
scanf("%d",&student[i].marks[j]);  // taking student's marks//
}
}
void del(int n)    // function definition//
{
char sname[20];
int i,j,flag;
printf("enter the name of student whose records u wanna delete:");
scanf("%s",sname);
for(i=0;i<n;i++)
if(strcmp(student[i].name,sname)==0)      //checking//
flag=i;
for(j=flag;j<n-1;j++)
{
strcpy(student[j].name,student[j+1].name);  //swapping//
student[j].year=student[j+1].year;
strcpy(student[j].branch,student[j+1].branch);
for(i=0;i<5;i++)
student[j].marks[i]=student[j+1].marks[i];
strcpy(student[j].eno,student[j+1].eno);
}
printf("The records after deleting info are:");
display(n-1);               //function call for display function//
}
void search(int n)          //function definition//
{
int i, avg,j, flag;
char eno[20];
printf("enter eno of student whose marks are to be calculated");
scanf("%s",eno);
for(i=0;i<n;i++)

                if(strcmp(student[i].eno,eno)==0)
                flag=i;

                for(j=0;j<n;j++)
                                {
                                avg+=student[flag].marks[j];
                                }
printf("\nThe avg is %d",avg);
}

void display(int n)           // function definition//
{
int i,j,sum[50],pos,a[50],k;
float small;
struct info s1;
for(i=0;i<n;i++)
{
                 sum[i]=0;
                for(j=0;j<5;j++)
   
                sum[i]+=student[i].marks[j];
                student[i].percent=(float)sum[i]/5;
   

}
printf("The information of students are:");


for(i=0;i<n;i++)
{
    // sorting info in increasing order of percentage//
    for(j=0;j<n-i-1;j++)
    {
        if(student[j].percent>student[j+1].percent)
        {
        strcpy(s1.name,student[i].name);
        strcpy(s1.branch,student[i].branch);
        strcpy(s1.eno,student[i].eno);
        s1.year=student[i].year;
        for(k=0;k<5;k++)
        s1.marks[k]=student[i].marks[k];
        s1.percent=student[i].percent;
        strcpy(student[i].name,student[i+1].name);
        strcpy(student[i].branch,student[i+1].branch);
        strcpy(student[i].eno,student[i+1].eno);
        student[i].year=student[i+1].year;
        for(k=0;k<5;k++)
        student[i].marks[k]=student[i+1].marks[k];
        student[i].percent=student[i+1].percent;
        strcpy(student[i+1].name,s1.name) ;
        strcpy(student[i+1].branch,s1.branch);
        strcpy(student[i+1].eno,s1.eno)  ;
        student[i+1].year=s1.year;
        for(k=0;k<5;k++)
        student[i+1].marks[k]=s1.marks[k];
        student[i+1].percent=s1.percent;
        }
    }

    }
for(i=0;i<n;i++)
{
    printf("\nName: %s \n branch: %s \n enrolement no: %s",student[i].name,student[i].branch,student[i].eno)  ;
    printf("\nThe marks of 5 subjects are:");
    for(j=0;j<5;j++)
    printf("\n%d",student[i].marks[j]);
    printf("\npercentage %f",student[i].percent);
}
}

4 comments:

  1. u r a genious .. tz code doesn't shows dat u hav little interest in pgming,,instead it shows ur an expert programmer......

    ReplyDelete
  2. thankz a lot for providing such useful codings....

    ReplyDelete
  3. Hey there just wanted to give you a quick heads up and let
    you know a few of the pictures aren't loading properly. I'm not sure why but I think its
    a linking issue. I've tried it in two different internet browsers and both show the same results.
    Feel free to visit my web site :: ipad2 cyber Monday 2012

    ReplyDelete