My code is working properly for slope=1 but not for other slopes. Its drawing either horizontal or vertical line for slopes other than 1. What is wrong with this code.Any help will be appreciated.
#include <graphics.h>
#include <stdio.h>
#include <math.h>
int main( )
{
int x,y,x1,y1,x2,y2,dx,dy;
float step;
int i,gd,gm;
printf("Enter the value of x1,y1: ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2,y2 : ");
scanf("%f%f",&x2,&y2);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,1);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
}
dx=dx/step;
dy=dy/step;
You've made step a float, but dx and dy are integer. As such, this divide is going to give you a 0 in one of these 2 values. I was under the impression that DDA routines were all integer so having the float in there at all makes me wonder. I'll look deeper at the algorithm and see what else I find.
Here's a routine that uses floats in a way that won't zero the step.
and another for windows.
It seems that you are just accepting one value in the
scanf("%f%f",&x1);
scanf("%f%f",&y1);
statements. Try correcting that and running the code once again.
Related
I am trying to design a fan and make the lines rotate using C language.
This is my code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main(){
int gd=DETECT,gm=0,xr,yr,xr1,yr1,xr2,yr2,x,y,x1,y1,x2,y2;
int x3,y3,i;
float rad;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
printf("x1 and y1:");
scanf("%d %d",&x1,&y1);
printf("x2 and y2:");
scanf("%d %d",&x2,&y2);
printf("x3 and y3:");
scanf("%d %d",&x3,&y3);
printf("\n\n");
setcolor(RED);
line(x,y,x1,y1);
line(x,y,x2,y1);
line(x,y,x3,y3);
rad=toRadians(1);
for(i=0;i<60;i++){
xr=x+((x1-x)*cos(0.017)-(y1-y)*sin(0.017));
yr=y+((x1-x)*sin(0.017)+(y1-y)*cos(0.017));
xr1=x+((x2-x)*cos(0.017)-(y2-y)*sin(0.017));
yr1=y+((x2-x)*sin(0.017)+(y2-y)*cos(0.017));
xr2=x+((x3-x)*cos(0.017)-(y3-y)*sin(0.017));
yr2=y+((x3-x)*sin(0.017)+(y3-y)*cos(0.017));
setcolor(RED);
line(x,y,xr,yr);
line(x,y,xr1,yr1);
line(x,y,xr2,yr2);
x1=xr;
y1=yr;
x2=xr1;
y2=yr1;
x3=xr2;
y3=yr2;
delay(500);
cleardevice();
}
getch();
closegraph();
}
The problem is that the lines don't rotate in a synchronized way. It's like one line is rotating, then after some time the second line starts rotating and so on. Can anyone please help me?
Depending on your compiler, you might see a warning like this:
'=': conversion from 'double' to 'int', possible loss of data
This is pretty much telling you what's going on. You declared your coordinates as int but the trigonometric functions produce double. Very small ones to be precise. int is just not able to capture these small changes and will throw away any digit after the decimal point as soon as you do the assignment to xr etc.
To solve this, simply declare your variables as double. You can then convert these to int before passing them to line(). But you keep your calculations in double precision:
line((int)round(x), (int)round(y), (int)round(xr), (int)round(yr));
...
Another way that circumvents this problem is to increase the angle and calculate the rotated directions from the original values as 3Dave suggested in the comments.
The main take-away message here is: Do not ignore warnings that your compiler gives you.
OK, so my task is to get a single digit from a natural number and sum the square numbers (Using function while, which means no arrays yet :S). For instance I type 123 so sum=1*100+2*10+3*1; However the problem is that the digit could be whatever. My problem is that the power rises with int but its like so - 1, 10, 99, 1000. The problem for me is 99. Also answer is looping but I'll fix it later. Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int N,
number=0,
answer=0,
a=1,
i=0;
printf("Type natural number: ");
scanf("%d",&N);
while(N>i)
{
number=N%10;
N/=10;
a=10;
a=pow(a,i);
answer+=number*number*a;
printf("%d\n", answer);
i++;
}
return 0;
}
Try it the other way around. Don't make the input an integer. Start at the beginning of the stream, get the character, convert it to an int 'number'. Then do
answer = 10 * answer;
answer += (number * number);
This will build up your answer little by little. Note that I am not sure that this is what you are asking for due to your example not seeming to match the code.
Let me know if this is off-base and I will update it.
The problem goes like this:
Write a program that reads the three angles and sides of two triangles and print if they are congruent or not. We do not how many times the user wants to do it.
#include <stdio.h>
#include <conio.h>
int main()
{
float l1,l2,l3,l4,l5,l6;
float a1,a2,a3,a4,a5,a6;
char ans;
int d=1;
while(d<=2)
{
printf("\nIntroduce the sides of triangle %d:",d);
scanf("%f %f %f",&l1,&l2,&l3);
printf("Introduce the angles of triangle %d:",d);
scanf("%f %f %f",&a1,&a2,&a3);
{
if(l1==l4 &&l2==l5 && l3==l6 && a1==a4 && a2==a5 && a3==a6)
printf("\n\tCongruent");
else
printf("\n\tNot congruent");
}
}
getch();
return 0;
}
That's my code but there is a problem in the beggining, because i soon as it ends the angles prompt, the program just finishes and says they are not congruent, without ever asking for triangle number 2, therefore i havent done the "asking is the user wants to do other triangles thing". I know that my code is somewhat wrong, but i dont get where.
All help is appreciatead!
This is because your loop is incomplete. Instead of while(d<=2), I recommend for (int d = 1; d <= 2; ++d).
Is it a requirement that the user enter the vertices of both triangles in the same sequence? If not, then you would need to check WHICH angles match, then check the corresponding sides. Also, is in necessary to validate that the given angles and sides form a valid triangle? This could potentially become a very complicated problem.
You are comparing the sides before you read the data from both triangles. The comparison must be outside the while.
You need to increment d somewhere (anywhere) inside the loop.
You dont need those { } closing the if-else.
You are not checking if the values are acceptable, so let's suppose whoever use this program will only provide correct value, so...
The easiest way to check for congruency would be to check if all the sides on the first triangle has a correspondent side on the second triangle.
The way to do the comparison I'll leave to you as a challenge... You can start as:
if(l1 == l4 && l2 == l5 && l3 == l6) ...
Think how you would solve that in a papper or something, once you have an idea on how to solve, try and implement it. =)
I went and changed and added some things, and now it looks like this:
#include <stdio.h>
#include <conio.h>
int main()
{
float l1,l2,l3,l4,l5,l6;
float a1,a2,a3,a4,a5,a6;
char resp;
printf("\n\t Triangles");
printf("\nBegin?");
while(resp=getchar()=='y')
{
fflush(stdin);
printf("\nIntroduce the sides of the first triangle:");
scanf("%f %f %f",&l1,&l2,&l3);
printf("Introduce the angles of first triangle:");
scanf("%f %f %f",&a1,&a2,&a3);
printf("\nIntroduce the sides of the second triangle:");
scanf("%f %f %f",&l4,&l5,&l6);
printf("Introduce the angles of the second triangle:");
scanf("%f %f %f",&a4,&a5,&a6);
fflush(stdin);
if((l1==l4|| l1==l5 ||l1==l6) && (l2==l4 ||l2==l5 || l2==l6) && (l3==l4 || l3==l5 || l3==l6))
printf("\n\tCongruent");
else
printf("\n\tNot congruent");
printf("\nMore triangles?:");
}
getch();
return 0;
}
It runs pretty good , does everything fine but i was wondering is there any way to do the problem without asking for printf("\nBegin?");, or asking for it is the only way to do it?
If there is another way , do that means that i will have to change mywhile(resp=getchar()=='y')?
#include <stdio.h>
#include <math.h>
int fib();
int scan;
int main() {
scanf("%d", &scan);
printf("%d\n", fib());
scanf("%s");
return 0;
}
int fib() {
return floor((pow(1+sqrt(5)/2, scan)-(-pow(1-sqrt(5)/2, scan)))/sqrt(5));
}
I'm pretty new to programming with C and decided to try and calculate any number in the Fibonacci series. I based it off of my lua script here. I'm at a loss of what I've done wrong, could someone give me some insight?
You have the formula wrong. You want fib to be:
int fib() {
return round((pow((1+sqrt(5))/2, scan)-(-pow((1-sqrt(5))/2, scan)))/sqrt(5));
}
instead. You were missing parenthesis around the 1+sqrt(5) and 1-sqrt(5) terms and were using floor instead of round, which was underestimating the fibonacci numbers in my tests. (This mostly has to do with low precision in the pow function. The seventh fibonacci number, 13, came out to 12.969)
You also probably want to change
scanf("%s");
to
char tmp;
scanf("%c", &tmp);
Since the way you have it incorrectly omits an argument.
Hope this helps!
So here is a program that is supposed to calculate an approximation to the value of pi if you take enough terms into the sum which is mathematically described in the following program and calculates the expression of the root, you get a value that gets closer and closer to the value of pi the more terms you have.
#include <stdio.h>
#include <math.h>
main()
{
int j, terms;
double sum, precision, pi;
printf("How many terms: "); scanf("%d", &terms);
for(j=1;j<=terms;j++)
sum+=1/(j*j);
pi = sqrt(6*sum);
printf("Pi: %lf.\n", pi);
}
But there is something making it go wrong here and I can't quite figure out what.
sum+=1/(j*j);
I thought the mistake might be in that line because all others look fine,thinking at first maybe the computer isn't counting decimals.I'm unsure.But my question is: What is it in this code that makes it malfunction?And how do I fix it?
This performs integer division:
1/(j*j);
try this:
sum+=1.0/(j*j);
If j*j might overflow, do this
sum+=1.0/((double)j*j);