Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include <stdio.h> #include <math.h> #define MAX 4.0 #define MIN 2.0 int main(vo

ID: 3727785 • Letter: #

Question

#include <stdio.h> #include <math.h> #define MAX 4.0 #define MIN 2.0 int main(void) {
FILE *inFile; //read file
inFile = fopen("ATOMS.DAT", "r"); //open file
//declare variable char line[100]; int n = 0; int id = 0; double dis = 0; int cnt = 0; double sum = 0; double avg = 0; fscanf(inFile, "%d", &n);
//create arrays for all x, y, and z coordinates double x[n]; double y[n]; double z[n]; //skip comments fscanf(inFile, "%s", line); fscanf(inFile, "%s", line);
for (int i = 0; i < n; i++) { //read file line by line and assign to variables fscanf(inFile, "%d", &id); fscanf(inFile, "%lf", &x[i]); fscanf(inFile, "%lf", &y[i]); fscanf(inFile, "%lf", &z[i]);
} for (int i = 0; i < n; i++) { //for loop for (int j = i + 1; j < n; j++) { //distance between i and j dis = sqrt( pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2) + pow(z[i] - z[j], 2));// distance formula
//check dis in range or not if (dis >= MIN && dis <= MAX) { sum = sum + dis;
//increment the count cnt++;
}
} }
//calculate the avg avg = sum / cnt; //display printf("The average of distances in range is %lf", avg); }
Modify your atoms.c from HW assignment 3 so that it make use of struct to hold the coordinate data. (i.e., declare a struct with three members: x, y, and z, and then declare an array of that struct. Read data in and populate the array of struct, and then loop through it to calculate)

Explanation / Answer

The modified code would be

#include <stdio.h>
#include <math.h>
#define MAX 4.0
#define MIN 2.0
struct co_od
{
double x;
double y;
double z;
};
typedef struct co_od cood;
int main(void) {

FILE *inFile; //read file

inFile = fopen("ATOMS.DAT", "r"); //open file

//declare variable
char line[100];
int n = 0;
int id = 0;
double dis = 0;
int cnt = 0;
double sum = 0;
double avg = 0;
fscanf(inFile, "%d", &n);

//create arrays for all x, y, and z coordinates
cood arr[n];
//skip comments
fscanf(inFile, "%s", line);
fscanf(inFile, "%s", line);

for (int i = 0; i < n; i++) { //read file line by line and assign to variables
fscanf(inFile, "%d", &id);
fscanf(inFile, "%lf", &arr[i].x);
fscanf(inFile, "%lf", &arr[i].y);
fscanf(inFile, "%lf", &arr[i].z);

}
for (int i = 0; i < n; i++) { //for loop
for (int j = i + 1; j < n; j++) {
//distance between i and j
dis = sqrt(
pow(arr[i].x - arr[j].x, 2) + pow(arr[i].y - arr[j].y, 2)
+ pow(arr[i].z - arr[j].z, 2));// distance formula

//check dis in range or not
if (dis >= MIN && dis <= MAX) {
sum = sum + dis;

//increment the count
cnt++;

}

}
}

//calculate the avg
avg = sum / cnt;
//display
printf("The average of distances in range is %lf", avg);
}

DO give a thumbs up and in case there are doubts leave a comment.