Saturday, August 18, 2018

STRUCTURE-1 STUDENT DETAILS

  • Problem Description

    Create a structure called Student.

    struct Student
    {
    char name[30];
    char department[20];
    int yearOfStudy;
    float cgpa;
    };

    The structure variable should be "S1"

    Write a program to get the details of n students and to display their details, sorted in ascending order based on name.

    Input and Output Format:

    Refer sample input and output for formatting specification.

    Name, Department, Year of study, CGPA.

    Students details are sorted based on their "Names" in ascending order

    Mandatory :

    Note: The structure variables, data members and structure name are CASE Sensitive.

    Follow the same case mentioned in the mandatory

  • CODING ARENA
  • #include <stdio.h>
    #include<string.h>
    struct Student 
      {
      char name[30];
      char department[30];
      int yearOfStudy;
      float cgpa; 
      }S1[100];
    int main()
    {
      struct Student t;
      int i=0,j=0,n;
      scanf("%d",&n); 
      for(i=0;i<n;i++)
        {
        scanf("%s",S1[i].name);
        scanf("%s",S1[i].department);
        scanf("%d",&S1[i].yearOfStudy);
        scanf("%f",&S1[i].cgpa);
      }
      for(i=0;i<n;i++)
      {
        for(j=i+1;j<n;j++)
        {
          if(strcmp(S1[i].name,S1[j].name)>0)
          {
            t=S1[i];
            S1[i]=S1[j];
            S1[j]=t;
          }
        }
      }
      for(i=0;i<n;i++)
        
      {
        printf("\nName:%s",S1[i].name);
        printf("\nDepartment:%s",S1[i].department);
        printf("\nYear of study:%d",S1[i].yearOfStudy);
        printf("\nCGPA:%.1f",S1[i].cgpa);
      }
      return 0;
    }

  • Test Case 1

    Input (stdin)
    3
    
    raju cse 1 7.8
    
    somu IT 2 8.2
    
    Jagan swe 3 8.6
    
    
    Expected Output
    Name:Jagan
    
    Department:swe
    
    Year of study:3
    
    CGPA:8.6
    
    Name:raju
    
    Department:cse
    
    Year of study:1
    
    CGPA:7.8
    
    Name:somu
    
    Department:IT
    
    Year of study:2
    
    CGPA:8.2
  • Test Case 2

    Input (stdin)
    2
    
    shujathkhan IT 3 9.1
    
    john cse 3 9.3
    
    
    Expected Output
    Name:john
    
    Department:cse
    
    Year of study:3
    
    CGPA:9.3
    
    Name:shujathkhan
    
    Department:IT
    
    Year of study:3
    
    CGPA:9.1

No comments:

Post a Comment