centroid

.c

School

Clemson University *

*We aren’t endorsed by this school

Course

2000

Subject

Computer Science

Date

Oct 30, 2023

Type

c

Pages

3

Uploaded by MinisterRainGrasshopper26

Report
// *** // *** DO NOT modify this file // *** #include "centroid.h" /* Centroid_create: creates a single centroid @param dim - dimension of centroid @param id - id for the centroid */ Centroid * Centroid_create (int dim, int id) { Centroid * cent = malloc (sizeof (*cent)); cent-> data = malloc (sizeof (* (cent-> data)) * dim); cent->dimension = dim; cent->ID = id; cent->size = 0; return cent; } // Centroid_createArray: to create array of centroids // @param nrow - number of centroids // @param dim - dimension of each centroid Centroid * *Centroid_createArray (int nrows, int dim) { Centroid * *centroids = malloc (sizeof (*centroids) * nrows); if (centroids == NULL) { return centroids; } int niter; for (niter = 0; niter < nrows; niter++) { centroids[niter] = Centroid_create (dim, niter); } return centroids; } /* Centroid_reset - function to put all values of centroid to zero @param Centroid * - pointer to centroid which needs to be reset */ void Centroid_reset (Centroid * cent) { cent->size = 0; // set all values to zero memset (cent->data, 0, sizeof (int) * (cent->dimension)); } /* Centroid_addPoint - function to add points to the centroid @param Centroid * - pointer to centroid to which point needs to be added @param DataPoint * - pointer to datapoint which is being added to the centroid */ void Centroid_addPoint (Centroid * cent, DataPoint * dp) { int iter;
// for each dimension, add the value of dp to the centroid int dim = cent->dimension; for (iter = 0; iter < dim; iter++) { cent->data[iter] += dp->data[iter]; } cent->size++; } /* Centroid_findCenter - function to find the present center of the cluster after adding all points to centroids @param Centroid * - pointer to centroid whose center is being calculated */ void Centroid_findCenter (Centroid * cent) { if ((cent->size) == 0) // the center is the origin { return; } int iter; int size = cent->size; // for each dimension, add the value of dp to the centroid int dim = cent->dimension; for (iter = 0; iter < dim; iter++) { cent->data[iter] /= size; // integer division } } /* Centroid_cmp - function to compare 2 centroids to be used for sorting */ int Centroid_cmp (const void *ptr1, const void *ptr2) { const Centroid *cent1 = *(const Centroid **) ptr1; // casting pointer types const Centroid *cent2 = *(const Centroid **) ptr2; int dim = cent1 -> dimension; int ind; for (ind = 0; ind < dim; ind ++) { if ((cent1->data)[ind] != (cent2->data)[ind]) { return (cent1->data)[ind] - (cent2->data)[ind]; } } return 0; } /* Centroid_print - function to print the centroids to a file */
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: Suppose you wanted to know if there was a relationship between time spent on the internet and IQ.…
Q: LO 77 Identify the elements that make a quantitative trait. Which of the following characteristics…
Q: How can you predict the length of the cash conversion cycle within that cycle for a parts store?
Q: 9.A car changes it velocity from 10 m/s to 40 m/s over a 4 second interval. a. What is the car's…
Q: 5) A card is drawn randomly from a standard 52-card deck. Find the probability that the card drawn…
Q: Given P(x) = 3x5 – 8x4 + 70x³ – 194x? – 125x + 150 and that 5i is a zero, write P in factored form…
Q: Use the definition of the derivative to find the derivative of the function f (x) = Then find all…
Q: A university interested in tracking its honors program believes that the proportion of graduates…
Q: Use the t-distribution to find a confidence interval for a mean given the relevant sample results.…
Q: 13. According to Wikipedia, as of November 30, 2019, 43.5% of registered voters in Mercer County are…
Q: Jefferson Midwest Trust has assets of $500,000 and liabilities of $400,000. Jefferson Midwest…
Q: (1) Given f(x)=2x+√x, find (ƒ-¹) (3)
Q: 2+4+6+ + 2n = n(n+1) for all n ≥ 1. In your own words, what is the difference between the principle…
Q: Imagine a regression with YEAR (as in 2015 or 2001) predicting SALES (in thousands of dollars) as…
Q: 1. One thousand two hundred and seventy deer are living on an island that is eight hundred and…
Q: Begin by selecting the formula to calculate the predetermined overhead (OH) allocation rate. Then…
Q: Given that; f(2)=2, f'(2)=3 g(2)-6, g'(2)=4 Calculate the following (fg) 2 = (f/g)'(2)= (g/f) (2)=
Q: Which of the following is not an assumption of a linear regression? A The residuals follow a…
Q: 10. The distribution of the weights of loaves of bread from a certain bakery follows approximately a…
Q: I know I correctly inputting in the def the balance and name but I am stumped on how to allow it to…
Q: Part 1 of Suppose you want to find the volume of the solid of revolution formed when the region…
Q: While creating an information system architecture to serve this new service, a number of concerns…