I need help with this C program Write a program that creates a structure templat
ID: 3769633 • Letter: I
Question
I need help with this C program Write a program that creates a structure template with two members according to the following criteria:
a) The first member is a social security number. The second member is a structure with tree members. Its first member contains a first name, its second member contains a middle name, and its final member contains a last name. Create and initinialize an array of five such structures. Have the program print the data in this format:
Dribble, Flossie M. - 302039823
Only the initial letter of the middle name is printed, and a period is added. Neither the initial(of course) nor the period should be printed if the middle name members is empty. Write a function to do the printing; pass the structure array to the function.
b) Modify part a. by passing the structure value insteand of the address
Explanation / Answer
Answer:
(a)
#include <stdio.h>
struct student{
int ssn;
char fname[50];
char mname[50];
char lname[50];
};
void Showdata(struct student stu);
int main(){
struct student s1;
Showdata(s1); // passing structure variable s1 as argument
return 0;
}
void Showdata(struct student stu){
printf("Enter social security number:");
scanf("%d",&s1.ssn);
printf("Enter student's first name: ");
scanf("%s",&s1.fname);
if( isalpha(s1.fname[0]))
{
s1.fname[0] = toupper(s1.fname[0]);
}
printf("Do you have a middle name(y/n)");
scanf("%s",&q);
if(q=='y')
{printf("Enter student's middle name: ");
scanf("%s",&s1.mname);
if( isalpha(s1.mname[0]))
{
s1.mname[0] = toupper(s1.mname[0]);
}
printf("Enter student's last name: ");
scanf("%s",&s1.lname);
if(isalpha(s1.lname[0]))
{
s1.lname[0] = toupper(lname[0]);
}
printf("Your full name is: %s %s %s %d ",s1.fname,s1.sname[0],s1.lname,s1.ssn);
}
else
{ printf("Enter student's last name: ");
scanf("%s",&s1.lname);
printf("Your full name is %s %s :",s1.fname,s1.lname);
}
printf(" ");
}
}
(b) By passing the structure value insteand of the address
#include <stdio.h>
struct student{
int ssn;
char fname[50];
char mname[50];
char lname[50];
};
void Showdata(struct student *stu);
int main(){
struct student s1;
Showdata(&s1); // passing structure variable s1 as a reference
return 0;
}
void Showdata(struct student *stu){
printf("Enter social security number:");
scanf("%d",&s1.ssn);
printf("Enter student's first name: ");
scanf("%s",&s1.fname);
if( isalpha(s1.fname[0]))
{
s1.fname[0] = toupper(s1.fname[0]);
}
printf("Do you have a middle name(y/n)");
scanf("%s",&q);
if(q=='y')
{printf("Enter student's middle name: ");
scanf("%s",&s1.mname);
if( isalpha(s1.mname[0]))
{
s1.mname[0] = toupper(s1.mname[0]);
}
printf("Enter student's last name: ");
scanf("%s",&s1.lname);
if(isalpha(s1.lname[0]))
{
s1.lname[0] = toupper(lname[0]);
}
printf("Your full name is: %s %s %s %d ",s1.fname,s1.sname[0],s1.lname,s1.ssn);
}
else
{ printf("Enter student's last name: ");
scanf("%s",&s1.lname);
printf("Your full name is %s %s :",s1.fname,s1.lname);
}
printf(" ");
}
}