我正在尝试完成一项任务,要求我编写一个函数a
,其中b
和n
是指向长度为#include<stdio.h>
#define N 10
double inner_product(const double *a, const double *b, int n)
{ int sum=0;
int row= (int)&a;
int column=(int)&b;
const double ar[row][column];
const double *p;
for(p=&ar[0][0]; p<&ar[n][n]; p++)
{sum += *p;}
return sum;
}
int main()
{
const double *a, *b;
int row= (int)&a; int column=(int)&b;
int N;
double x;
const double *p, ar[row][column];
printf("Enter the %d elements of each row and column seperated by a comma:\n",N);
for(p=&ar[0][0];p<&ar[N][N];p++)
{scanf("%d%d",&row, &column);}
x=inner_product(a,b, N);
printf("the inner product of the matrix is: %lf", x);
return 0;
}
的数组的指针。此函数将仅使用指针算法找到定义为$ \ sum_ {i = 0} ^ n i ^ 2 = a_i * b_i $的内积。
以下是我提出的代码。它不编译并给出错误。有人可以帮我修复此错误并建议对此代码进行任何改进。
const headerFormatter = (col, colIndex) => {
return (
<FormattedMessage className="react-bootstrap-table-sort-order dropup" id={col.text} key={colIndex} />
);
}
答案 0 :(得分:0)
正如我在评论中所说,你需要从头开始。这是一个重写的程序来完成这项工作。 assessment = getIntent().getExtras().getParcelable("assessment");
函数只是因为它有调试打印代码(注释掉)和三种可选的算术方法(其中两个也被注释掉),因此适度大。核心非常简单。
inner_product()
打印功能很有用;您可以设计每五个数字输出换行符的变体,或每行输出任意数量的数字。打印测试数据很重要;它确保程序正在处理您期望的数据。
输出(启用调试):
#include <stdio.h>
#define N 10
static double inner_product(const double *a, const double *b, int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
/*printf("%8.3f * %8.3f = %14.6f ", *a, *b, *a * *b);*/
// Alternative ways of computing the sum
// sum += *(a+i) * *(b+i);
// sum += a[i] * b[i];
sum += *a++ * *b++;
/*printf("CS = %15.6f\n", sum);*/
}
return sum;
}
static void print_vector(const char *tag, const double *v, int n)
{
printf("Vector %s (%d):\n", tag, n);
for (int i = 0; i < n; i++)
printf("%9.3f", v[i]);
putchar('\n');
}
int main(void)
{
// random -n 10 -F '%7.3f' -- -1000 1000 | commalist -B 8 -n 5 -W 8
double a[N] =
{
-78.533, 52.153, -632.825, -196.897, 157.031,
-804.630, -787.420, -281.817, -963.641, 561.922,
};
double b[N] =
{
112.700, -83.580, -294.587, 320.394, -8.366,
218.917, 148.115, 421.533, -691.338, -741.578,
};
print_vector("A", a, N);
print_vector("B", b, N);
double x = inner_product(a, b, N);
printf("The inner product of the vectors is: %lf\n", x);
return 0;
}
输出(禁用调试):
Vector A (10):
-78.533 52.153 -632.825 -196.897 157.031 -804.630 -787.420 -281.817 -963.641 561.922
Vector B (10):
112.700 -83.580 -294.587 320.394 -8.366 218.917 148.115 421.533 -691.338 -741.578
-78.533 * 112.700 = -8850.669100 CS = -8850.669100
52.153 * -83.580 = -4358.947740 CS = -13209.616840
-632.825 * -294.587 = 186422.018275 CS = 173212.401435
-196.897 * 320.394 = -63084.617418 CS = 110127.784017
157.031 * -8.366 = -1313.721346 CS = 108814.062671
-804.630 * 218.917 = -176147.185710 CS = -67333.123039
-787.420 * 148.115 = -116628.713300 CS = -183961.836339
-281.817 * 421.533 = -118795.165461 CS = -302757.001800
-963.641 * -691.338 = 666201.641658 CS = 363444.639858
561.922 * -741.578 = -416708.992916 CS = -53264.353058
The inner product of the vectors is: -53264.353058
我通过编写脚本来驱动Vector A (10):
-78.533 52.153 -632.825 -196.897 157.031 -804.630 -787.420 -281.817 -963.641 561.922
Vector B (10):
112.700 -83.580 -294.587 320.394 -8.366 218.917 148.115 421.533 -691.338 -741.578
The inner product of the vectors is: -53264.353058
重复计算来测试它;它给出了相同的答案(在我调试脚本之后)。