中級 C プログラミング クラスの最初のラボで、簡単なタスクがあります。ユーザーから 8 つの double の配列を取得し、さらに 1 つの double を取得しています。次に、配列からの 1 つの double の 2 乗と、配列内の他の double の 2 乗をチェックして、それらがプログラムに与えられた最後の入力 (追加の double) の 2 乗と等しいかどうかを確認します。
私の問題は、何らかの理由で、2 つの入力の 2 乗が追加の入力の 2 乗に等しい場合、コンパイラがそうではないと判断することです。
ここで何が間違っているのか教えてください。gnu gdb デバッガーと gcc コンパイラーで Codelite を使用しています。
サンプル入力: 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;
}