6. (20 pts) Write a program that reads an array of struct. Each structis a Circe
ID: 3720309 • Letter: 6
Question
6. (20 pts) Write a program that reads an array of struct. Each structis a Circe. Each Circle con sists of three members, x- and y-coordinates of the center of the circle, and the radius of the circle. Your program reads these circles from a file (user supplied from the command line) and stores it in an array of type Circle (defined as a struct with three members of float: x, y, and radius). You must use typedef for the type Circle. As you do not know how long the list is, use malloc and realloc to dynamically allocate the array. Malloc 100 pairs at a time. If the file contained more points than the currently allocated space could fit, reallocate 100 more space into the array. Continue tillthe entire file is read Syntax: readCircle filename Complete the program below. /*2 pts: include: */ /2 pts: typedef: /* 3 pts: command line argument and error handling: int main( 2 pts: Declare array of struet using malloc: 3 pts: Open file for reading and error handling: / /* 3 pts: Read file until eof is found and error handling 3 pts: If no more room in the array, expand the array using realloo 2 pts: close and returnExplanation / Answer
#include <stdio.h>
#include<stdlib.h>
struct Circle
{
float x;
float y;
float radius
};
int main( int argc, char *argv[] ) {
{
struct course *ptr;
// Allocates the memory for noOfRecords structures with pointer ptr pointing to the base address.
ptr = (struct Circle*) malloc (100*sizeof(struct Circle));
File *file=fopen(argv[1],"r");
int i=0,n=0
while(file!=EOF)
{
n++;
if(n%100!=0)
{
fscanf("%f %f %f",&(ptr+i)->x,&(ptr+i)->y,&(ptr+i)->radius);
}
else
{
ptr=realloc(ptr,n+100);
fscanf("%f %f %f",&(ptr+i)->x,&(ptr+i)->y,&(ptr+i)->radius);
}
i++;
}
fclose(file);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.