Structure

Aim: 10.1 Write a program to read structure elements from the keyboard.

Code:

#include <stdio.h>

struct book

{

     int id;

     char name[20];

     float price;

};

int main(void) 

{

     struct book b1;

     printf("\nEnter Book Id : ");

     scanf("%d",&b1.id);

     fflush(stdin);

     printf("\nEnter Book Name : ");

     scanf("%s",b1.name);

     printf("\nEnter Book Price : ");

     scanf("%f",&b1.price);

     printf("\nBook Id    = %d",b1.id);

     printf("\nBook Name  = %s",b1.name);

     printf("\nBook Price = %.2f",b1.price);

     return 0;

}

Output screenshot:








Aim: 10.2 Define a structure type struct personal that would contain person name, date of joining and salary using this structure to read this information of 5 people and print the same on screen.

Code:

#include <stdio.h>

struct personal

{

    char name[20];

    char doj[10];

    float salary;

}p[5];

int main(void) 

{

    int i=1;

    for(i=1;i<=5;i++)

    {

        printf("\nEnter Person Name : ");

        scanf("%s",p[i].name);

        printf("Enter Person Date of Joining (dd-mm-yyyy) : ");

        scanf("%s",p[i].doj);

        printf("Enter Person Salary : ");

        scanf("%f",&p[i].salary);

    }

    for(i=1;i<=5;i++)

    {

        printf("\nPerson %d Detail",i);

        printf("\nName   = %s",p[i].name);

        printf("\nDOJ    = %s",p[i].doj);

        printf("\nSalary = %.2f",p[i].salary);

    }

    return 0;

}

Output screenshot:











Aim: 10.3 Define structure data type called time_struct containing three member’s integer hour, integer minute and integer second. Develop a program that would assign values to the individual number and display the time in the following format: 16: 40:51.

Code:

#include <stdio.h>

struct time_struct

{

    int hour;

    int minute;

    int second;

}t;

int main(void)

{

    printf("\n Enter Hour : ");

    scanf("%d",&t.hour);

    printf("\n Enter Minute: ");

    scanf("%d",&t.minute);

    printf("\n Enter Second : ");

    scanf("%d",&t.second);

    printf("\n Time %d:%d:%d",t.hour%24,t.minute%60,t.second%60);

    return 0;

}

Output screenshot:

Aim: 10.4 Define a structure called cricket that will describe the following information: 

Player name

Team name

Batting average

Using cricket, declare an array player with 50 elements and write a C program to read the information about all the 50 players and print team wise list containing names of players with their batting average.

Code:

#include<stdio.h>

#include<conio.h>

#include<string.h>

struct cricket

{

    char pname[20];

    char tname[20];

    float bavg;

};

int main()

{

    struct cricket s[5],t;

    int i,j,n=5;

    float p;

    printf("\nEnter data of %d players\n",n);

    for(i=0;i<n;i++)

    {

        printf("\nEnter PName TName BAvg for player-%d = ",i+1);

        scanf("%s %s %f",s[i].pname,s[i].tname,&p);

        s[i].bavg=p;

    }

    for(i=1;i<=n-1;i++)

    {

        for(j=1;j<=n-i;j++)

        {

            if(strcmp(s[j-1].tname,s[j].tname)>0)

            {

                t=s[j-1];

                s[j-1]=s[j];

                s[j]=t;

            }

        }

    }

    printf("\nAfter teamwise sorting... Player list is ");

    for(i=0;i<n;i++)

    {

        printf("\n%-20s %-20s %.2f",s[i].pname,s[i].tname,s[i].bavg);

    }

    getch();

    return 0;

}

Output screenshot:












Aim: 10.5 Design a structure student_record to contain name, branch and total marks obtained. Develop a program to read data for 10 students in a class and print them.

Code:

#include <stdio.h>

struct student_record

{

    char name[20];

    char branch[20];

    int total_marks;

}p[10];

int main(void) 

{

    int i=1,n=10;

    for(i=1;i<=n;i++)

    {

        printf("\nEnter Student Name : ");

        scanf("%s",p[i].name);

        printf("Enter Students Branch : ");

        scanf("%s",p[i].branch);

        printf("Enter Students Marks : ");

        scanf("%d",&p[i].total_marks);

    }

    for(i=1;i<=n;i++)

    {

        printf("\nStudent %d Detail",i);

        printf("\nName        = %s",p[i].name);

        printf("\nBranch      = %s",p[i].branch);

        printf("\nTotal marks = %d",p[i].total_marks);

    }

    return 0;

}

Output screenshot:


No comments:

Post a Comment

If you have any doubts or suggestions, Please let me know