I was set the task of writing a program that inputs a series of 10 points from a text file, then outputting them and the distance between the first and the last points into another text file. That works fine, but they also want the distance between each point and the one before it to be displayed too.
So far my efforts have been largely useless so any help would be excellent :)
#include <stdio.h>
#include <math.h>
#define MAX_LINES 20
int main(void){
FILE *InFile = (FILE *) NULL; /* Input file */
FILE *OutFile = (FILE *) NULL; /* Output file */
int NLines = 0; /* Actual number of lines read */
char line[100]; /* To store one line of data */
int Status = 0; /* To hold return value of file I/O functions */
float pointx[MAX_LINES], pointy[MAX_LINES], pointz[MAX_LINES]; /* Array to store the other column */
int i = 0; /* Counter variable for for loop */
float r, d;
InFile = fopen("DataIn.txt", "r"); /* Open input file */
while((NLines < MAX_LINES) && (!feof(InFile))) {/* read data, one line at a time */
fgets(line, sizeof(line), InFile);
Status = sscanf(line, "%f %f %f\n", &pointx[NLines], &pointy[NLines], &pointz[NLines]);
if (Status != 3) break; /* end of file reached */
NLines++;
}
OutFile = fopen("DataOut.txt", "w"); /* Open output file */
for(i=0;i<NLines;i++) /* print out data */
Status = fprintf(OutFile, "%f %f %f\n", pointx[i], pointy[i], pointz[i]);
r=sqrt(pow(pointx[i], 2) + pow(pointy[i], 2) + pow(pointz[i], 2))
- sqrt(pow(pointx[i+1], 2) + pow(pointy[i+1], 2) + pow(pointz[i+1], 2));
d=sqrt(r);
fprintf(OutFile, "Distance between points: %f.\n", d);
fprintf(OutFile, "Distance between points 1 and 10 is %f.\n", d);
Status = fclose(InFile) + fclose(OutFile); /* Close files */
return 0;
}
Edited by Imbellis, 09 November 2008 - 11:33 AM.


Sign In
Create Account

Back to top










