Finding if two triangle are congruent - c

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')?

Related

Real numbers x and y are given. We need to determine whether or not a point with coordinates (x; y) belong to the shaded area

Real numbers x and y are given. We need to determine whether or not a point with coordinates (x; y) belongs to the shaded area.(Using only C programming language).
I'm a beginner in C programming. I have no idea how to solve this problem. But want so much learn this. Please explain to me.
well lets see if my math doesn't fail me
to define such a thing the equation would be something along the lines of:
1<Y<3, -5<=X<=5
Y<3/4X+1, 0<=X<=4
Y<4/3X+1, -4<=X<=0
Y<3X+1, -5<=X<=-4
Y<1/3X+1, 4<=X<=5
This are a bunch of equations that basically set the area with this you can make a really ineffective if and else() we could use a bit of math and if you had asked me this like a year ago i would still have in mind the analitics class unfortunetely thats long gone, but i have one last card up my sleeve and thats wolfram alpha i putted up the equations it pasted the following(except the 1<Y<3 as that caused a small error)
(Y<3/4X+1, 0<=X<=4) and(Y<4/3X+1, -4<=X<=0) and (Y<3X+1, -5<=X<=-4) and (Y<1/3X+1, 4<=X<=5)
Welp unfortunately it outputted the same so were gonna go into the code now we just need to create a function i will try to find a better "formula compression" tomorrow
Also since you are new i will have to explain some things || is a logical or while && is a logical and this has to do with math i chose double instead of float because the problem is mostly a math problem so i chose some precision although the input is an int because im lazy ive already answered on how to protect the scanf from wrong input requests at Is there a way of limiting scanf in C?
so if you need this code to be well rounded and user proof read above
in C true is any number that is not zero meaning while(1) or while(133) make infinite loops since they will always be true
%d in the scanf are the format modifier to basically tell C "im waiting for an integer"
int IsItInside(double x, double y){
if (y>3 || y<1 ){// i dont need to "range" this since the its always like this
return 0;
}
if (y<(((3*x)/4)+1)&& x>=0 && x<=4){
return 1;
}
if (y<((4/(3*x))+1) && x>=-4 && x<=0){
return 1;
}
if (y<((3*x)+1) && x>=-5 && x<=-4){
return 1;
}
if (y<((1/3*x)+1) && x>=4 && x<=5){
return 1;
}
return 0;
}
int main(int argc, char const *argv[]){
int x=0, y=0;
printf("Enter x: ");
scanf ("%d", &x);
printf("Enter y: ");
scanf ("%d", &y);
if(IsItInside(x,y)){
printf("It is inside");
}else{
printf("It is outside");
}
return 0;
}

Why does my C program keep giving me 1 as the error?

I ran it, and everything seems to be fine--except that it keeps giving me a margin of error of 1. Why is it doing this?
The program is supposed to prompt the user to input an estimation for the cube root of 3, and it uses Newton's method of approximation to show how many attempts it took to get to the approximation. After 500 attempts or a margin of error less than 0.000001, it's supposed to exit the loop. Why, though, doesn't the margin of error change?
Here's my code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
float a, i, e; //declare float variables
printf("Consider the function f(x) = x^3 - 3 = 0.\n");
printf("Simplifying, we get x^3 = 3.\n");
printf("Simplifying it further, we get x = 3^(1/3).\n");
printf("Enter your estimate of the root: ");
scanf("%f", &a); //prompt user to guestimate
printf("So you're saying that x = %f.\n", a);
i=0; //initiate attempt counter
e=abs((a-pow(3, (1/3)))/pow(3, (1/3))); //margin of error formula
while (e>=0.000001 && i<=500) //initiate while loop with above expressions
{
if (a!=pow(3, (1/3)))
{
printf("Attempt %f: ", i);
a = a - (pow(a, 3) - 3)/(3*pow(a, 2));
printf("%f, ", a);
printf("%f margin of error\n", e);
i=i+1;
}
else
break;
}
}
abs() deals with ints and will return an int, you need fabsf().
In the same way, pow() is for doubles, you should use powf().
Another mistake is writing 1/3 and expecting 0.333... as a result. 1 and 3 are int literals, so the operation performed is integer division. You need to use float literals, such as 1.0f/3.0f.
That's it for type compatibility. I can see another error however : you expect e to somehow remember its formula and reapply it automagically. That's not how imperative languages work : when you write e = something, "something" is calculated and stored in eĀ once and for all. You're doing it correctly for a, now just bring e=abs(...); inside the while loop to update it each time.

DDA algorithm not drawings lines for some coordinates?

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.

C if-statement issues?

I'm trying to practice making if-statements and am having very little luck with this. Right now I'm trying to make a trigonometric calculator, a very simple one, using if statements, but I can't get it working. The actual problem occurs after input the trig function (sine, cosine, tangent). This is what happens.
1. I compile it
2. It outputs the user prompt
3. I input the function and hit enter
4. The program jumps to a new blank line
5. Nothing happens and the program closes if I press enter
Here is the code itself. Please be kind if I've done something monumentally stupid, I'm pretty new to C.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(void)
{
float x;
float a, o, h;
float sine, cosine, tangent;
printf("Enter the trig function you wish to calculate: ");
scanf("%f", &x);
if (x == sine)
{ printf("Enter the value of the opposite leg: ");
scanf("%f", &o);
printf("Enter the value of the hypotenuse: ");
scanf("%f", &h);
sine = o / h;
printf("The sine is equal to %f", sine);
}
if (x == cosine)
{ printf("Enter the value of the adjacent leg: ");
scanf("%f", &a);
printf("Enter the value of the hypotenuse: ");
scanf("%f", &h);
cosine = a / h;
printf("The cosine is equal to %f", cosine);
}
if (x == tangent)
{ printf("Enter the value of the opposite leg: ");
scanf("%f", &o);
printf("Enter the value of the adjacent leg: ");
scanf("%f", &a);
tangent = o / a;
printf("The tangent is equal to %f", tangent);
}
getch();
}
Thank you to everyone who who was actually helpful and wasn't rude about my lack of understanding, I didn't realize I had to add a numerical character instead of just a character.
Simple (minimal) fixes
Yes, you're on the verge of doing various things that an experienced programmer would call silly, but they're the sorts of mistakes that novices make (you're neither the first nor the last to make them).
int main(void)
{
float x;
float a, o, h;
float sine, cosine, tangent;
printf("Enter the trig function you wish to calculate: ");
scanf("%f", &x);
if (x == sine)
The primary problem is that you've not given sine, cosine or tangent values, so you've no idea what to enter to make the equality work.
The secondary problem is that comparing floating point numbers for equality is not a good idea.
You'd probably do best with something like:
int main(void)
{
int x;
float a, o, h;
enum { sine, cosine, tangent };
printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
scanf("%d", &x);
if (x == sine)
This is more or less orthodox, and reading and comparing integers for equality is reliable. You would have to change the actions since I've pre-empted the names sine, cosine, and tangent as enumeration (integer) constants. You could work around that by using upper-case names for the constants (that's pretty orthodox), or using a prefix for the names, or ...
int main(void)
{
int x;
float a, o, h;
float sine, cosine, tangent;
enum { SINE, COSINE, TANGENT };
printf("Enter the trig function (0 = sine, 1 = cosine, 2 = tangent): ");
scanf("%d", &x);
if (x == SINE)
Friendlier input
As you might gather from the comments below, it would be better to allow the user to enter the name of the function they'd like to enter instead of making them enter a coded number. That's a little trickier to code up reliably, which is the main reason why I left the answer above using numbers.
#include <stdio.h>
#include <string.h>
int main(void)
{
char line[4096];
printf("Enter trig function you wish to calculate: ");
if (fgets(line, sizeof(line), stdin) != 0)
{
char *nl = strchr(line, '\n');
if (nl != 0)
*nl = '\0';
if (strcmp(line, "sine") == 0)
{
/* Process sine */
}
else if (strcmp(line, "cosine") == 0)
{
/* Process cosine */
}
else if (strcmp(line, "tangent") == 0)
{
/* Process tangent */
}
else
{
fprintf(stderr, "Unrecognized trig function (%s)\n", line);
}
}
}
The 4096 is simply a big round number that's so long that it is very unlikely that anyone will ever enter a line that is longer than that. If they do enter such a line, then GIGO and they get what they deserve (which will be a polite error message that the name they entered was not recognized).
This is still not wonderful code. It might be reasonable to strip leading and trailing white space, and maybe case-convert the input to lower case, and one of the messages should probably identify the valid function names. It would be possible to have the code loop repeatedly, but then you'd need a function to prompt and read the response, etc. All of which adds usability at the expense of more code which complicates things unnecessarily for a beginning programmer.
Your if statements aren't working because you're comparing the input value, x, with a floating point value that hasn't been set. I think what you want to do is this:
int x;
printf("Enter the trig function you wish to calculate\n");
printf("1=sine, 2=cosine, 3=tangent: ");
scanf("%d", &x);
if (x == 1)
{
// do sine
}
else if (x == 2)
{
// do cosine
}
else if (x == 3)
{
// do tangent
}
else
{
printf("I don't know that function.\n");
}
Monumentally stupid? Naw. It's an easy kind of mistake to make when you first start programming. Stick with it. We've all been there.
I'm not sure what you are entering into the program to begin with, but this is where your error is. If you are entering a character array (a "string"), and that is being passed to x, you can't compare that with a floating point value. Also, your sine, cosine, and tangent variables have no value/have not been assigned anything. To solve your issue, assign your variables a number, such as float sine = 1; and make sure what you enter into the command line to pass to x is a number. If you want to enter "cosine", and pass that value to x, then you will have to change your x variable to a char array, such as char[] x = "", and then change your sine, cosine, and tangent variables to be character arrays as well. If you do change your variables to arrays, remember to remove the & from the scanf statement, like this -> scanf("%s", x);.
Right now, this code:
float x;
float a, o, h;
float sine, cosine, tangent;
printf("Enter the trig function you wish to calculate: ");
scanf("%f", &x);
if(x == sine)
...reads a value into x, but then compares it to the current value of sine. Unfortunately, you haven't initialized sine, so it's comparing to some unknown, semi-random value.
When you compare to cosine and tangent, you're doing more of the same.
None of these comparisons has a meaningful result (e.g., they might easily all be true).
At a guess, you probably want to have the user enter a string, and compare that to the values "sine", "cosine", and "tangent", using strcmp.

simplify complex roots

i have made a program to compute roots of quauation but it does not simplify the roots.can anyone help me to simplify them
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int a,b,c;
float d,d2;
printf(" Enter a,b and c:");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("(%d+i%d)/%d\n",-b,sqrt(-d),2*a) ;
printf("(%d-i%d)/%d\n",-b,sqrt(-d),2*a);
}
else
{
printf("(%d+%d)/%d\n",-b,sqrt(d),2*a);
printf("(%d-%d)/%d\n",-b,sqrt(d),2*a);
}
getch();
}
You can't compute the square root of a negative number. d is negative and you're trying to find its square root. The whole point of complex solutions and the imaginary unit i is to write -1 as i^2, and then when d < 0 you have:
sqrt(d) = sqrt(i^2 * (-d)) = i*sqrt(-d)
So change to this:
if(d<0)
{
printf("(%d+i%lf)/%d",-b,sqrt(-d),2*a);
printf("(%d-i%lf)/%d",-b,sqrt(-d),2*a);
}
I don't know why you had parantheses around your printf arguments, I removed those.
The second %d should also be changed to %lf since sqrt returns a double.
If you want to compute square roots tof negative numbers, find a C99 compiler (basically, anything besides MSVC will do), include <complex.h> header, use complex data type and csqrt function.
http://en.wikipedia.org/wiki/Complex.h

Resources