Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
<#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;
double input_status,
d = 1,
r,
q,
r1,
q1,
total_perimeter;

int t=0  ;

int main()

{
	errno_t err;

	// Open for read (will fail if file "crt_fopen_s.c" does not exist)
	err = fopen_s(&pg, "input.txt", "r");
	if (err == 0)
	{
		printf("The file 'input.txt' was opened\n \n ");
	}
	else
	{
		printf("The file 'input.txt' was not opened\n");
	}
	
	 fopen_s(&pg , "input.txt" , "r");

	 do {

		 input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
		 t = t + 1;
		 printf("point %d : r %lf q %lf \n", t, r, q);
		
		t = t + 1;
		 input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
		 printf("point %d : r %lf q %lf \n", t, r1, q1);

		 d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos(q1 - q)));
		 printf("distance between %d and %d  %lf \n \n ",(t-1) ,t , d);
		
	 } while (input_status != EOF);
	 
    return 0;

}
Posted
Updated 14-Dec-15 16:47pm
v4
Comments
Sergey Alexandrovich Kryukov 14-Dec-15 22:40pm    
All right... Now, you have to make a question out of this post. When you say set of values, this should be a mathematically well-defined object. Define this set formally? Where are the elements of this set, in terms of the objects created by your code. If this set has few elements, perhaps you can list them all; if not, you can always describe the set, what belongs to it. Then, you said "extra". It means that you wanted some set of some values, and obtained it, plus some other set. So, describe 1) what you expected to get? 2) what you got instead? 3) why do you feel it's wrong. And please explain the purpose of the whole thing, what you wanted to achieve, why.
—SA
Member 12202551 14-Dec-15 22:54pm    
this is my hole problem and this what i get so far , i am still working on it
Member 12202551 14-Dec-15 23:01pm    
point 1 : r 3.00000 q 180.000
point 2 : r 4.472136 q 116.56500
distance between 1 and 2 2.2627111
an so on
point 3 : r q
point 4 : r q
distance between 3 and 4
point 5 : r q
point 6 : r q
distance between 5 and 6
point 7 : r 6.403124 q 308.659800
point 8 : r 5.200961 q 268.898300
distance between 7 and 8
my ans should be stopped at this point but this give me an extra value of point 9 and 10
point 9 : r 6.403124 q 308.659800
point 10 : r 5.200961 q 268.898300
distance between 9 and 10
[no name] 14-Dec-15 23:46pm    
You know how it should work. Use your debugger to step through the code to see where the "extra" values are coming from. This is the standard next step.

The reason is that the EOF condition is only reached if you try to read BEYOND the end of the file.

So the last time you read the last two values there was no EOF.

So the do-loop does not fall out.

So it goes back and tries to read two more sets but THEN that is when the EOF condition is reached and the loop falls out.

BUT by then you have already tried to read data twice over but those fail of course... but the values r and q and r1 and q1 have been read from the previous time and are not changed.

So what you are getting is the last two pairs printed again.

You should test for the EOF AFTER having tried to read and exit the loop

So test for the EOF AFTER the attempt to read r and q and just exit the loop (use the break statement) without any printing or proceeding to reading r1 and q1.

I hope this helps.

example...
C++
...
	 do {
 
		 input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
                 if(input_status == EOF) break; //<<---- look here
		 t = t + 1;
		 printf("point %d : r %lf q %lf \n", t, r, q);
		
		t = t + 1;
		 input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
		 printf("point %d : r %lf q %lf \n", t, r1, q1);
 
		 d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos(q1 - q)));
		 printf("distance between %d and %d  %lf \n \n ",(t-1) ,t , d);
		
	 } while (input_status != EOF);
....
 
Share this answer
 
v4
Comments
Member 12202551 15-Dec-15 1:59am    
thanks for help ......... that worked
Member 12202551 15-Dec-15 2:04am    
but how can i calculate total perimeter ????
like sum of all sides ??
d only store value of last two points
that means value of last calculated side only .....
LeumasD 15-Dec-15 2:19am    
The algorithm you are using is reading two point coordinates at a time.

You really do not need to do that... what you ought to do is

1- read the first point r,q
2- print the values if you want
3- store this point for later to be able to close the polygon r0,q0
4- P=0
5- loop through the rest of the points
______read coordinates r1,q1
______exit if EOF
______print the values if you want
______calc distance D between r,q and r1,q1 as you do already
______print if you want
______make P=P+D
______make r=r1 and q=q1 so as to make the previous point become the current point
6- Now set r1=r0 and q1=q0 which makes the current point become the very first point and thus closing the polygon
7- calculate D as before between r,q and r1,q1 and make P=P+D
8- print P


That is it.

The above assumes that the data in the file does not contain a repetition of the first point as the last point to close the polygon.

If it does then you do no need to store the first point and you do not need steps 6 and 7 since the loop will read the last point which will be the first point and the loop would have already calculated the final side.

I hope that helps.
Member 12202551 15-Dec-15 3:18am    
thanks for that, but sorry i cant understand as i am learning this
can u please explain a bit more from 5 to 6??
thanks
LeumasD 15-Dec-15 3:36am    
I have just noticed that your data points are in Polar Coordinates not in Cartesian coordinates... so I adjusted the naming of variables in the pseudo code I gave you above.... read it again.

I will try and see if I can give you some real code in a few minutes.

Here is a solution that also calculates the perimeter.

It implements the pseudo-code I gave you above.

I noticed that your data points are polar coordinates and the angles are given in DEGREES not RADIANS.

This means your formula to calculate the distance using the cos() function is incorrect because the cos() function needs angles in RADIANS... so you need to convert the (q1-q) to radians before you use the cos().

This is done by: (q1-q)*dtor

So now the formula becomes
C#
d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));



The variable dtor is a defined at the top of the program as pi/180.

Here is the program

C++
#include "stdafx.h"
#include <stdio.h>
#include <math.h>

FILE *pg;
double input_status, d,r0,q0,r,q,r1,q1,total_perimeter=0;
int t=0;
double dtor = 3.14159265358979 / 180.0; //<<---- notice 
int main() 
{
     errno_t err;
     // Open for read (will fail if file "input.txt" does not exist)
     err = fopen_s(&pg, "input.txt", "r");
     if (err == 0)
        printf("The file 'input.txt' was opened\n\n");
     else
     {
        printf("The file 'input.txt' was not opened\n");
        return 0; //exit the program
     }
     //fopen_s(&pg , "input.txt" , "r");   //you really do not need this line
     input_status = fscanf_s(pg, "%lf %lf ", &r, &q);
     r0 = r; q0 = q;  //save the first point for later to close the polygon
     if(input_status==EOF)
     {
        printf("There are no data points in the file");
        return 0; //exit the program
     }
     printf("point %d: r=%lf , q=%lf\n", t, r, q);
     do
     {
          input_status = fscanf_s(pg, "%lf %lf ", &r1, &q1);
          if(input_status==EOF) 
               break; //if no more points exit the loop
          t++;
          printf("point %d: r=%lf , q=%lf\n", t, r1, q1);
          d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
          printf("distance between %d and %d = %lf \n\n",(t-1),t,d);
          r = r1; q = q1;  //make the previous point become the current point 
          total_perimeter += d; //accumulate the perimeter             
     }while(1);
     r1 = r0; q1 = q0;  //make the new point become the first point to close the polygon
     d = sqrt((r*r) + (r1*r1) - (2 * r*r1) *(cos((q1 - q)*dtor)));
     printf("distance between %d and 0 = %lf \n\n\n",t,d);
     total_perimeter += d;  //accumulate the perimeter
     printf("Total Perimiter = %lf\n",total_perimeter);     
     return 1; 
}
 
Share this answer
 
v8
Comments
Member 12202551 15-Dec-15 22:21pm    
thanks a lot for your help ....
Member 12202551 15-Dec-15 22:37pm    
thanks ... but can u please tell me again whats going on ... from where .....
while (1) ....to .... return (1)
i want to understand that part
thanks
LeumasD 15-Dec-15 23:22pm    
In regards to the return 1... If you notice for the error situations I used return 0.

This makes the program terminate and return a value of 0 (i.e. false)... you could instead have returned any value indicating some error condition.

I elected to also return 1 (i.e. true) when the program ran all the way to the end and terminated correctly... you could use instead a 0 to indicate no error and any other number to indicate an error code.

LeumasD 15-Dec-15 23:30pm    
In regards to the while (1) the 1 means true.

I was not sure what your compiler uses for true... is it TRUE or true or what... but 1 always means true in C or C++ so that while (1) is the same as saying while(true) or while (TRUE) etc.

Using a loop that is do...while(true) is an endless loop.

So the loop will never finish UNLESS some statement within the loop BREAKS the loop and exits.

If you notice that is what I do right after trying to read the two values r and q from the file and test to see if EOF has been triggered.

So the loop keeps going round and round reading from the file until the EOF is encountered upon which the statement break is executed and thus exiting the loop.

You had it before where you where testing to see if the EOF is encountered in the while() itself.

That was not the right place since it was either too early or too late and had no effect at all.

The loop needs to be exited right after attempting to read from the file and it happens to have been the EOF not later and if EOF is not encountered then testing for that condition at the while() is of no use.... as you have seen earlier where you were getting additional spurious printouts.


Member 12202551 16-Dec-15 7:29am    
thanks a lot ....... you helped me a lot and i appreciate that .....and you explained me all this very well
thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900