Loop Help !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "stdafx.h"
#include <math.h>
#include<stdio.h>

/*My Program Will Take Two Inputs At a Time And Calculate The Length Of side And Will Add it up To the existing Perimeter And It Will FInish(SENTINEL) whenever any of the value if Negative*/
void reader(double *r1, double *q1, double *r2, double *q2); /*Prototype of Function responsible for taking the inputs from user*/
double length(double,double,double,double);                  /*Prototype of Function accountable for the calculation of length*/
double total(double,double);                                 /*Prototype of Function to keep on adding all the perimeters together*/
void format(double,double);                                  /*Protoype of format descryptor*/

int main(void)
{	double r1=0,r2=0; /*Declaration of First pair of Point where r1 is distance from origin and q1 is angle from an arbitary axis*/
	double q1=0,q2=0; /*Declaration of second pair of Point*/
	double d;         /*variable which will hold the ongoing perimeter for a particular points in loop*/
	double t=0;       /*Initialised variable which holds the total perimeter calculated in the program*/
   do {                 
        reader(&r1,&q1,&r2,&q2);  /*calling the function to get the inputs from user*/
		d = length(r1,q1,r2,q2);  /*Calling the function to calculate the length of side using the points given by user*/
		
		t = total(t,d);           /*Calling the function to add up the existing calculated perimeter to existing*/
		
		format(d,t);              /*Calling the function to print the values to 6 decimal places*/
		
	} while (0 < r1 && 0 < r2 && 0 < q1 && 0 < q2);/*SENTINEL - whenever any of the number is negative the loop will finish*/
	printf("INVALID INPUTS THANKS FOR USING THE PROGRAM");
}








/*FUNCTIONS*/

/*Function to Read the Polar Coordinates*/
void reader(double *r1, double *q1, double *r2, double *q2)
{
	
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis >");
	scanf("%lf %lf",r1,q1);
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis for next point >");
	scanf("%lf %lf",r2,q2);

}

/*Function to Calculate the Length of a side of Polygon*/
double length(double r1,double q1,double r2,double q2)
{
	double d1,d2;
	d1 = (r1*r1)+(r2*r2)-(2*r1*r2)*(cos((q2-q1)*(0.0175)));
	d2 = sqrt(d1);
	
	return(d2);
}

/*Function to calculate the total Perimeter of polygon*/
double total(double t,double d)
{
	double a;
	a = t + d;
	return(a);

}

/*Function to print values format descriptor*/
void format(double d, double t)
{
	printf("The length of side is %0.6f and the partially calculated perimeter is %0.6f\n\n\n",d,t);

}

This code is running perfectly ...
the only problem now is that whenever a negative value goes in...it exits the loop and finishes but it calculates the perimeter with negative values too and adds to the answer i dont want the negative value calculations to be added up.. ! im not getting how to modify the loop to first validate the values inputed and then do the calculations..
thanks for your time all !
I would classify myself as a beginner as well, but I think I know what your problem is.

Your function calls are inside you loop, so they will run before the program gets to the sentinel.
You should get your values assigned to r1, r2, q1, and q2 before the loop. I would move the reader function out of the loop and make the loop a while loop instead of a do while loop.

Make sense?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "stdafx.h"
#include <math.h>
#include<stdio.h>

/*My Program Will Take Two Inputs At a Time And Calculate The Length Of side And Will Add it up To the existing Perimeter And It Will FInish(SENTINEL) whenever any of the value if Negative*/
void reader(double *r1, double *q1, double *r2, double *q2); /*Prototype of Function responsible for taking the inputs from user*/
double length(double,double,double,double);                  /*Prototype of Function accountable for the calculation of length*/
double total(double,double);                                 /*Prototype of Function to keep on adding all the perimeters together*/
void format(double,double);                                  /*Protoype of format descryptor*/

int main(void)
{	double r1=0,r2=0; /*Declaration of First pair of Point where r1 is distance from origin and q1 is angle from an arbitary axis*/
	double q1=0,q2=0; /*Declaration of second pair of Point*/
	double d;         /*variable which will hold the ongoing perimeter for a particular points in loop*/
	double t=0;       /*Initialised variable which holds the total perimeter calculated in the program*/
   do {                 
        reader(&r1,&q1,&r2,&q2); /*calling the function to get the inputs from user*/
		
		if( r1 < 0 || q1 < 0 || r2 < 0 || q2 < 0) break;
		
		d = length(r1,q1,r2,q2);  /*Calling the function to calculate the length of side using the points given by user*/
		
		t = total(t,d);           /*Calling the function to add up the existing calculated perimeter to existing*/
		
		format(d,t);              /*Calling the function to print the values to 6 decimal places*/
		
	} while (0 <= r1 && 0 <= r2 && 0 <= q1 && 0 <= q2);/*SENTINEL - whenever any of the number is negative the loop will finish*/
	printf("INVALID INPUTS THANKS FOR USING THE PROGRAM");
	printf("The Total Perimeter for the inputs are %.6lf",t);
}








/*FUNCTIONS*/

/*Function to Read the Polar Coordinates*/
void reader(double *r1, double *q1, double *r2, double *q2)
{
	
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis >");
	scanf("%lf %lf",r1,q1);
	printf("Please Enter the Distance From the origin and the Angle from an arbitrary axis for next point >");
	scanf("%lf %lf",r2,q2);

}

/*Function to Calculate the Length of a side of Polygon*/
double length(double r1,double q1,double r2,double q2)
{
	double d1,d2;
	d1 = (r1*r1)+(r2*r2)-(2*r1*r2)*(cos((q2-q1)*(0.0175)));
	d2 = sqrt(d1);
	
	return(d2);
}

/*Function to calculate the total Perimeter of polygon*/
double total(double t,double d)
{
	double a;
	a = t + d;
	return(a);

}

/*Function to print values format descriptor*/
void format(double d, double t)
{
	printf("The length of side is %0.6f and the partially calculated perimeter is %0.6f\n\n\n",d,t);

}


yup buddy i wasn't getting how to do it...
but after posting here...i got this in my head.....and its working !
its same what you said ! :)
Topic archived. No new replies allowed.