#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
#include
ReplyDelete#include
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); }
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("Position 5\n");
printf("%s %d",s1[4].c,s1[4].a);
// }
return 0;
}
^Copy this answer for 100% evaluation.
ReplyDelete#include
ReplyDelete#include
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); }
for(i=0;i<k;i++) {
printf("%s %d\n",s1[i].c,s1[i].a) ;
}
//if(s1[4].a==s1[5].a){
// printf("\n%s %d",s1[5].c,s1[5].a);
//}
// else
// {
printf("Position 5\n%s %d",s1[4].c,s1[4].a);
// }
return 0;
}
2021-100% evaluation
ReplyDeleteuse the above code
add stdio and string after include
ReplyDelete