在数组中找到两个等于输入平方的数字

时间:2014-02-15 17:54:51

标签: c

我正在使用visual studio在C中构建一个程序,我知道一个数组长度为8的已定义数组。但是我的代码打印出非常奇怪的结果,我无法跟踪错误。无论如何这里是代码。我很感激任何建议!

#include <stdio.h>
#include <math.h>

int main(void)
{
    int x;
    int arraylist[] = { 4, 7, -4, 3, 8, 2, 9, -3,};
    printf("Enter a number to compare to the array ");
    scanf("%d", &x);
    printf("X is %d\n", x);
    int i;
    for (i = 0; i <= 8; i++)
    {
        for (int j = 0; j <= 8; j++)
        {
            int firstnum = arraylist[i] * arraylist[i];
            int secondnum = arraylist[j] * arraylist[j];
            int total = firstnum + secondnum;
            printf("%d", &total);
            if (firstnum + secondnum == x*x)
            {
                printf("%d", &i);
                printf("%d", &j);
            }
            else
            {
                printf("False");
            }
        }
    }
    return 0;
}

如果我将16设置为x,则会打印出多行数字,并且这些数字不符合参数。

OUTPUT == 32 65 32 25 80 20 97 25 65 98 65 57 113 53 130等等另外30个左右的数字。知道我哪里出错吗?

2 个答案:

答案 0 :(得分:0)

printf只是传递了你想要打印的变量,而不是它们的地址,所以:

        printf("%d ", total); /* not &total */
        if (firstnum + secondnum == x*x)
        {
            printf("[ %d ", i); /* not &i */
            printf("%d ]\n", j); /* not &j */
        }
        else
        {
            printf("False\n");
        }

我还添加了一些空格和方括号以及\n,因此打印的数字不会互相碰撞,您可以看到结果。

您可以将printf ij这两个 printf("[ %d %d ]\n", i, j); 组合在一起:

arraylist[]

另外(我不知道这是否是故意的 - 我假设没有),你在for的数组初始化中有一个额外的逗号,这意味着最后有一个结尾0,你正在使用这个在<=7循环中 - 如果您想要8个以0开头的项目,它应该计为<=8而不是#include <stdio.h> #include <math.h> int main (void) { int x; int arraylist[] = { 4, 7, -4, 3, 8, 2, 9, -3 }; printf ("Enter a number to compare to the array "); scanf ("%d", &x); printf ("X is %d\n", x); int i; for (i = 0; i <= 7; i++) { int j; for (j = 0; j <= 7; j++) { int firstnum = arraylist[i] * arraylist[i]; int secondnum = arraylist[j] * arraylist[j]; int total = firstnum + secondnum; printf ("%d ", total); if (firstnum + secondnum == x * x) { printf ("[%d ", i); printf ("%d] ", j); } else { printf ("False "); } printf ("\n"); } } return 0; }

根据上述内容,您的程序略有改变:

Enter a number to compare to the array 3
X is 3
32 False
65 False
32 False
25 False
80 False
20 False
97 False
25 False
65 False
98 False
65 False
58 False
113 False
53 False
130 False
58 False
32 False
65 False
32 False
25 False
80 False
20 False
97 False
25 False
25 False
58 False
25 False
18 False
73 False
13 False
90 False
18 False
80 False
113 False
80 False
73 False
128 False
68 False
145 False
73 False
20 False
53 False
20 False
13 False
68 False
8 False
85 False
13 False
97 False
130 False
97 False
90 False
145 False
85 False
162 False
90 False
25 False
58 False
25 False
18 False
73 False
13 False
90 False
18 False

这会产生以下输出:

{{1}}

那些似乎是狂野的输出,但这些确实是各种方块的总和。例如,它开始32 = 4 ^ 2 + 4 ^ 2,65 = 4 ^ 2 + 7 ^ 2,32 = 4 ^ 2 +( - 4)^ 2等。

答案 1 :(得分:0)

可以使用0到N-1之间的索引访问N个条目的数组。

在您的情况下,您有一个包含8个条目的数组,并且您尝试使用0-8而不是0-7之间的索引来访问它。

简而言之,将<= 8更改为< 8

并且为了使其对任何给定大小都是通用的,请使用sizeof(arraylist)/sizeof(*arraylist)而不是8。

此外,您正在打印变量ijtotal的地址,而不是其值。

所以你需要摆脱&