1) Declare a student structure that contains : > Student\'s first and last name
ID: 3763662 • Letter: 1
Question
1) Declare a student structure that contains :
> Student's first and last name
> Student ID
2) Create the following functions:
> getStudentInfo(void)
>> Declares a single student, uses printf()/scanf() to get input
>> Returns the single student back
> printStudentInfo(student_t *st_ptr)
>> Takes the pointer to a student (to avoid making a copy)
>> Prints out all of the student information
3) Declare an array of five students
4) Using a for loop and getStudentInfo() function, get input of all the students
5) Using a for loop and printStudentInfo() function, print all the output of all students.
Explanation / Answer
1)
#include <stdio.h>
typedef struct
{
int id;
char name[32];
}
student_t;
int main(void)
{
student_t student;
printf("Enter student name: "); scanf("%s", &student.name[0]);
printf("Enter student id: "); scanf("%i", &student.id);
student_t students[100];
for (int i=0; i<100; i++)
{
printf("Enter ID of student #%i ", (i+1));
scanf("%i", &student[i].id);
}
}
2)
#include <stdio.h>
typedef struct
{
int age;
char name[32];
}
student_t;
student_t get_student_info(void);
int main(void)
{
student_t students[100];
for (int i=0; i<100; i++)
{
students[i] = get_student_info();
}
}
student_t get_student_info(void)
{
student_t student;
printf("Enter student name: "); scanf("%s", &student.name[0]);
printf("Enter student id: "); scanf("%i", &student.id);
return student;
}
3)
#include <stdio.h>
typedef struct
{
int id;
char name[32];
}
student_t;
void get_student_info(student_t *student);
int main(void)
{
student_t students[100];
for (int i=0; i<100; i++)
{
get_student_info(&student[i]);
}
}
void get_student_info(student_t *student)
{
printf("Enter student name: "); scanf("%s", &student->name[0]);
printf("Enter student id: "); scanf("%i", &student->id);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.