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.
Related
Im new to C and I really don't know know what I'm doing wrong.
The issue that I am having is I'm supposed to ask 3 questions of the user using scanf. I'm supposed to ask the user for an integer, a positive real number and a non negative number and then calculate the numbers into XX.XX using %.2f.
//pre-processor directives
#include <stdio.h>
#include <math.h>
//main function
int main()
{
//declare variables
int smp1,smp2, smp3,total;
printf("sample 1?\n"); // positive integer
scanf("%d", &smp1);
printf("sample 2?\n"); //positive real number
scanf("%f",&smp2);
printf("sample 3?\n"); // non negative number
scanf("%u", &smp3);
total = (smp1 + smp2 / smp3);
printf("The final result is %.2f",total);
//end of main
return 0;
}
No matter what I put in there my result ends up being 0.00. It won't even do simple addition and I don't know enough to know why.
Your main issue is that you declare all your variables as ints, but smp2 and total must hold floating point values.
Change your declarations to
int smp1;
double smp2, total;
unsigned int smp3;
This way, the types of the variables match up with the conversion specifiers used in the printf and scanf calls.
Types matter in C, and it's up to you that the types of the arguments in each printf and scanf call match up with the conversion specifiers.
Check your compiler documentation on how to enable warnings (even better, to treat all warnings as errors). Most compilers should warn about type mismatches like this, but sometimes you have to set a flag in order for those warnings to appear.
there's something wrong in these variables.
can someone fix this? my answer keep getting on 0.00
Test case:
we want to find the mean between 3 numbers using struct
input=2,
2 of them are: 3 5 8 and 3 5 7
out put should be:
//*3+5+8=(16)/3=5.33
//*3+5+7=(15)/3=5.00
#include<stdio.h>
struct rata{
float in1;
float in2;
float in3;
};
float rata2(in1,in2,in3){
return (float)((in1+in2+in3)/3);
}
void main(){
int i,n;
char hasil[100];
scanf("%d",&n);
struct rata walao;
for (i=0;i<n;i++){
scanf("%d %d %d",&walao.in1,&walao.in2,&walao.in3);
hasil[i]=rata2(walao.in1,walao.in2,walao.in3);
}
for (i=0;i<n;i++){
printf("%.2f\n",hasil[i]);
}
}
There are 3 errors in your code that is preventing you from getting the correct answer. Can you find them? Here's a hint, they have to do with types.
Below are the answers and the reasons behind them.
char hasil[100] is assigning hasil to be a char array of size 100. While chars can be assigned numerical values, they are to be treated as integers if they are. Floats =/= Integers, and this should be rectified by saying float hasil[100]
scanf("%d %d %d",&walao.in1,&walao.in2,&walao.in3) is scanning for 3 digits. Since floats can also be assigned integer values, this is valid. However, the language requires that all values used in a calculation should be the same type (hint for 3!). To fix you can do 1 of 2 things, both are legal but entirely up to you. You can either write it as scanf("%f %f %f",&walao.in1,&walao.in2,&walao.in3) or keep it as is. Your choice will come to play in final error
The input rata2 is taking is unspecified. Is it ints? floats? chars? It doesn't know but it trys to resort to using ints, as mostly everything in C can be represented by a number. Since it resorts to ints, the value returned by your calculation is an int as well and no late cast to float will change that. Those variables need to be specified as something and how you handled error 2 decides what you do here. If you changed the earlier scanf to take floats instead of digits, rewrite rata as float rata2(float in1, float in2, float in3), remove the cast and you're done. If you kept as is, rewrite rata as float rata2(int in1, int in2, int in3), and rewrite the return as return ((float)in1 + (float)in2 + (float)in3)/3;. Either course of action is acceptable but its far more easier and faster specifying them as floats then trying to cast everything (Plus its a LOT cleaner).
This should rectify your code (tested it on my machine). Also, for future note, do
scanf("%d",&n);float hasil[n];
It makes more sense and you don't have to run into the issue of people specifying memory you don't have access to.
I know this problem has been on the internet for a while but i cant seem to find how to stop my program from rounding the 3rd decimal.
the answer output is 4524.370 and should be 4524.369
also, i know my equations are stupid and could be simplified but im lazy
//Tanner Oelke CSE155E
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void){
double v, t; //base variables
double sq1, sq2; //calculation variable for square root
printf("Please enter the given air temperature in Fahrenheit:");
scanf("%lf", &t);
//unessecary equations but it works
sq1=(57*t+297);
sq2=(sq1/247);
v=1086*sqrt(sq2);
printf("%.3lf\n", v);
return 0;
}
With an input of "70.0" the result is 4524.369754... which displays as "4524.370" - What OP gets.
With an input of "69.999975" the result is 4524.369002... which displays as "4524.369" - what OP wants.
If OP expects "70.0" to result in "4524.369", then some minor adjustment to the formula is needed. The precision of double is at least 10 significant digits and often is 15+. Even doing this in float then f(70.0)--> 4524.370.
Else OP has the wrong expectation.
Response to OP's comment:
"to shorten the decimal place to 3 spots without rounding". Hmmm seems strange to want this:
// properly rounded result
printf("%.3lf\n", v);
// shorten to 3 places without rounding
double v3 = floor(v*1000.0)/1000.0;
printf("%.3lf\n", v3);
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.
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);