Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The program should calculate the center of mass of a set of objects read in from a provided data file. The program will then output

The program should calculate the center of mass of a set of objects read in from a provided data file. The program will then output the location of the center of mass to a file, plotted using the gnuplot plotting tool.

Your program should: 1. Read in the input data file and store the circles in a dynamically sized array (using pointers)

2. Calculate the center of mass in the X/Y plane. Use the x and y locations of the center points and use the area of each circle as its mass (little m) value

3. Output the location of the centroid in a new file (command line argument). The output file will consist of two whitespace separated numbers, i.e.

You will be reading in gnuplot files that describe circles. The names of input/output files are given as command line arguments. First line is a comment thatdenotes how many circles described in the file, each subsequent line consists of an x coordinate, y coordinate, and radius. The input file, circles.dat, is listed here:

#7

1.3 1.4 1.0

2.1 2.9 2.5

3.5 2.6 0.5

4.3 1.7 1.0

2.8 2.3 2.0

4.3 1.3 1.5

1.9 1.8 0.5

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

For this lab, you will be provided the following files: lab2.h the function prototypes and struct will live here lab2.c the function implementation will live here. driver.c main lives here. circles.dat the circle information likes here

The code will be tested using several variations of the circles.dat file. The following files are used with the gnuplot plot_circles.plg plot_centroid.plg

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

You will be implementing 3 functions: main, read_data, and calc_centroid. The prototype for the functions read_data and calc_centroid, as well as, the descriptions of their purpose can be found in lab2.h. The main and description of what should go in main can be found in driver.c .

Ensure input/output files open correctly. If not, print message to the user and exit program.

When passing information to a program using command line arguments, ensure correct number of arguments pass to command line when running program. If not, print message to user and exit program.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// lab2.c //

#include

#include

#include

// Circle Type, use this to represent the data from the input file //

typedef struct circle_t {

double x,y,r;

} circle;

/*

This function takes in a File pointer and an int pointer

The function should:

1. Read the first line of the input file to determine how many circles there are

1a. Store the number of circles in the passed int pointer

2. Use malloc to allocate memory for an array of circles

3. Read and store all circles in the file

4. Return the malloc'd array of circles

*/

circle* read_data(FILE* input, int* num);

/*

This function takes an output File pointer, an array of circles

and the number of circles in the array. It should:

1. Calculate the centroid/center of mass for all the circles

1a. Find area for each circle, keep running total

1b. Calculate X sum: x location * area

1c. Calculate Y sum: y location * area

1d. Divide each sum by total area

2. Write centroid out to file

*/

void calc_centroid(FILE* output, circle* circles, int num);

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

/ Lab2.h /

/*Implementation of these functions go in this file*/

circle* read_data(FILE* input, int* num)

{

}

void calc_centroid(FILE* output, circle* circles, int num)

{

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// Driver.c //

/*

Main should:

1. Open a file for reading, the input file from command line args

check to make sure the correct number of argurments were on the command line

2. Open a file for writing, the output file from command line args

Make sure the files open appropriately

Make sure you close the files when they are no longer needed

3. Declare an int to send to read_data

4. Call read_data

5. Call calc_centroid

6. return 0

Make sure you release the dynamically allocated memory when it is no longer needed

*/

int main(int argc, char const *argv[])

{

return 0;

}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// plot_circles //

if (!exists("circlefile")) circlefile='circles.dat'

set title "Circles"

set xlabel "x"

set ylabel "y"

set style fill transparent solid 0.2 noborder

plot circlefile w circles

pause -1 "Hit any key to continue"

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// plot_centroid //

if (!exists("circlefile")) circlefile='circles.dat'

set title "Circles + Centroid"

set xlabel "x"

set ylabel "y"

set style fill transparent solid 0.2 noborder

plot circlefile w circles, \

"centroid.dat" w points pointsize 3 pointtype 7

pause -1 "Hit any key to continue"

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced Oracle Solaris 11 System Administration

Authors: Bill Calkins

1st Edition

0133007170, 9780133007176

More Books