I have a simple task for my first lab in my intermediate C programming class. I'm taking an array of 8 doubles from the user, and then 1 additional double. I then check the square of one double from the array plus the square of other doubles in the array to see if they are equivalent to the square of the last input given to the program (the additional double).
My problem is that for some reason, when my two inputs squared equal the additional input squared, my compiler doesn't think so.
Please let me know what I'm doing wrong here; I'm using Codelite with the gnu gdb debugger and gcc compiler.
Sample input: 4 3 3 3 3 3 3 3 5
#include <stdio.h>
#include <math.h>
int main(int argc, char **argv)
{
int input[8];
double inputSquared[8];
int pairs[16];
int i, j, k; //i and j are for loop workers.
double x;
int numPairs = 0;
printf("Welcome to Lab 1.\nEnter 8 integers. After each entry, press the enter button.\n");
printf("---------------------------------------\n");
for(i=0; i<8; i++){
printf("Enter integer %d:", i+1);
scanf("%d", &input[i]);
printf("\n");
}
//printf("Now enter one more integer.\n The sum of the squares of the following o this integer squared.\n");
printf("Enter an integer: ");
scanf("%lf", &x);
for(k = 0; k<8; k++){
inputSquared[k] = pow((double)input[k], 2);
}
for(i = 0; i<8; i++){
for(j = i + 1; j<8-1; j++){ //does not check for pairs reflexively. If 1 is in the array, it does not check 1^2 + 1^2.
printf("%lf, %lf; %lf; %lf, %d \n", inputSquared[i], inputSquared[j], pow(x, 2.0), inputSquared[i] + inputSquared[j], ((inputSquared[i] + inputSquared[j]) == ((pow(x, 2.0)))));
if(inputSquared[i] + inputSquared[j] == pow(x, 2.0)){
pairs[2 * numPairs] = input[i];
pairs[2 * numPairs + 1] = input[j];
numPairs++;
}
}
}
if(numPairs == 1)
printf("\nYou have %d pair:", numPairs); // grammar condition for having 1 pair
else
printf("\nYou have %d pairs:\n", numPairs);
for(i = 0; i < numPairs; i++)
printf("(%d,%d)", pairs[2 * i], pairs[2 * i + 1]);
scanf("%lf", &x);
return 0;
}