[IN C WITH COMMENTS ]1 Question 1 A point mass consists of a 3-D location and an
ID: 3814156 • Letter: #
Question
[IN C WITH COMMENTS ]1 Question 1 A point mass consists of a 3-D location and an associated mass, such as Location: (6, 0, 2) Mass: 3g In a system of point masses, let p1, p2, ..., pn be the n 3-D points and m1, m2, ....mn be their associated masses. If m is the sum of the masses, the center of gravity C is calculated a C = 1 m (m1p1 + m2p2 + ...... + mnpn) Write a program that repeatedly inputs point-mass system data sets from an input file until an input operation fails. For each data set, display the location matrix, the mass vector, n , and the center of gravity. Each data set includes a location matrix (a matrix in which each row is a point), a one-dimensional array of masses, and the number of point masses, n. Allow n to vary from 3 to 10. Sample Data File 4 5 -4 3 2 4 3 -2 5 -4 -3 -1 2 -9 8 6 1 This sample should be stored as: location 5 -4 3 4 3 -2 -4 -3 -1 -9 8 6 mass 2 5 2 1 1 n 4 Your main function should repeatedly input and process data sets from an input file until end of file is encountered. For each point-mass system data set, display the location matrix, the mass vector, n , and the center of gravity. Implement at least the following functions: fget point mass : Takes an open input file and a maximum value for n as parameters and fills a two-dimensional array output parameter with a location matrix and a one-dimensional array output parameter with a mass vector from the data file. Returns as function value the actual value of n. center grav : Takes a location matrix, mass vector, and n value as parameters, and calculates and returns as the function value the center of gravity of the system. fwrite point mass : Takes an open output file and the location matrix, mass vector, and n value of a point-mass system as parameters and writes the system to the file with meaningful labels. For the above sample, the output file should contain: x y z m 5 -4 3 2 4 3 -2 5 -4 -3 -1 2 -9 8 6 1 Total mass: 10.000 Center of Gravity: Cx Cy Cz 1.300 0.900 0.000 For the function center grav(), the center of gravity has three values. Thus, make the center of gravity a one-dimensional array of length 3. Do not include a return statement inside this function. For the function fwrite point mass() pass the center of gravity (a one-dimensional array of length 3) as an input parameter
Explanation / Answer
// C code
#include <stdio.h>
// DECLARE MAX SIZE as 10.
const int max_size = 10;
// function to get location matrix and mass vector.
int fget_point_mass(FILE *fp,float location_matrix[max_size][max_size],float mass_vector[max_size])
{
// OPEN FILE input.txt in read mode.
fp = fopen("input.txt", "r");
// declare variable n to hold number of n point masses.
int n;
// declare row_index and column index to access location matrix.
int row_index,column_index;
if( fp != NULL ) // MAKING SURE THAT FILE IS available for READING.
{
// first row contain no of records. read that n.
fscanf(fp,"%d",&n);
// till end of file is reached.
while(!feof(fp ))
{
// start scanning row_index...
for(row_index=0; row_index<n; row_index++)
{
// // start scanning column_index...
for(column_index=0; column_index<n; column_index++)
{
// since last column contain mass once column index reaches n-1
// save that in mass vector
if(column_index==n-1)
fscanf(fp,"%f",&mass_vector[row_index]);
else
// else save it in location matrix.
fscanf(fp,"%f",&location_matrix[row_index][column_index]);
} // end local for
} // end outer for
} // end while.
// close file pointer.
fclose(fp);
} // end if.
return n; //return number of records i.e n.
}
// function to calculate center of gravity.
void center_grav(float location_matrix[max_size][max_size],float mass_vector[max_size],int n,float center_of_gravity[3])
{
// declare varible total_mass to hold total MASS.
float total_mass=0.0;
// declare i and j to access location matrix.
int i,j;
// first accesss mass vector and find total mass.
for(i=0; i<n; i++)
total_mass = total_mass + mass_vector[i];
// now start calculating Center of gravity in each direction i.e x,y,z...
for(i=0; i<3; i++)
{
// declare variable point_sum to hold point sum.
float point_sum = 0.0;
// now start scanning towards each column.
for(j=0; j<n; j++)
{
// point sum hold point sum = m1 p1 + m2 p2 +...........mn pn.
point_sum = point_sum + location_matrix[j][i]*mass_vector[j];
}
// now center of gravity is point_sum/total_mass;
// i.e (m1 p1 + m2 p2 +...........mn pn)/m;
center_of_gravity[i] = point_sum/total_mass;
}
}
// function to write results to file.
void fwrite_point_mass(FILE* fp,float location_matrix[max_size][max_size],float mass_vector[max_size],int n,float center_of_gravity[3])
{
// open file output.txt in write mode.
fp = fopen("output.txt", "w");
// declare varible total_mass to hold total MASS.
float total_mass=0.0;
int i;
for(i=0; i<n; i++)
total_mass = total_mass + mass_vector[i];
// first write x y z m to output file.
fprintf(fp,"x y z m");
// declare row_index and column index to access location matrix.
int row_index,column_index;
// start scanning across row_index.
for(row_index=0; row_index<n; row_index++)
{
// for each row print a new line.
fprintf(fp," ");
// start scanning across column_index.
for(column_index=0; column_index<n; column_index++)
{
// if it is last column access mass_vector.
if(column_index==n-1)
{
fprintf(fp,"%f",mass_vector[row_index]);
}
//otherwise access location matrix.
else
{
fprintf(fp,"%.3f ",location_matrix[row_index][column_index]);
}
}
}
// write Total mass to file.
fprintf(fp," Total mass: %.3f,",total_mass);
// write Center of Gravity to file.
fprintf(fp," Center of Gravity: Cx Cy Cz");
// write Center of Gravity to file.
fprintf(fp," %.3f %.3f %.3f",center_of_gravity[0],center_of_gravity[1],center_of_gravity[2]);
// close file pointer.
fclose(fp);
}
int main()
{
//declare location matrix and matrix vector
float location_matrix[max_size][max_size];
float mass_vector[max_size];
// n to hold number of records.
int n;
// declare ceter of gravity.
float center_of_gravity[3];
// file pointer to hold input and output file.
FILE *fp;
// call fget_point_mass to fill location_matrix and to mass vector
n = fget_point_mass(fp,location_matrix,mass_vector);
// call center_grav to fill center of gravity vector.
center_grav(location_matrix,mass_vector,n,center_of_gravity);
// call fwrite_point_mass to write to file.
fwrite_point_mass(fp,location_matrix,mass_vector,n,center_of_gravity);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.