How do I make a local variable change the global variable?

Here's the summary:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<cmath>
using namespace std;

double dist=0;

struct Circle {
       Point centre;
       int radius;
       };

double circle_distance(const Circle& circle1,const Circle& circle2)
{
       double a, b, c;
       a=abs(circle1.centre.y-circle1.centre.x);
       b=abs(circle2.centre.y-circle2.centre.x);
       c=pow(a,2)+pow(b,2);
       
       ::dist=sqrt(c);
       
       return dist;
}


what I want to do is to make ::dist=sqrt(c); change the global variable dist, it seems to go back to 0 if I refer to it anywhere outside the function.

Thanks
Post a main() that causes that to occur. There's probably something else...
This is the whole thing:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include<cmath>
using namespace std;

double dist=0;

struct Point {
       int x;
       int y;
       };
       
struct Circle {
       Point centre;
       int radius;
       };
       
void print_circle(const Circle& circle)
{
     cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
     cout<<"The Radius is: "<<circle.radius<<endl;
}

double circle_distance(const Circle& circle1,const Circle& circle2)
{
       double a, b, c;
       a=abs(circle1.centre.y-circle1.centre.x);
       b=abs(circle2.centre.y-circle2.centre.x);
       c=pow(a,2)+pow(b,2);
       
       ::dist=sqrt(c);
       
       return dist;
}

int circle_test(const Circle& circle1, const Circle& circle2)
{
    circle_distance(circle1, circle2);
    
    int x;
    x=abs(circle2.radius-circle1.radius);
    
    if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
    return 0;
    
    if (dist<x)
    return 1;
    
    if (dist>(circle2.radius+circle1.radius))
    return 2;
    
    else return 3;
    
}


int main()
{
    Circle c1;
    Circle c2;
    cout<<"Please enter the centre and radius of circle #1: ";
    cin>>c1.centre.x>>c1.centre.y>>c1.radius;
    cout<<"Please enter the centre and radius of circle #2: ";
    cin>>c2.centre.x>>c2.centre.y>>c2.radius;
    
    cout<<"\nCircle #1:"<<endl;
    print_circle(c1);
    
    cout<<"\nCircle #2:"<<endl;
    print_circle(c2);
    
    switch (circle_test(c1, c2)) {
           case 0:
                cout<<"\nThe circles are identical!"<<endl;
                break;
           case 1:
                cout<<"\nOne circle is completely inside the other!"<<endl;
                break;
           case 2:
                cout<<"\nThe circles are seperate!"<<endl;
                break;
           default:
                   cout<<"\nThe circles intersect!"<<endl;
                   }
                   
    
    
    
    


return 0;    
}


EDIT: When I plug in 0 0 3 for circle 1 and 10 10 5 for circle 2, I should get "the circles are separate" but instead I get that one is inside the other. I'm assuming this is because it is reading that the distance=0.
Last edited on
Hello,
First I think you have write wrong the expression for calculating the distance (x1-x2)^2+(y1-y2)^2 instead of your expression.

Secondly the piece of code you wrote gives dist a value (but because of the error mentioned before) but a wrong one. I don't get why you experienced this kind of problem. I just added a struct Point also.
Ok so I changed it to the way you said, it now reads:

1
2
3
4
5
6
7
8
9
10
11
double circle_distance(const Circle& circle1,const Circle& circle2)
{
       double a, b;
       a=abs((circle1.centre.x-circle2.centre.x));
       b=abs((circle1.centre.y-circle2.centre.y));
       
       ::dist=pow(a,2)+pow(b,2);
       
       
       return dist;
} 


Now when I plug in 0 0 3 and 10 10 5 I get the correct answer: The circles are separate. However, when I plug in 0 0 5 and 4 0 2 I also get: The circles are separate when I should be getting 'The circles intersect', so there must be something that is still wrong...

P.S The rest of the code is exactly the same, I only modified the expression for calculating the distance.
Bah, fuck it. I just submitted the assignment with the error. Hopefully it's a small mistake and the professor won't deduct too many points. I'd still like to know what my mistake was since I'm a computer science major if anyone would care to explain :))

Thanks.
 
circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius


What's that test supposed to be for? Why do you care about equality of circles?

Anyways - don't do stuff with return codes unless it is somehow necessary. In your case, you are testing once for equality and once for overlap. You should have a function that tests for equality and another that tests for overlap - testing both at the same time makes no sense, you either want to know if the circles are equal or if they do overlap, never both.


Your new circle_dist function returns the square of the distance - your old one was correct. Just don't use pow for calculating squares, just write x*x or something. And use a local dist instead of a global one.

Why do you even use a global there? It doesn't make any sense. Just do this:

 
int dist = circle_distance(circle1, circle2);


in your circle_test function.


I guess you got into CS major without previous programming experience?
Last edited on
Yeah I don't really have much of a "background" in programming, but I love it. I feel like my professor is an idiot, the return function and the equality of the circles test were both required - that confused me a lot too.

int dist = circle_distance(circle1, circle2);

This makes a lot more sense to me. Thanks.
Mind posting the version you have now so I can look at it?
This is the final version that I just re-submitted. It works perfectly now thanks to you.

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//header files
#include<iostream>
#include<cmath>
using namespace std;

//defining the two structs
struct Point {
       int x;
       int y;
       };
       
struct Circle {
       Point centre;
       int radius;
       };
//defining the function that will print the radii and centre's of the circles.       
void print_circle(const Circle& circle)
{
     cout<<"The Centre is: ("<<circle.centre.x<<", "<<circle.centre.y<<")"<<endl;
     cout<<"The Radius is: "<<circle.radius<<endl;
}
//defining the function that will calculate the distance between the centre's of
//the two circles.

double circle_distance(const Circle& circle1,const Circle& circle2)
{
       double a, b, c;
       a=abs((circle1.centre.x-circle2.centre.x));
       b=abs((circle1.centre.y-circle2.centre.y));
       
       c=(a*a)+(b*b);
       int dist=sqrt(c);
       
       
       
       return dist;
}

//defining the function that will evaluate whether the circles are the same, intersecting, etc...
int circle_test(const Circle& circle1, const Circle& circle2)
{
    circle_distance(circle1, circle2);
    
    int x;
    int dist = circle_distance(circle1, circle2);
    x=abs(circle2.radius-circle1.radius);
    
    if (circle1.centre.x==circle2.centre.x&&circle1.centre.y==circle2.centre.y&&circle1.radius==circle2.radius)
    return 0;
    
    if (dist<x)
    return 1;
    
    if (dist>(circle2.radius+circle1.radius))
    return 2;
    
    else return 3;
    
}

//begin main function
int main()
{
    //define two circles c1 and c2.
    Circle c1;
    Circle c2;
    //ask user for input and store data
    cout<<"Please enter the centre and radius of circle #1: ";
    cin>>c1.centre.x>>c1.centre.y>>c1.radius;
    cout<<"Please enter the centre and radius of circle #2: ";
    cin>>c2.centre.x>>c2.centre.y>>c2.radius;
    //Call upon the print_circle function to output the data to the user.
    cout<<"\nCircle #1:"<<endl;
    print_circle(c1);
    
    cout<<"\nCircle #2:"<<endl;
    print_circle(c2);
    
    //Call upon the circle_test function and use a switch statement to ouput
    //various information based on what the function returns to the user.
    switch (circle_test(c1, c2)) {
           case 0:
                cout<<"\nThe circles are identical!"<<endl;
                break;
           case 1:
                cout<<"\nOne circle is completely inside the other!"<<endl;
                break;
           case 2:
                cout<<"\nThe circles are seperate!"<<endl;
                break;
           default:
                   cout<<"\nThe circles intersect!"<<endl;
                   }
                   
                   int end;
                   cin>>end;

return 0;    
}
//end main 
Last edited on
Sorry, I didn't see that circle distance returns a double - in that case, you of course have to use a double for the distance. In the circle_distance function you can leave out the declaration of dist - just return the sqrt of c.

In circle test you forgot to remove your old call to circle_dist. Also, use a double here again (sorry again, I somehow assumed you were using ints for the distance... I just realized that wouldn't make too much sense).

Otherwise this should be ok as it is (though don't rely on me for that, I may have missed something).
Last edited on
Yeah thanks a heap for the help, hopefully by the time I reach as many posts as you have - I will have some idea of how to do these assignments myself without too many errors :P
Probably. You couldn't imagine how much I learned just from helping people correct beginner assignments.
Topic archived. No new replies allowed.