I’ve written a small program but the answer it gives is always wrong, here is the code
double radiusc;
double xcenter;
double ycenter;
bool onthecircle(int x,int y);
int main(void)
{
int inputwait;
printf("Please enter x coordinate of your center point: ");
scanf("%d",&xcenter);
printf("Please enter y coordinate of your center point: ");
scanf("%d",&ycenter);
printf("Please enter the radius of the circle: ");
scanf("%d",&radiusc);
double left_x = xcenter - radiusc;
double left_y = ycenter;
double down_x = xcenter;
double down_y = ycenter - radiusc;
if(left_x >= floor(left_x))
{
left_x = floor(left_x);
int (left_x);
}
else
{
left_x = floor(left_x) + 1;
int (left_x);
}
for(left_x; left_x<=xcenter; left_x++)
{
for(left_y;left_y>ycenter-radiusc;left_y--)
{
if(onthecircle(left_x,left_y))
printf("Jest na kole: %d , %d \n", left_x,left_y);
}
}
scanf("%i",&inputwait);
return 0;
}
bool onthecircle(int x,int y)
{
double t1= x - xcenter;
double t2 = x - ycenter;
if ((t1*t1 + t2*t2) > (0,9 *radiusc*radiusc) &&
(t1*t1 + t2*t2) < (1,1 *radiusc*radiusc))
return 1;
else
return 0;
}
When I debug, typing 0,0 as my center and r = 1, it gives me the point 0, and some big number, instead of saving 1 as the value for r, it gets a random huge number, and I have no idea why. Any ideas?
In addition to the problem Ed found,
0,9is not a valid floating-point constant; you want0.9. (Locale settings can affect input and output representations; they don’t affect language syntax.)The comma in
(0,9 *radiusc*radiusc)is a comma operator, not a decimal point; you’re multiplying the square of the radius by 9.And you’re missing the
#include <stdio.h>and#include <math.h>(forfloor()at the top of your program (your compiler might let you get away with that, but it’s not optional).And the format in your
printfcalls is incorrect; you need"%f"or"%g"for `double’.More:
Your use of a
doubleas a loop control variable is questionable. The callscanf("%i",&inputwait);to wait for input before terminating the program without a prompt is user-hostile. There is no need to use global variables. I don’t know whatint (left_x);is supposed to do; I think it’s parsed as a declaration (of a variable that you never use), and the parentheses are superfluous. Your use of the type nameboolmeans either that you’re compiling your code as C++, or that you have either a#include <stdbool.h>or a definition ofboolthat you haven’t bothered to show us.That’s about as much debugging as I’m willing to do for now. Fix these errors and try again.