You are asked to define a user-defined data type for students, that will contain
ID: 3589838 • Letter: Y
Question
You are asked to define a user-defined data type for students, that will contain the following information about a student: ID number, first name, last name, major, GPA. Your structure should be cal Student. Based on the defined structure, user name on the command line, show how you will use a loop to search for the given name within the array of 5 students. use an array to record information about 5 students. Then, given a Write a C program that will accept a user name as a string input from the user, and then determines whether or not the user name appears as one of the names in the 5 student records. Your program should prompt the user for inputs. If the name is found, your program should display the information in the student's record. You may use predefined functions, such as strempo if you wish. However, this is NOT required. What to submit: (1) Hard copy of your program code(s) 2) Screen shots showing results for sample runs of the program, for at least 3 input casesExplanation / Answer
#include<stdio.h>
#include<strings.h>
struct Student
{
char Id_No[10]; // ID number
char fname[20]; // first name
char lname[20]; // last name
char major[20];
float gpa; // Grade point average
};
int main()
{
struct Student s[5]; // array to record information upto 5 students
int i; // array index
char usrname[20]; // to accept user name from the input device
int flag = 0; // flag is set one, when given user input name is found in student list
for(i=0;i<5;i++)
{
printf("Enter %d student details : ", i+1);
printf("Enter ID number : ");
scanf("%s", s[i].Id_No);
printf("Enter First name : ");
scanf("%s", s[i].fname);
printf("Enter Last name : ");
scanf("%s", s[i].lname);
printf("Enter major : ");
scanf("%s", s[i].major);
printf("Enter GPA : ");
scanf("%f", &s[i].gpa);
}
printf("Enter a user name : ");
scanf("%s", usrname);
for(i=0;i<5;i++)
{
if ( strcasecmp(s[i].fname,usrname) == 0 || strcasecmp(s[i].lname,usrname) == 0)
{
printf("%s - student details found ... ", usrname);
printf("=============================================== ");
printf("Student Id Number : %s ", s[i].Id_No);
printf("First name : %s ", s[i].fname);
printf("Last name : %s ", s[i].lname);
printf("Major : %s ", s[i].major);
printf("GPA : %.1f ",s[i].gpa);
flag=1;
break;
}
}
if( flag == 0 )
{
printf("%s - student information not found ", usrname);
}
return 0;
}
/*
lenovo@lenovo-Vbox:~/chegg$ uname -a
Linux lenovo-Vbox 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
lenovo@lenovo-Vbox:~/chegg$ cc stu.c
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter 1 student details :
Enter ID number : 101
Enter First name : Mars
Enter Last name : Martin
Enter major : B.Tech
Enter GPA : 4.3
Enter 2 student details :
Enter ID number : 102
Enter First name : Duke
Enter Last name : james
Enter major : B.Com
Enter GPA : 2.0
Enter 3 student details :
Enter ID number : 103
Enter First name : Belew
Enter Last name : kem
Enter major : B.Sc
Enter GPA : 3.3
Enter 4 student details :
Enter ID number : 104
Enter First name : Preston
Enter Last name : paul
Enter major : B.Tech
Enter GPA : 8.4
Enter 5 student details :
Enter ID number : 105
Enter First name : Steve
Enter Last name : jobs
Enter major : B.Sc
Enter GPA : 5.6
Enter a user name : paul
paul - student details found ...
===============================================
Student Id Number : 104
First name : Preston
Last name : paul
Major : B.Tech
GPA : 8.4
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter 1 student details :
Enter ID number : 101
Enter First name : Mars
Enter Last name : Martin
Enter major : B.Tech
Enter GPA : 4.3
Enter 2 student details :
Enter ID number : 102
Enter First name : Duke
Enter Last name : james
Enter major : B.Com
Enter GPA : 2.0
Enter 3 student details :
Enter ID number : 103
Enter First name : Belew
Enter Last name : kem
Enter major : B.Sc
Enter GPA : 3.3
Enter 4 student details :
Enter ID number : 104
Enter First name : Preston
Enter Last name : paul
Enter major : B.Tech
Enter GPA : 8.4
Enter 5 student details :
Enter ID number : 105
Enter First name : Steve
Enter Last name : jobs
Enter major : B.Sc
Enter GPA : 5.6
Enter a user name : Duke
Duke - student details found ...
===============================================
Student Id Number : 102
First name : Duke
Last name : james
Major : B.Com
GPA : 2.0
lenovo@lenovo-Vbox:~/chegg$
lenovo@lenovo-Vbox:~/chegg$ ./a.out
Enter 1 student details :
Enter ID number : 101
Enter First name : Mars
Enter Last name : Martin
Enter major : B.Tech
Enter GPA : 4.3
Enter 2 student details :
Enter ID number : 102
Enter First name : Duke
Enter Last name : james
Enter major : B.Com
Enter GPA : 2.0
Enter 3 student details :
Enter ID number : 103
Enter First name : Belew
Enter Last name : kem
Enter major : B.Sc
Enter GPA : 3.3
Enter 4 student details :
Enter ID number : 104
Enter First name : Preston
Enter Last name : paul
Enter major : B.Tech
Enter GPA : 8.4
Enter 5 student details :
Enter ID number : 105
Enter First name : Steve
Enter Last name : jobs
Enter major : B.Sc
Enter GPA : 5.6
Enter a user name : kems
kems - - student information not found
lenovo@lenovo-Vbox:~/chegg$
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.