找到两个向量的乘积的C程序错误地输出零。为什么?

时间:2013-09-27 02:30:22

标签: c arrays loops vector iteration

我试图迭代地找到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 ;

}

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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

$

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