1

我试图迭代地找到 2 个向量的内积,但我似乎只收到零。谢谢你的帮助。当 1 被估算为参数时,我将 r 和 c 指定为 1 和 2,并将 r2 和 c2 指定为 1 和 2,我应该得到 5。2 个向量的内积是 a1 * b1 + a2 * b2 + a3 * b3 ...

#include <stdio.h>
#include <stdlib.h>

int inner(int A[], int B[], int n){
  int product = 0;
  int i;

  for(i = 0; i < n; i++){
    product = product + A[i] * B[i];
  }

  return product;
}

int main( int argc, char *argv[] ) {

  int n = atoi(argv[1]);
  int *A, *B;
  int r, c, i, j, r2, c2, product;

  printf("Enter values for r and c for vector A: ");
  scanf("%d %d", &r, &c);
  A = (int *)malloc(n * sizeof(int) * r * c);

  for(i = 0; i < r; i++)
    for(j = 0; j < c; j++)
      A[i*c+j] = i + j;

  printf("\nEnter values for r2 and c2 for vector B: ");
  scanf("%d %d", &r2, &c2);
  B = (int *)malloc(n * sizeof(int) * r2 * c2);

  for(i = 0; i < r2; i++)
    for(j = 0; j < c2; j++)
      A[i*c2+j] = i + j;

  product = inner(A, B, n);

  printf("\nThe inner product of the two vectors is %d\n\n", product);

  return 0 ;

}
4

3 回答 3

3

您有一个错误,您将值放入A而不是B.

于 2013-09-27T02:32:54.110 回答
1

A和B有多大?您为 A 分配 n * c * r 个整数,并将其初始化为二维 c*r 矩阵,因此仅初始化第一个 c*r 元素。剩余的 (n-1)*c*r 元素未初始化。然后计算前 n 个元素的点积。这似乎没有意义。你的意思是内部(A,B,c * r)?

我建议你为 inner() 编写一个更简单的测试用例。

于 2013-09-27T02:56:52.033 回答
1

像这样调试程序的第一条规则是打印关键数据。在这里,最关键的数据项可能是 的值n。您没有说明如何调用程序,但是您n从参数列表 ( int n = atoi(argv[1]);) 中读取,而没有检查是否有参数。您看到的行为与 一致n == 0。因此,您可以通过以下方式帮助自己:

#include <stdio.h>
#include <stdlib.h>

int inner(int A[], int B[], int n)
{
    int product = 0;
    int i;

    printf("n = %d\n", n);
    for (i = 0; i < n; i++)
    {
        printf("P = %2d, A[%d] = %d, B[%d] = %d\n", product, i, A[i], i, B[i]);
        product = product + A[i] * B[i];
    }

    return product;
}

int main( int argc, char *argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s number\n", argv[0]);
        return 1;
    }
    int n = atoi(argv[1]);
    int *A, *B;
    int r1, c1, i, j, r2, c2, product;

    printf("Enter values for r1 and c1 for vector A: ");
    scanf("%d %d", &r1, &c1);
    printf("r1 = %d, c1 = %d\n", r1, c1);
    A = (int *)malloc(n * sizeof(int) * r1 * c1);

    for (i = 0; i < r1; i++)
        for (j = 0; j < c1; j++)
            A[i*c1+j] = i + j;

    printf("\nEnter values for r2 and c2 for vector B: ");
    scanf("%d %d", &r2, &c2);
    printf("r2 = %d, c2 = %d\n", r2, c2);
    B = (int *)malloc(n * sizeof(int) * r2 * c2);

    for (i = 0; i < r2; i++)
        for (j = 0; j < c2; j++)
            B[i*c2+j] = i + j;      // Not A again!

    printf("n = %d\n", n);
    product = inner(A, B, n);

    printf("\nThe inner product of the two vectors is %d\n\n", product);

    return 0;
}

使用bash(所以当程序提示我时我不必继续输入数字),您可以获得示例输出,例如:

$ for i in $(seq 6 9); do echo; ./cp $i <<< "3 4 5 5"; done

Enter values for r1 and c1 for vector A: r1 = 3, c1 = 4

Enter values for r2 and c2 for vector B: r2 = 5, c2 = 5
n = 6
n = 6
P =  0, A[0] = 0, B[0] = 0
P =  0, A[1] = 1, B[1] = 1
P =  1, A[2] = 2, B[2] = 2
P =  5, A[3] = 3, B[3] = 3
P = 14, A[4] = 1, B[4] = 4
P = 18, A[5] = 2, B[5] = 1

The inner product of the two vectors is 20


Enter values for r1 and c1 for vector A: r1 = 3, c1 = 4

Enter values for r2 and c2 for vector B: r2 = 5, c2 = 5
n = 7
n = 7
P =  0, A[0] = 0, B[0] = 0
P =  0, A[1] = 1, B[1] = 1
P =  1, A[2] = 2, B[2] = 2
P =  5, A[3] = 3, B[3] = 3
P = 14, A[4] = 1, B[4] = 4
P = 18, A[5] = 2, B[5] = 1
P = 20, A[6] = 3, B[6] = 2

The inner product of the two vectors is 26


Enter values for r1 and c1 for vector A: r1 = 3, c1 = 4

Enter values for r2 and c2 for vector B: r2 = 5, c2 = 5
n = 8
n = 8
P =  0, A[0] = 0, B[0] = 0
P =  0, A[1] = 1, B[1] = 1
P =  1, A[2] = 2, B[2] = 2
P =  5, A[3] = 3, B[3] = 3
P = 14, A[4] = 1, B[4] = 4
P = 18, A[5] = 2, B[5] = 1
P = 20, A[6] = 3, B[6] = 2
P = 26, A[7] = 4, B[7] = 3

The inner product of the two vectors is 38


Enter values for r1 and c1 for vector A: r1 = 3, c1 = 4

Enter values for r2 and c2 for vector B: r2 = 5, c2 = 5
n = 9
n = 9
P =  0, A[0] = 0, B[0] = 0
P =  0, A[1] = 1, B[1] = 1
P =  1, A[2] = 2, B[2] = 2
P =  5, A[3] = 3, B[3] = 3
P = 14, A[4] = 1, B[4] = 4
P = 18, A[5] = 2, B[5] = 1
P = 20, A[6] = 3, B[6] = 2
P = 26, A[7] = 4, B[7] = 3
P = 38, A[8] = 2, B[8] = 4

The inner product of the two vectors is 46

$

回显输入和中间结果是调试程序的一种基本但强大的技术。

于 2013-09-27T03:55:10.893 回答