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

struct avi
{
char c[50];
int a;
} s1[20];
int main()
{
int i,k,tem,j;
char temp[50];
scanf("%d",&k);

for(i=0;i<k;i++) {
scanf("%s%d",s1[i].c,&s1[i].a);
}
for(i=0;i<k;i++) {
for(j=i+1;j<k;j++) {
if(s1[i].a<s1[j].a)
{

tem=s1[i].a; strcpy(temp, s1[i].c);

s1[i].a=s1[j].a; strcpy(s1[i].c, s1[j].c);
s1[j].a=tem; strcpy(s1[j].c, temp);

}
}
} if(s1[4].a==s1[5].a){ strcpy(temp, s1[4].c); strcpy(s1[4].c, s1[5].c);
strcpy(s1[5].c, temp); }
printf("Name Year\n");
for(i=0;i<k;i++) {
printf("%s %d\n",s1[i].c,s1[i].a) ;
}
//if(s1[4].a==s1[5].a){
// printf("\nName Year\n%s %d",s1[5].c,s1[5].a);
//}
// else
// {
printf("\nName Year\n%s %d",s1[4].c,s1[4].a);
// }
return 0;
}

Problem Description

There are some set of novels based on the year of Publication, you have to find the book at the position 5.

  • Test Case 1
    Input (stdin)
    10
    OOPswithc++ 
    2000
    DatabaseManagementsystems 
    1998
    ComputerArchitecture
    2002
    Compilerdesign 
    2003
    Datastructures 
    2005
    JavaProgramming 
    2009
    CProgramming 
    2007
    Cloudcomputing 
    2006
    Wirelessnetworks
    2010
    Mobilecomputing 
    2014
    Expected Output
    Name Year
    Mobilecomputing 2014
    Wirelessnetworks 2010
    JavaProgramming 2009
    CProgramming 2007
    Cloudcomputing 2006
    Datastructures 2005
    Compilerdesign 2003
    ComputerArchitecture 2002
    OOPswithc++ 2000
    DatabaseManagementsystems 1998
    
    Name Year
    Cloudcomputing 2006
  • Test Case 2
    Input (stdin)
    10
    OOPswithc++ 
    2001
    DatabaseManagementsystems 
    1998
    ComputerArchitecture
    2009
    Compilerdesign 
    2003
    Datastructures 
    2015
    JavaProgramming 
    2009
    CProgramming 
    2011
    Cloudcomputing 
    2006
    Wirelessnetworks
    2010
    Mobilecomputing 
    2014
    Expected Output
    Name Year
    Datastructures 2015
    Mobilecomputing 2014
    CProgramming 2011
    Wirelessnetworks 2010
    ComputerArchitecture 2009
    JavaProgramming 2009
    Cloudcomputing 2006
    Compilerdesign 2003
    OOPswithc++ 2001
    DatabaseManagementsystems 1998
    
    Name Year
    ComputerArchitecture 2009

Problem Description

You need to handle a list of books, each with a name and a year of publication. The goal is to sort these books by their year of publication in descending order and then print the book at position 5 (index 4) after sorting.

Solution Approach

  1. Read Input:

    • Read the number of books.
    • Read each book's name and year of publication.
  2. Sort Books:

    • Sort the books by their year of publication in descending order.
  3. Output Results:

    • Print the sorted list of books.
    • Print the book at position 5 after sorting.

Explanation

  1. Data Structure:

    • struct avi holds the book name and publication year.
  2. Input Reading:

    • scanf("%d", &k); reads the number of books.
    • scanf("%s%d", s1[i].c, &s1[i].a); reads each book's name and publication year.
  3. Sorting:

    • The nested for loops sort the books in descending order based on the year of publication.
    • strcpy is used to swap both the book names and publication years.
  4. Output Sorted List:

    • The sorted list is printed with each book's name and year.
  5. Print Book at Position 5:

    • If there are at least 5 books, the book at position 5 (index 4) is printed.