Question
Please answer in C language. Implement the functions growArray and addStudent as prototyped. If addStudent is called and the array is full, use growArray to
Please answer in C language.
Implement the functions growArray and addStudent as prototyped. If addStudent is called and the array is full, use growArray to increase the array's capacity before adding the student. Hint: The array in StudentArray is a pointer to a dynamically allocated block of memory. Is there any reason we can't point it to a larger, newly allocated block?
#include
#include "t04util.h"
#define ARR_SIZE 2
typedef struct { StudentType **arr; int count; int capacity; } StudentArray;
void initStuArray(StudentArray *); void printArray(const StudentArray *);
/* Implement the following functions */ void growArray(StudentArray *); void addStudent(StudentArray *, StudentType *);
int main(void) { StudentType *matilda, *joe, *timmy; matilda = (StudentType *) calloc(1, sizeof(StudentType)); joe = (StudentType *) calloc(1, sizeof(StudentType)); timmy = (StudentType *) calloc(1, sizeof(StudentType));
initStudent("Matilda", "Mallister", 22, "123444555", 9.0, matilda); initStudent("Joe", "The Plumber", 24, "567888999", 8.7, joe); initStudent("Timmy", "Tortoise", 99, "345667788", 11.5, timmy); StudentArray * comp2404 = (StudentArray *) calloc(1, sizeof(StudentArray));
initStuArray(comp2404);
/* Uncomment the following lines once addStudent is implemented */
// addStudent(comp2404, matilda); // addStudent(comp2404, joe); // addStudent(comp2404, timmy);
printArray(comp2404);
free(comp2404->arr); free(comp2404);
return 0; }
void initStuArray(StudentArray *stuArray) { stuArray->count = 0; stuArray->capacity = ARR_SIZE; stuArray->arr = (StudentType **) calloc(stuArray->capacity, sizeof(StudentType *)); }
/* Function: growArray */ /* in: Location of StudentArray to be modified */ /* out: Modified StudentArray */ /* Purpose: double the size of stuArray->arr */ void growArray(StudentArray * stuArray) { }
/* Function: addStudent */ /* in: Location of StudentArray to be modified */ /* in: Location of StudentType to be added */ /* out: StudentArray with added StudentType */ /* Purpose: adds stu to stuArray->arr; if stuArray->arr */ /* is full, grow it then add stu. */ void addStudent(StudentArray * stuArray, StudentType * stu) { }
void printArray(const StudentArray * stuArray) { int i;
printf("StudentArray "); printf("Capacity: %d ", stuArray->capacity); printf("Count: %d ", stuArray->count); printf("Students: "); for (i = 0; i < stuArray->count; i++) { printStudent( stuArray->arr[i] ); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started