Jump to content

Vector program problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
Hey there, I'm new here and I've only been coding for a little while for a course at university so forgive any stupid mistakes and such!

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.


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
The code that might allow me to compile and observe program behavior appears to have been elided and replaced with '...'. It might serve you better to post a minimally compilable code sample that demonstrates the issue(s), with a sample of the input data file as well.

#3
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
Sorry :S The original code was huge. I've edited it and taken out some if statements but it should essentially work. The attached file is a data example.

Attached Files



#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
All you really need is a for loop to do the distance formula to point i and point i+1.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
Sorry I'm being pretty thick at the moment... how do I do that?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You already have the distance formula (to calculate distance between start/end points). Also, I just noticed that you don't actually find the distance between points 1 and 10 in your code, but between points 20 and 21. You need to use {} to make your for loop work on more than one statement.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Isn't the distance between two points more like this?
            float xterm = x[i + 1] - x[i];
            float yterm = y[i + 1] - y[i];
            float zterm = z[i + 1] - z[i];
            float dist  = sqrt(xterm * xterm + yterm * yterm + zterm * zterm);


#8
Imbellis

Imbellis

    Newbie

  • Members
  • Pip
  • 7 posts
Yeah I realised the bracket thing earlier :S noob mistake. I think my distance calculation should work but its confusing to look at right now so I'm going to change it to your one dcs. I have managed to make it loop properly now which is awesome.

Thanks guys :)