Novels

QUESTION

There are some set of novels based on the year of Publication, you have to find the book at the position 5.\n\nIf both the years are same, the kindly perform ascending sorting using \”Book name\”.\n\nExample:\n\nInput\n6\nJava 2009\nCPP 2009\nADA 2009\nBASIC 2009\nCPP 2010\nJAVA 2010\n\nOutput:\nADA 2009\nBASIC 2009\nCPP 2009\nJava 2009\nCPP 2010\nJAVA 2010\n\nPosition 5\nCPP 2010\n\n\n

ANSWER

#include <stdio.h>
#include<string.h>
struct book
{
char name[30];
  int year;
}bk[10];
int main()
{
  int i,j,temp,n;
  char na[30];
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
  scanf("%s%d",bk[i].name,&bk[i].year);
  }
  for(i=0;i<n;i++)
  {
  for(j=0;j<n;j++)
  {
  if(bk[j].year<bk[j+1].year)
  {
  temp=bk[j].year;
    bk[j].year=bk[j+1].year;
    bk[j+1].year=temp;
    strcpy(na,bk[j].name);
    strcpy(bk[j].name,bk[j+1].name);
    strcpy(bk[j+1].name,na);
  }
  }
  }
  printf("Name Year\n");
  for(i=0;i<n;i++)
  {
    printf("%s %d\n",bk[i].name,bk[i].year);
  }
  printf("\nName Year");
  printf("\n%s %d",bk[4].name,bk[4].year);
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro