# Thread: First time using pointers, and i feel lost :|

1. ## First time using pointers, and i feel lost :|

It's me again, trying to figure out how to use pointers,
Im supposed to calculate alpha, beta and the lenght of c of a trianlge using a function (triangle_1)
and from what i think i'll have to use *pointers to get the values out of the function.
i tested the *cc, *alpha and *beta calculations outside a function and they seem to work

Code:
```#include <stdio.h>#include <math.h>
#define TOTAL_DEG  (180.0)

void triangle_1 (double a, double b,double c, double gamma, double *cc, double *alpha, double *beta)
{

*cc= sqrt((a*a+b*b-(2*a*b*cos(gamma))));
*alpha =(gamma)*(acos((double)(b*b +(c)*(c)- a*a)/(2.0*b*c)));
*beta = -gamma-*alpha+TOTAL_DEG;

}

int main()
{
double a,b,c,gamma,alpha,beta,result;
double tri;
printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
scanf("%lf,%lf,%lf",&a,&b,&gamma);
prinf("a,b,c: %lf %lf %lf; gegenueber liegende Winkel: %lf %lf %lf",a,b,c,*alpha,*beta,gamma);

return 0;

}

}```

thanks for the help o/

*edit, im unsure if im missing these to get the rad to degree or the other way around, i always hated trigonometry

2. Recall that given a double variable named alpha, to get a pointer to alpha, you should use the address-of operator &, not the dereference operator, i.e., &alpha

By the way, triangle_1 is a rather poor name for a function. You probably have no choice in the matter, but it would be better design to have three functions:
Code:
```double compute_length_of_third_side(double a, double b, double gamma)
{
return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
}

double compute_alpha(double a, double b, double c, double gamma)
{
return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
}

double compute_beta(double gamma, double alpha)
{
return -gamma - alpha + TOTAL_DEG;
}```
Notice that this reveals a problem with your triangle_1 function: you don't need c because you're computing it, and subsequently your use of c without overwriting it is a bug.

3. Originally Posted by laserlight
Recall that given a double variable named alpha, to get a pointer to alpha, you should use the address-of operator &, not the dereference operator, i.e., &alpha

By the way, triangle_1 is a rather poor name for a function. You probably have no choice in the matter, but it would be better design to have three functions:
Code:
```double compute_length_of_third_side(double a, double b, double gamma)
{
return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
}

double compute_alpha(double a, double b, double c, double gamma)
{
return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
}

double compute_beta(double gamma, double alpha)
{
return -gamma - alpha + TOTAL_DEG;
}```
Notice that this reveals a problem with your triangle_1 function: you don't need c because you're computing it, and subsequently your use of c without overwriting it is a bug.
ye the guideline says "Write a Function (triangle_1) that computes those calculations (prototype is given) void triangle_1 (double a, double b, double gamma, double *c, double *alpha,double *beta);
thought about using multiple functions aswell :/

4. Code:
```#include <stdio.h>#include <math.h>
#include <stdlib.h>
#define TOTAL_DEG  (180.0)

double berechnung_length(double a, double b, double gamma, double c)
{
c = sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
return c;
}

double berechnung_alpha(double a, double b, double c, double gamma, double alpha)
{
alpha = gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));
return alpha;
}

double berechnung_beta(double gamma, double alpha, double beta)
{
beta = - gamma - alpha + TOTAL_DEG;
return beta;
}

//void triangle_1 (double a, double b,double c, double gamma, double *cc, double *alpha, double *beta)
//	{
//
//		*cc= sqrt((a*a+b*b-(2*a*b*cos(gamma))));
//		*alpha =(gamma)*(acos((double)(b*b +(c)*(c)- a*a)/(2.0*b*c)));
//		*beta = -gamma-*alpha+TOTAL_DEG;
//
//	}

int main()
{
double a,b,c,gamma;
double laenge, alpha, beta;

printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
scanf("%lf %lf %lf",&a,&b,&gamma);
laenge = berechnung_length(a,b,gamma, c);
alpha = berechnung_alpha(a,b,c,gamma, alpha);
printf("a = %lf\t b = %lf\t gamma = %lf \t",a,b,gamma);
printf("Daraus folgt c = %lf, alpha = %lf und beta = %lf", laenge, alpha, beta);
return 0;

}```
so i changed it up a bit, left my old weird stuff in with the //'s
seems to kinda be working now, but idk if its my fault but it keeps returning -1.#IND00 or 0.00000 as the calculation
*edit the c calculation is working

5. Don't use function parameters as local variables!
And you never call berechnung_beta.
Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define TOTAL_DEG 180.0

double berechnung_length(double a, double b, double gamma)
{
return sqrt(a * a + b * b - (2 * a * b * cos(gamma)));
}

double berechnung_alpha(double a, double b, double c, double gamma)
{
return gamma * acos((double)(b * b + c * c - a * a) / (2.0 * b * c));;
}

double berechnung_beta(double gamma, double alpha)
{
return -gamma - alpha + TOTAL_DEG;
}

int main()
{
double a, b, gamma;
printf("Bitte gib a, b und gamma eines Dreiecks ein\n");
scanf("%lf %lf %lf",&a,&b,&gamma);

double laenge = berechnung_length(a,b,gamma);
double alpha = berechnung_alpha(a,b,laenge,gamma);
double beta = berechnung_beta(gamma, alpha);

printf("a = %lf\t b = %lf\t gamma = %lf\n",a,b,gamma);
printf("Daraus folgt c = %lf, alpha = %lf und beta = %lf\n",
laenge, alpha, beta);
}```
If it still gives wrong answers, you need to tell us what inputs you are using and what output you expected.

6. What do you want to calculate? Side A, B, C or the degrees alpha, beta, gamma. Two sides and one angle or something. - like in the example below?

Rechner zum Dreieck - Seiten, Höhe, Winkel, Flächeninhalt berechnen

7. im trying to calculate c, alpha and beta, the user should put in a,b and gamma

8. Originally Posted by Lyrockzz
im trying to calculate c, alpha and beta, the user should put in a,b and gamma
The calculation of side C, or the third side of a triangle, or the calculation of the angles alpha and beta requires different calculation types.
If you want to calculate side C, the side whose length you are looking for (also A + B), then here is an example:

Code:
```. . .
#define PI 3.14159

//Deklaration
double dreieck_dritte_seite(double a, double b, double radiant);
. . .

{

}

//The side the being searched - Die Seite die gesucht wird
double dreieck_dritte_seite(double a, double b, double radiant)
{
double y = 2.0;  //Hilfsvariable for pow()
double seite_c;

seite_c = sqrt((pow(a, y) + pow(b, y)) - (2 * a * b * cos(radiant)));
return seite_c;
}```