Given the global structure type: typedef struct { char name[30]; long ssn; int g
ID: 3666602 • Letter: G
Question
Given the global structure type: typedef struct { char name[30]; long ssn; int grades[5]; float average; } STUDENT; Write a main ( ) function that : Creates an object of type STUDENT; Passes the address of this object to a function called GetData ( ) ; Passes the object by value to a function called WriteData ( ) ; Then write the function GetData ( ) that : Receives the address of the structure object as one of its formal arguments ; Prompts the terminal operator and receives the name field ; Prompts the terminal operator and receives the ssn field ; Prompts the terminal operator and lops 5 times to receive all entries in the grades array ; Computes the floating point average of the 5 grades and stores it into the field average ; Returns control back to main ( ). Programming in C++ Chapter One: Moving From C to C++ Objects Academy, 2016 Page 1-22 Finally, write the function WriteData ( ) that: Prints a title line to identify the NAME, SSN, and AVERAGE ; Prints the name, ssn, and the average ( to an accuracy of 2 decimal positions). Returns control back to main ( ).
Explanation / Answer
#include<stdio.h>
typedef struct { char name[30]; long ssn; int grades[5]; float average; } STUDENT;
STUDENT GetData(STUDENT student1);
void WriteData(char *,long,float);
main()
{
STUDENT student1;
student1=GetData(student1);
WriteData(student1.name,student1.ssn,student1.average);
}
STUDENT GetData(STUDENT student1)
{
int i;
float grade_sum=0;
printf(" Plese enter your name: ");
scanf("%[^ ]s",&student1.name);
printf(" Plese enter your ssn: ");
scanf("%ld",&student1.ssn);
for(i=1;i<=5;i++)
{
printf(" PLese enter gread no %d: ",i);
scanf("%d",&student1.grades[i]);
grade_sum=grade_sum+student1.grades[i];
}
student1.average=grade_sum/5;
return student1;
}
void WriteData(char *name,long ssn,float average)
{
printf(" Hi %s, Please find your details below...",name);
printf(" Name: %s",name);
printf(" SSN: %ld",ssn);
printf(" Avarage Grade: %.2f",average);
}
//Hope it helps. :-)
//If you are not able to understand any of above, please comment, I’ll try to explain further.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.