Reputation: 21
here is the code: used to calculate the other two sides of a right angled triangle when one of the cathetus( sides adjacent to the right angle) is given.
(i am a beginner in C)
int* pythagoreanTriple(int a, int *result_size){
// Complete this function
result_size[0]=a;
int sqa=a*a;
if(sqa%2)
{result_size[1]=(sqa-1)/2;
result_size[2]=result_size[1]++;
}
else{
int m=a/2;
result_size[2]=m*m+1;
result_size[1]=m*m-1;
}
return result_size;
}
int main() {
int a;
scanf("%d", &a);
int result_size;
int* triple = pythagoreanTriple(a, &result_size);
for(int triple_i = 0; triple_i < 3; triple_i++) {
if(triple_i) {
printf(" ");
}
printf("%d", triple[triple_i]);
}
puts("");
return 0;
}
Upvotes: 0
Views: 151
Reputation: 409136
In the main
function the variable result_size
is a single int
. You pass a pointer to that variable to the pythagoreanTriple
function where you treat it as an array of three elements. The pointer could be treated as an array, but only of a single element, which represents the variable in the main
function.
If it should be an array of tree elements, then define it as such.
Upvotes: 2