So I have to program a scalarproduct ((x1*y1)+(x2*y2)) with the use of structs and scanf.
However my program just skips y1 and counts both y1 and x2 as the same number even though I type in completely different numbers??
I did the following:
struct _point2d
{
double x[1]; // this means x1 is x[0] and x2 is x[1]
double y[1];
};
double PscalarProduct( double a, double b, double c, double d )
{
printf("The scalar product ((x1*y1) + (x2*y2)) (whereas x1 = %lf,
y1 = %lf, x2 = %lf, y2 = %lf) is %lf\n", a, b, c, d, (( a*b ) + ( c*d )) );
}
int main()
{
struct _point2d Vector;
Vector.x[1];
Vector.y[1];
printf("Enter x1 and y1 \n");
scanf("%lf", &(Vector.x[0]));
scanf("%lf", &(Vector.y[0]));
printf("Enter x2 and y2 \n");
scanf("%lf", &(Vector.x[1]));
scanf("%lf", &(Vector.y[1]));
PscalarProduct(Vector.x[0], Vector.y[0], Vector.x[1], Vector.y[1]);
return 0;
}
However if i run the program with the numbers 1[=x1] 2[=y1] 3[=x2] 4[=y2] I receive this text:
The scalar product ((x1*y1) + (x2*y2)) (whereas x1 = 1.000000, y1 = 3.000000, x2 = 3.000000, y2 = 4.000000) is 15.000000
How can this be that y1 and x2 are the same numbers??? y1 should be the number 2.00000.
The struct members x and y arrays can hold only one element each. But you are reading 2 elements as input.
In C, array index ranges from 0 to N-1. Your code has undefined behaviour due to out of bounds access.
Increase the array size:
struct _point2d
{
double x[2]
double y[2];
};
Related
Hello i have homework triangulation.c with two watchovers and boat.
I have to find distance of this boat, and her coordinates.
I have coordinates of x1,y1,x2,y2,alpha,beta i know how to calculate distance between watchtowers, distance to boat but not her coordinates
here is code i have
float to_radians(const int angle);
float get_watchtowers_distance(const int x1, const int y1, const int x2, const int y2);
float get_boat_distance(const float d, const int alpha, const int beta);
int main() {
int x1 = 76;
int y1 = 316;
int x2 = 57;
int y2 = 516;
int alpha = 17;
int beta = 17;
float d = get_watchtowers_distance(x1,y1,x2,y2); //200.90
float b = get_boat_distance(d, alpha, beta); //30.71
float x3 = ? //97.07
float y3 = ? //418.90
printf("%.2f %.2f %.2f", b, x3, y3);
return 1;
}
can you help me with some formula to get x3 and y3 coordinates? thank you
y coordinates from user as double variable and calculating them if points forms a triangle but i cant get correct result.
I think there is a problem about using double variables for example if i put integer variable to x1 , y1 it does not calculate mAB
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<conio.h>
int main()
{
double x1,y1,x2,y2,x3,y3;
printf("Enter x , y coordinates of first vertice!\n");
scanf("%lf",&x1);
scanf("%lf",&y1);
/*
if(isInteger(x1)==0)
{
printf("\ndouble");
}
else
{
printf("\nint");
}
*/
/*********************************************/
printf("Enter x , y coordinates of second vertice!\n");
scanf("%lf",&x2);
scanf("%lf",&y2);
/*********************************************/
printf("Enter x , y coordinates of third vertice!\n");
scanf("%lf",&x3);
scanf("%lf",&y3);
/*********************************************/
/*********************************************/
double mAB = (fabs(x1-x2) / fabs(y1-y2));
double mAC = (fabs(x1-x3) / fabs(y1-y3));
printf("\n mAB %lf", mAB);
printf("\n mAC %lf", mAC);
if(mAB == mAC)
{
printf("These points does not forms a triangle!!!!");
}
else
{
/*
* 1-2 AB
* 1-3 AC
* 2-3 BC
*/
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
/*********************************************/
double distancexAC = (x3 - x1) * (x3 - x1);
double distanceyAC = (y3 - y1) * (y3 - y1);
double distanceAC = csqrt(fabs(distancexAC - distanceyAC));
/*********************************************/
double distancexBC = (x2 - x3) * (x2 - x3);
double distanceyBC = (y2 - y3) * (y2 - y3);
double distanceBC = csqrt(fabs(distancexBC - distanceyBC));
/*********************************************/
printf("\n AB %lf", distanceAB);
printf("\n AC %lf", distanceAC);
printf("\n BC %lf", distanceBC);
double perimeter = distanceAB+distanceAC+distanceBC;
printf("\n Perimeter: %lf", perimeter);
}
getch();
return 0;
}
Test results:
Enter x , y coordinates of first vertice!
1 1
Enter x , y coordinates of second vertice!
2 2
Enter x , y coordinates of third vertice!
3 3
mAB 1.000000
mAC 1.000000These points does not forms a triangle!!!!
Second test
Enter x , y coordinates of first vertice!
3.4 5.6
Enter x , y coordinates of second vertice!
1.2 3.4
Enter x , y coordinates of third vertice!
1.8 9.8
mAB 1.000000
mAC 0.380952
AB 0.000000
AC 3.883298
BC 6.371813
Perimeter: 10.255111
Calculate the distance between points by adding the sum of squares, not subtracting them. fabs() not needed. sqrt() is sufficient. #Alexander Daum.
Even better, use hypot().
The hypot functions compute the square root of the sum of the squares of x and y, without undue overflow or underflow. C11 §7.12.7.3 2
double distancexAB = (x2 - x1) * (x2 - x1);
double distanceyAB = (y2 - y1) * (y2 - y1);
// double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
double distanceAB = sqrt(distancexAB + distanceyAB);
// or even more simple
double distanceAB = hypot(x2 - x1, y2 - y1);
OP uses weak code to detect if slopes are parallel. 2 problems:
fabs() loses the sign of the slope.
OP's method is subject to divide by 0.0.
// Alternative code:
double delta_x12 = x1 - x2;
double delta_y12 = y1 - y2;
double delta_x13 = x1 - x3;
double delta_y13 = y1 - y3;
if (delta_x12*delta_y13 == delta_y12*delta_x13) {
printf("These points do not form a triangle.");
}
If OP wants to calculate the area, code could use Heron's formula and use that to determine if "points do not form a triangle".
double a = hypot(x1 - x2, y1 - y2);
double b = hypot(x2 - x3, y2 - y3);
double c = hypot(x2 - x1, y3 - y1);
double perimeter = a + b + c;
double s /* semi-perimeter */ = perimeter/2;
double area2 = s*(s-a)*(s-b)*(s-c);
// due to small inaccuracies, area2 may be negative.
double area = area2 > 0.0 ? sqrt(area2) : 0.0;
if (area == 0) {
printf("These points do not form a triangle.");
}
Where calculating distances, you have to write:
double distanceAB = sqrt(distancexAB + distanceyAB);
instead of
double distanceAB = csqrt(fabs(distancexAB - distanceyAB));
Because the distance is the hypothenuse and use sqrt instead of csqrt, because csqrt is intended for complex variables.
You don't need fabs, because distancexAB and distanceyAB are squares and so they cannot be negative, and the sum of two positive numbers will be positive as long as no overflow occurs.
Input and Output Format:
The 1st line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the first rectangle.
The 2nd line of the input consists of 4 integers separated by a space that correspond to x, y, l and w of the second rectangle.
Output consists of 4 integers that correspond to x, y, l and w of the Union rectangle.
Sample Input :
3 8 1515
2 6 10 10
Sample Output:
2 6 16 17
here is my code
it gets validated for some test cases and it is not accepted when I submit it.
I'm trying this on an online coding website.
here is my code.
#include<stdio.h>
#include<math.h>
int main()
{
int x1,y1,x2,y2,l1,w1,l2,w2,x3,y3,l3,w3;
scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
scanf("\n%d %d %d %d",&x2,&y2,&l2,&w2);
if(x1<x2)
x3=x1;
else
x3=x2;
if(y1<y2)
y3=y1;
else
y3=y2;
if(x1==x2)
{
if(l1<l2)
w3=l2;
else
w3=l1;
}
if(y1==y2)
{
// printf("inp");
if(w1<w2)
{
w3=w2;
//printf("%d",w3);
}
else
{
w3=w1;
}
}
if(x1<x2)
l3=l2+fabs(x1-x2);
else if(x2<x1)
l3=l1+fabs(x1-x2);
if(y1<y2)
w3=w2+fabs(y1-y2);
else if(y2<y1)
w3=w1+fabs(y1-y2);
printf("%d ",x3);
printf("%d ",y3);
printf("%d ",l3);
printf("%d",w3);
return 0;
}
if anyone have alternative logic then tell me.
#include<stdio.h>
int main()
{
int x1, y1, x2, y2, l1, w1, l2, w2;
scanf("%d %d %d %d",&x1,&y1,&l1,&w1);
scanf("\n%d %d %d %d",&x2,&y2,&l2,&w2);
int min_x = x1 < x2 ? x1 : x2;
int min_y = y1 < y2 ? y1 : y2;
int max_x = (x1+w1) > (x2+w2) ? (x1+w1) : (x2+w2);
int max_y = (y1+l1) > (y2+l2) ? (y1+l1) : (y2+l2);
int max_w = max_x - min_x;
int max_l = max_y - min_y;
printf("%d %d %d %d", min_x, min_y, max_l, max_w);
return 0;
}
This is assuming that your widths are in the x direction and your lengths are in the y direction. If it is the other way, it should not be too hard to change.
I have faced the same problem in the online course...
here is my link to the question.
union of two rectangles.write a program to find the smallest possible rectangle enclosing the 2 given rectangles
and my program which is working and is accepted without any error or "wrong answer"..
#include<stdio.h>
int main() {
int x1, x2, y1, y2, l1, l2, w1, w2, xmax, xmin, ymax, ymin;
scanf(“%d %d %d %d\n”,&x1,&y1,&l1,&w1);
scanf(“%d %d %d %d\n”,&x2,&y2,&l2,&w2);
xmin = x1 < x2 ? x1 : x2;
ymin = y1 < y2 ? y1 : y2;
int b = x1 + l1;
int c = x2 + l2;
xmax = b > c ? b : c;
int d = y1 + w1;
int e = y2 + w2;
ymax = d > e ? d : e;
int l = xmax - xmin;
int w = ymax - ymin;
printf(“%d %d %d %d”,xmin,ymin,l,w);
return 0;
}
I am trying to find distance between two points, but the result is off.
If i type in (1,2) and (4,5) I get
Distance = 1.414214
instead of 4.242640
This happens regardless of what numbers I input.
#include<stdio.h>
#include<math.h>
float distance(float a, float b, float c, float d);
int main()
{
float a,b,c,d,D;
printf("Please enter the first x coordinate. x1= ");
scanf("%f",&a);
printf("Please enter the first x coordinate. y1= ");
scanf("%f",&b);
printf("Please enter the first x coordinate. x2= ");
scanf("%f",&c);
printf("Please enter the first x coordinate. y2= ");
scanf("%f",&d);
D = distance(a,b,c,d);
printf("Distance = %f",D);
return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
float d, D, x, y, X, Y;
x = x1 - x2;
y = y1 - y2;
X = x*x;
Y = y*y;
d = X + Y;
float sqrtf (float d);
return sqrtf(d);
}
You're getting the wrong answer because you're passing the wrong arguments to your function.
D = distance(a,b,c,d);
Based on your input scanning, you are telling it this:
D = distance(x1,y1,x2,y2);
However, your function takes a different order of arguments (x1, x2, y1, y2).
Change that line to this:
D = distance(a,c,b,d);
And you should get the right answer.
Look at the variable declaration inside the main function-
int a,b,c,d,D;
The data type be a float instead of integer.
I just started an intro to computer programming class, I only know 2 weeks worth of programming.
I keep getting the "conflicting types for 'sqrt' " so I made a prototype and I'm still getting the message. I've tried everything.
#include<stdio.h>
#include<math.h>
float distance(float a, float b, float c, float d);
int main()
{
int a,b,c,d,D;
printf("Please enter the first x coordinate. x1= ");
scanf("%f",&a);
printf("Please enter the first x coordinate. y1= ");
scanf("%f",&b);
printf("Please enter the first x coordinate. x2= ");
scanf("%f",&c);
printf("Please enter the first x coordinate. y2= ");
scanf("%f",&d);
D = distance(a,b,c,d);
printf("Distance = %.4f",D);
return 0;
}
float distance(float x1, float x2, float y1, float y2)
{
float d, D, x, y, X, Y;
x = x1 - x2;
y = y1 - y2;
X = x*x;
Y = y*y;
d = X + Y;
float sqrt (float d);
}
This is a function declaration
float sqrt (float d);
if you want to return the result of the function call you need
return sqrt(d);
also, the conflicting type error is due to the fact that the sqrt function prototype is
double sqrt(double x);
there is a float equivalent
float sqrtf(float x);
so perhaps your function should return
return sqrtf(d);
Note: I don't see any benefit in splitting this calculation so much you could just
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));