Error in my code
Run Time Check Failure #3 - T
I tried many times to fix it,
but I failed.
I added pointer to x, y,
but "Run Time Check Failure #3 - T" — same error.
Can you help me to fix this error?
#include<stdio.h>
#include<math.h>
typedef struct {
double x, y;
}location;
double dist(location a,location b)
{
return sqrt(pow(b.x - a.x, 2.0) + pow(b.y -a.y, 2.0));
}
void func(location l, location e)
{
double z;
location a = l;
location b = e;
printf("enter two dots:");
scanf("%lf %lf", a.x, a.y);
printf("enter two dots:");
scanf("%1",a, b);
printf("%.2lf", z);
}
void main()
{
location l;
location e;
func(l, e);
}
The problems in the code were these:
1) scanf variable args must be passed as pointers. See scanf changes below.
2) initialise your variables in struct - thats the Run Time Check Failure #3 warning. see location initialisation below.
I also simplified a little. Hope that helps.
#include<stdio.h>
#include<math.h>
typedef struct {
double x, y;
}location;
double dist(location a, location b)
{
return sqrt((b.x - a.x) * (b.x - a.x) + (b.y - a.y) * (b.y - a.y));
}
void main()
{
location start = { 0 };
location end = { 0 };
printf("Enter start x, y co-ordinates: ");
scanf("%lf %lf", &start.x, &start.y);
printf("Enter end x, y co-ordinates: ");
scanf("%lf %lf", &end.x, &end.y);
printf("The distance between start and end: %lf\n", dist(start, end));
}
Related
I am trying to figure out how do I get previous position from the function that I wrote. I tried to solve it for hours but couldn't make it. Enemy's(N) initial position is (0,0) and every x and y values after that generate randomly. 'O' is my base and it is located in (6,6). The borders are 11x11. The problem is refresh_position function always calculate displacement by getting previus position as '0'. Here is the code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void track_machine();
void refresh_position(int *X, int *Y, double *D, double *R);
void main()
{
track_machine();
}
void track_machine()
{
int x=0, y=0;
char op;
double D, R;
refresh_position(&x, &y, &D, &R);
for(int i=1; i<=11; i++)
{
for(int j=1; j<=11; j++)
{
if(i==x && j==y)
printf("N ");
else if(i==6 && j==6)
printf("O ");
else
printf(". ");
}
printf("\n");
}
printf("Enemies X position:%d\nY position:%d\nDisplacement:%f\nDistance to our camp:%f\nCommand waiting...:\n", x, y, D, R);
scanf(" %c", &op);
if(op=='R')
track_machine();
else if(op=='E')
main();
else
printf("\nERROR!\n");
}
void refresh_position(int *X, int *Y, double *D, double *R)
{
int x=*X, y=*Y, xD, yD, xR, yR;
srand(time(0));
while( (*X==0 || *X==6) || (*Y==0 || *Y==6) )
{
*X = rand()%11;
*Y = rand()%11;
}
xD=abs(*X-x);
yD=abs(*Y-y);
xR=abs(*X-6);
yR=abs(*Y-6);
*D = sqrt( (xD*xD) + (yD*yD) );
*R = sqrt( (xR*xR) + (yR*yR) );
}
I think, this is a kind of board game and board positions are numbered from 1 to 11. What I deduce from the while loop is, you do not want *X & *Y be '0' or '6'. However, using mod(11) w/o adding (increment) '1' will never produce a random result '11'. So, you'd better change that loop into a more efficient do-while, like;
do {
*X = rand() % 11 + 1;
*Y = rand() % 11 + 1;
} while((*X == 6) && (*Y == 6));
This way, rand() % 11 will produce a number b/w 0 - 10 (inclusive), then adding '1' will produce a number b/w 1 - 11 (inclusive). You will get rid of checking for '0' values.
Addressing your question about the code (tough not sure whether I've understood your question 100%); I think the problem arise from the function track_machine having no parameters. Initializing x & y in main and then passing those variables to the function via parameters will solve your issue (of course, if I've understood your question truly).
// main should be
int main()
{
int x = 1, y = 1;
track_machine(x, y);
}
// function decoration should be
void track_machine(int x, int y) {...}
// and your recursive call line should read as
if (op == 'R')
track_machine(x, y);
I didn't describe my problem good enough. Sorry for that but I found what I did wrong. The code prints 11 dots in both x and y direction. My goal was finding the distance between N (which is located randomly each 'R' command in 11x11 area) and O (which is my base located in 6,6) and finding the displacement every time N changes position when I give 'R' command. Here is the working code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
void track_machine();
void refresh_position(int *X, int *Y, double *D, double *R)
int main()
{
track_machine();
}
void track_machine()
{
int x=1, y=1;
char op;
double D, R;
srand(time(0));
D = 0;
R = 8.49;
while(1==1){
for(int i=1; i<=11; i++){
for(int j=1; j<=11; j++){
if(i==x && j==y)
printf("N ");
else if(i==6 && j==6)
printf("O ");
else
printf(". ");
}
printf("\n");
}
printf("Enemies X position:%d\nY position:%d\nDisplacement:%f\nDistance to our camp:%f\nCommand waiting...:\n", x, y, D, R);
scanf(" %c", &op);
if(op=='R')
refresh_position(&x, &y, &D, &R);
else if(op=='E'){
main();
break;
}
else
printf("\nERROR!\n");
}
}
void refresh_position(int *X, int *Y, double *D, double *R)
{
int x=*X, y=*Y;
do {
*X = rand()%11+1;
*Y = rand()%11+1;
} while((*X == 6) || (*Y == 6));
*D = sqrt( ((*X-x)*(*X-x)) + ((*Y-y)*(*Y-y)) );
*R = sqrt( ((*X-6)*(*X-6)) + ((*Y-6)*(*Y-6)) );
}
I can't use parameters for track_machine because I'm not allowed to :). Every time I give 'R' it refreshes the location of 'N' and calculates new 'D' and 'R' values as long as I don't give 'E' command. Actually it is not the whole code this is just a small part of it. When it returns to main it asks for 'op' to choose other functions in switch case. I hope I explained it enough this time. Thanks for help though!
I have to use prototype poly float to compute f(x)=5x^2+12.55x+0.75. I have error every time I run this code because poly is not used. Any help will be good and any tips for prototypes too.
#include<stdio.h>
float poly(float x)
{
return 1;
}
int main()
{
float b, c, a;
printf("Podaj x=");
a = scanf("%f", &b);
c = 5 * b * b + 12.55 * b + 0.75;
if(a<1)
{
printf("Incorrect input");
return 1;
}else
{
printf("Wynik: %.2f", c);
return 0;
}
}
Change poly to:
float poly(float x)
{
return 5*x*x + 12.55*x + .75;
}
In main, you can use the function:
print("poly(%g) = %g.\n", b, poly(b));
I'm fairly new to coding and am currently learning C. In class I was given an assignment to write a program that calculates the hypotenuse of the triangle by using our own functions. However, there seems to be something wrong with the code that I have written.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y, double z);
int main(void) {
double side1, side2, side3, counter;
side3 = 1;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf_s("%d %d", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2, side3));
}
return 0;
}
double hypotenuse(double x, double y, double z) {
x *= x;
y *= y;
z = sqrt(x + y);
return z;
}
My instructor said that we're allowed to use the square root function sqrt of the math library. The main errors that I'm facing are:
1) side3 is not defined (This is why I just arbitrarily set it to 1, but is there some other way to prevent this error from happening?)
2) If I, for example, inputted 3 and 4 as side1 and side2, then side3 should be 5. However, the printed result is an absurdly long number.
Thank you for the help! Any words of advice are appreciated.
You don't need side3 variable - it is not used in calculation. And you function hypotenuse returns the result, so you can directly output the result of sqrt.
I use Ubuntu Linux and write it this way. Please look if you like it.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y) {
double z = sqrt(x * x + y * y);
return z;
}
int main(void) {
double b1, b2, counter;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &b1, &b2);
printf("%.2f\n", hypotenuse(b1, b2));
}
return 0;
}
Test
$ ./a.out
Enter values for two sides: 1 1.73
2.00
OP's code has some problems:
Key problem: Code should have generated a compiler warning as scanf() is directed to treat &side1 as an int *. Turn on all compiler warnings to save you time. Code used "%d" rather than the matching "%lf" to read a double. Also the return value should be checked to validate input.
double side1, side2, side3, counter;
...
// scanf_s("%d %d", &side1, &side2);
if (scanf_s("%lf %lf", &side1, &side2) != 2) puts("Input error");
size3 is not needed. Call hypotenuse() with 2 arguments. #ghostprgmr
// printf("%.2f\n", hypotenuse(side1, side2, side3));
printf("%.2f\n", hypotenuse(side1, side2));
// double hypotenuse(double x, double y, double z) {
double hypotenuse(double x, double y) {
double z = ...
Minor: Code used "%.2f" to print the value of the hypotenuse. This may be OK with select input values to OP's code, but is a poor choice, in general. If input values are vary small like 0.001 and 0.002, the output will print rounded value of 0.00. With very large values, the result will show many non-important digits as OP found with 130899030500194208680850288727868915862901750748094271410143232.00.
For development and debugging, consider using "%e" ,"%g" or "%a" to see a relevant double.
Note that x * x + y * y is prone to over/under flow, even when mathematically sqrt(x * x + y * y) is in the double range. That is one advantage of the standard function hypot(x,y) as it usually handles those edge cases well.
As a reference for anyone viewing this question:
You don't need to write your own function. Standard C provides functions to calculate the hypotnuse:
7.12.7.3 The hypot functions
Synopsis
#include <math.h>
double hypot(double x, double y);
float hypotf(float x, float y);
long double hypotl(long double x, long double y);
Note that you likely need to link with -lm, though that's not listed explicitly in the function documentation in the C standard nor the latest POSIX documentation. It might be documented elsewhere in the standards.
(Link to C11 [draft] standard - likely to be much longer-lived.)
Use correct format specifiers!
Format Specifier for double is not %d! Rest is fine.
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y, double z);
int main(void) {
double side1, side2, side3, counter;
side3 = 1;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2, side3));
}
return 0;
}
double hypotenuse(double x, double y, double z) {
x *= x;
y *= y;
z = sqrt(x + y);
return z;
}
Also you could modify it to this:
#include <stdio.h>
#include <math.h>
double hypotenuse(double x, double y);
int main(void) {
double side1, side2, counter;
for (counter = 0; counter <= 2; counter++) {
printf("Enter values for two sides: ");
scanf("%lf %lf", &side1, &side2);
printf("%.2f\n", hypotenuse(side1, side2));
}
return 0;
}
double hypotenuse(double x, double y) {
x *= x;
y *= y;
return sqrt(x + y);
}
so i want to calculate angle and area of a triangle, but i need to assign the value of input and output using procedure. i cant find any examples about this and already tried some variation, but still have problem with using the pointer.
what i got so far
#include <stdio.h>
#include <math.h>
#define pi 3.141592654
// Declaration
void input(void);//get user input for triangle's sides
void calculate(int* x,int* y,int* z);//calculating area and angle
//main program
int main(void){
int x,y,z;
double a,b,c,height;
input(x,y,z);
calculate(x,y,z);
printf("angle a : %.3f degree\n",a);
printf("angle b : %.3f degree\n",b);
printf("angle c : %.3f degree\n",c);
printf("Area : %.3f cm2\n",height);
return 0;
}
//Definition
void input(int* x, int* y, int* z)
{
printf("insert side x :\n");
scanf("%d",*&x);
printf("insert side y :\n");
scanf("%d",*&y);
printf("insert side z :\n");
scanf("%d",*&z);
}
void calculate(int* x,int* y,int* z)
{
int s
s=(*x + *y + *z)*0.5;
*Area=sqrt(s*(s-x)*(s-y)*(s-z));
*a=acos(((*x * *x)+(*z * *z)-(*y * *y))/2(*x)(*z));
*b=acos(((*y * *y)+(*z * *z)-(*x * *x))/2(*y)(*z));
*c=acos(((*x * *x)+(*y * *y)-(*z * *z))/2(*x)(*y));
}
i got error in scan user input for x,y,z and assign degree and area result to area,a,b,c
Create needed arguments in both declaration and definition.
I don't think using pointers where they are not needed is good.
Your code should be like this:
#include <stdio.h>
#include <math.h>
#define pi 3.141592654
// Declaration
void input(int* x,int* y,int* z);
void calculate(double* a, double* b, double* c, double* Area, int x,int y,int z);
//main program
int main(void){
int x,y,z;
double a,b,c,height; /* It maybe good to rename height to Area */
input(&x,&y,&z);
calculate(&a,&b,&c,&height,x,y,z);
printf("angle a : %.3f degree\n",a);
printf("angle b : %.3f degree\n",b);
printf("angle c : %.3f degree\n",c);
printf("Area : %.3f cm2\n",height);
return 0;
}
//Definition
void input(int* x, int* y, int* z)
{
printf("insert side x :\n");
scanf("%d",x);
printf("insert side y :\n");
scanf("%d",y);
printf("insert side z :\n");
scanf("%d",z);
}
void calculate(double* a, double* b, double* c, double* Area, int x,int y,int z)
{
double s; /* type of s should be double, not int in this case */
s=(x + y + z)*0.5;
*Area=sqrt(s*(s-x)*(s-y)*(s-z));
*a=acos(((x * x)+(z * z)-(y * y))/(2 * x * z));
*b=acos(((y * y)+(z * z)-(x * x))/(2 * y * z));
*c=acos(((x * x)+(y * y)-(z * z))/(2 * x * y));
}
I'm new to C, and quite unfamiliar with writing any program larger than a few lines.
I'm trying to write a model for an object in freefall acted upon by gravity and drag. It uses Eulers method to solve two first order differential equations, one for position and one for velocity.
So we have: F = m dv/dt = -mg - k|v|v and dy/dt = v
These are solved by: Vn+1 = Vn - (delta t*(g+(k/m)|Vn|Vn)) and Yn+1 = Yn + (delta t * Vn)
(In this Vn+1 is the n+1th term etc.)
In my program i've tried to have two functions, for position and velocity, which work by passing pointers with Y and V values between them and the main function, and it should then loop until Y=0 and print off the values at each step.
When I run it it comes up with something like this: http://imgur.com/DNHIhHI
Could anyone tell me either what is wrong with this, or if I need to use a different approach completely?
Many Thanks, Code below
#include <stdio.h>
void Velocity(double *ptr, double m, double k, double t);
void Position(double *pst, double *ptr, double t );
int main()
{
double k = 18833.5608;
double t = 0;
double m;
double speed = 0;
double *ptr = &speed;
double y = 1000;
double *pst = &y;
printf("Enter mass of object: \n");
scanf("%f" , &m);
do
{
Velocity( ptr, m, k, t );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( pst, ptr, t);
printf("Position at time %f is: %f\n" , t , y);
t++;
}
while((y>0));
return 0;
}
void Velocity(double *velo, double m, double k, double t)
{
double g = 9.80665;
*velo = *velo - (t*(g+((k/m)*fabs(*velo)**(velo))));
}
void Position(double *Y , double *velo, double t )
{
*Y = *Y+(t*(*velo));
}
When writing programs that do calculations -- in any language, not just C -- try to make the code that does the computation take arguments and return results but not mutate variables. That is, do not write:
void do_calculation( double * result, double x, double y)
{
*result = x + y;
}
...
double r;
do_calculation(&r, 123, 456);
instead write
double do_calculation(double x, double y)
{
return x + y;
}
...
double r = do_calculation(123, 456);
Make sense?
If you want to modify an existing value, again, don't pass it in as a variable to be mutated. Instead of
void do_calculation(double * accumulator, double x, double y)
{
*accumulator = *accumulator + x + y;
}
...
double r = 10;
do_calculation(&r, 123, 456);
instead say
double do_calculation(double original, double x, double y)
{
return original + x + y;
}
...
double r = 10;
r = do_calculation(r, 123, 456);
Now, once you've got your program architected more sensibly, you need to learn how to debug small programs. Some good advice on that subject can be found here:
http://ericlippert.com/2014/03/05/how-to-debug-small-programs/
A misconcept. I believe you're trying to solve the equations by using small increments of time. Nothing wrong with that, just make the time increment as small as possible, and correct the formulas:
#include <stdio.h>
#include <math.h>
void Velocity(double *velocity, double m, double k, double t)
{
double g = 9.80665;
double velo = *(velocity);
velo = velo - (t*(g+((k/m)*abs(velo)*(velo))));
*(velocity)=velo;
}
void Position(double *position , double *velocity, double t )
{
double Y = *(position);
double velo = *(velocity);
Y = Y+(t*(velo));
*(position)=Y;
}
int main()
{
double k = 18833.5608;
double t = 0;
double dt = 0.001; //making a small increment of time
double m=100;
double speed = 0;
double y = 1000;
//printf("Enter mass of object: \n");
//scanf("%f" , &m);
do
{
Velocity( &speed, m, k, dt );
printf("Velocity at time %f is: %f\n" , t, speed);
Position( &y, &speed, dt);
printf("Position at time %f is: %f\n" , t , y);
t+=dt; //increment time by delta t
}
while((y>0));
return 0;
}