How would I change this program to create a struct for the three arrays of x, y,
ID: 3727786 • Letter: H
Question
How would I change this program to create a struct for the three arrays of x, y, and z. Then create an array from that struct?#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); }
How would I change this program to create a struct for the three arrays of x, y, and z. Then create an array from that struct?
#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); }
How would I change this program to create a struct for the three arrays of x, y, and z. Then create an array from that struct?
#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); }
Explanation / Answer
According to your problem code is below but there are some points
-: for access the array x,y,z(member of array) we use the dot(.) opretor
-: size of array x,y,z we can't decide so i take 100000. you can take size of x,y,z according to your problem
-: i don't have your file ("ATOMS.DAT") so it is not possible to run your program and attach output.
c code
#include <stdio.h>
#include <math.h>
#define MAX 4.0
#define MIN 2.0
int n;
struct coordinate//structure of array x,y,z;
{
double x[100];
double y[100];
double z[100];
};
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);
struct coordinate arr[n];//declare the array of structure coordinate
//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", &(arr[i].x[i]));//array x,y,z are the members of structure
fscanf(inFile, "%lf", &(arr[i].y[i]));//so for take input we use dot(.) opretor
fscanf(inFile, "%lf", &(arr[i].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(arr[i].x[i] - arr[j].x[j], 2) + pow(arr[i].y[i] - arr[j].y[j], 2)
+ pow(arr[i].z[i] - arr[j].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);
}
if you got my point then please like otherwise comment
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.