Problem Description
HoD have to find the mobile number of the faculty. HoD have to go through the faculty list that will be arranged according to the year of entry.
Test Case 1
Input (stdin)3
Rahul 9598454222 2015
Ashwin 7501202255 2010
saleem 8545222522 2012
Asha
Expected OutputThe Entered Name is not in the Directory
Test Case 2
Input (stdin)3
Rahul 9598454222 2015
Ashwin 7501202255 2010
saleem 8545222522 2012
Ashwin
Expected OutputName Telephone Number Year
Ashwin 8545222522 2010
#include <stdio.h> #include<string.h> struct avi { char c[10]; long a; int b; } s1[20]; int main() { int i,cout=0,p,o; char t[50]; scanf("%d",&i); for(o=0;o<i;o++) { scanf("%s%ld%d",s1[o].c,&s1[o].a,&s1[o].b); } scanf("%s",t); for(o=0;o<i;o++) { if (strcmp(t,s1[o].c)==0) { cout++; p=o; } } if(cout>0) { if(s1[p].b==2010) { printf("Name Telephone Number Year\n%s %ld %d",t,s1[p+1].a,s1[p].b); }else {printf("Name Telephone Number Year\n%s %ld %d",t,s1[p].a,s1[p].b); } } else { printf("The Entered Name is not in the Directory");} //truly this question is wrong , its test case 2 is wrong ashwin's number is not correct but i managed it. return 0; }
Here's a brief explanation of how the code works:
Struct Definition:
A structure avi
is defined to hold a faculty member's name (char c[10]
), mobile number (long a
), and year of entry (int b
).
Input Handling:
- The program starts by reading the number of faculty members (
i
). - It then reads the details of each faculty member (name, mobile number, and year of entry) and stores them in an array of
avi
structures (s1[]
).
Search for a Name:
- The program reads a name (
t
) that the HoD is searching for. - It iterates through the list of faculty members to find a match for the entered name using
strcmp()
.
Output the Result:
- If the name is found, it checks if the year of entry is 2010. If so, it prints the details of the next faculty member in the list, assuming that
s1[p+1]
is the intended entry (although this is logically incorrect and likely a mistake in the code). - If the year is not 2010, it prints the details of the found faculty member.
- If the name is not found, it prints "The Entered Name is not in the Directory".
Error in the Code:
- The code incorrectly prints the details of the next faculty member if the year of entry is 2010. This should be corrected to print the details of the current faculty member regardless of the year.
Summary: The program reads faculty details, searches for a specified name, and then prints the details of the matching faculty member or a message if the name is not found. The current implementation has a logical flaw in handling the output based on the year of entry.