如何使用标量添加两个向量并将结果保存在两个向量之一中,并从 C 中的函数返回此向量?




我有点被困在这里,遇到了一个小问题:

我为考试而学习,我的任务是编写一个可以执行以下操作的程序:
有两个给定的向量xy它们是R^(n(的元素,每个向量都是长度为n的双精度数组,nN的一个元素。此外,还有一个双变量a作为给定的 R元素。

我需要编写四个函数vec_plusvec_multdotprodnorm2,它们的目的如下:
(到目前为止我的问题只考虑vec_plusvec_mult(

1.vec_plus必须计算 x+ a * y(R^n的元素(并将其保存在x

2 中。vec_mult必须计算一个 *x(R^n的元素(并将其保存在x

3 中。dotprod必须计算标量乘积<x,y并返回它.

4。范数2必须计算欧几里得范数||x||=sqrt(<x,x(并返回它.

现在是我的代码:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double vec_plus(double* x, double* y, double a, unsigned int n) {
unsigned int i;
for (i = 0; i < n; i++) { 
x[i] = x[i] + (a * y[i]);
}
return *x;
}
double vec_mult(double* x, double a, unsigned int n) {
unsigned int i;
for (i = 0; i < n; i++) { x[i] = a * x[i]; }
return *x;
}
double dotprod(double* x, double* y, unsigned int n) {
unsigned int i;
double z = 0;
for (i = 0; i < n; i++) { z = z + x[i] * y[i]; }
return z;
}
double norm2(double* x, unsigned int n) {
unsigned int i;
double z = 0;
for (i = 0; i < n; i++) { z = z + hypot(x[i], x[i]); }
return z;
}
int main(void) {
unsigned int n;
unsigned int i;
printf("Please enter n = "); scanf_s("%u", &n);
double* x, * y, a;
x = (double*)malloc(n * sizeof(double));
y = (double*)malloc(n * sizeof(double));
if (x == NULL || y == NULL) { printf("malloc errorn"); exit(EXIT_FAILURE); }
for (i = 0; i < n; i++) {
printf("Please enter x_%d = ", i); scanf_s("%lf", &x[i]);
}
for (i = 0; i < n; i++) {
printf("Please enter y_%d = ", i); scanf_s("%lf", &y[i]);
}
printf("Please enter your scalar a = "); scanf_s("%lf", &a);
for (i = 0; i < n; i++) {
printf("nvec_plus_%d = %lf", i, vec_plus(x, y, a, n));
}
for (i = 0; i < n; i++) {
printf("nvec_mult_%d = %lf", i, vec_mult(x, a, n));
}
free(x); free(y);
return 0;
}


我有一种感觉,我可能在内存分配中的某个地方搞砸了,但我无法检测到我的错误,我也不确定。

作为示例,我将向您展示我的输入,我期望的输出和实际输出
/>

*My input*
Please enter n = 3
Please enter x_0 = 1
Please enter x_1 = 2
Please enter x_2 = 3
Please enter y_0 = 3
Please enter y_1 = 2
Please enter y_2 = 1
Please enter your scalar a = 6

*Output I expect/want*
vec_plus_0 = 19.000000
vec_plus_1 = 14.000000
vec_plus_2 = 9.000000
vec_mult_0 = 6.000000
vec_mult_1 = 12.000000
vec_mult_2 = 18.000000

*Actual output*
vec_plus_0 = 19.000000
vec_plus_1 = 37.000000
vec_plus_2 = 55.000000
vec_mult_0 = 330.000000
vec_mult_1 = 1980.000000
vec_mult_2 = 11880.000000


现在,这是我在编写代码时如何想象代码工作的方法:
假设我采用了与上述相同的输入.

*My input*
Please enter n = 3
Please enter x_0 = 1
Please enter x_1 = 2
Please enter x_2 = 3
Please enter y_0 = 3
Please enter y_1 = 2
Please enter y_2 = 1
Please enter your scalar a = 6


我想象的这部分代码中发生的事情:

double vec_plus(double* x, double* y, double a, unsigned int n) {
unsigned int i;
for (i = 0; i < n; i++) { 
x[i] = x[i] + (a * y[i]);
}
return *x;
}
function gets passed values of x, y, the scalar a and the length n.
now we get in the for loop with:
x[0] = x[0] + (a * y[0]); //I know the parentheses are redundant but i kind of tried all I could
x[0] = 1 + (6 * 3) // x[0] = 19
x[1] = x[1] + (a * y[1]);
x[1] = 2 + (6 * 2) // x[1] = 14
x[2] = x[2] + (a * y[2]);
x[2] = 3 + (6 * 1) // x[2] = 9


现在显然这不是实际的输出。有人可以帮我解决这个问题吗?
提前感谢,祝你有美好的一天!

vec_plus返回数组的第一个元素的值x

double vec_plus(double* x, double* y, double a, unsigned int n) {
unsigned int i;
for (i = 0; i < n; i++) { 
x[i] = x[i] + (a * y[i]);
}
return *x;  // returns value of the first element of x, x[0].
}

因此,每次调用时:

printf("nvec_plus_%d = %lf", i, vec_plus(x, y, a, n));

在 for 循环中main()你不会得到x[0]x[1]x[2]的预期值。您只能在每次迭代时获取并打印出x[0]的值。

此外,每次调用 vec_plus 函数时,x[0]都会更改:

x[i] = x[i] + (a * y[i]);  // x[0] is changing each time vec_plus() is called.

所以你不会得到x[1]x[2]的值,你只会得到x[0]的更改值,该值随着main()中每次迭代中对vec_plus的每次调用而变化:

x[i] = x[i] + (a * y[i]);
_________________________
1.Walkthrough, 1. call to vec_plus: 
x[0] = 1 + (6 * 3) // value of x[0] after this statement = 19
2.Walkthrough, 2. call to vec_plus:  
x[0] = 19 + (6 * 3) // value of x[0] after this statement = 37
3.Walkthrough, 3. call to vec_plus:  
x[0] = 37 + (6 * 3) //  value of x[0] after this statement = 55

您唯一需要做的就是更改 vec_plus 的返回值:

return x[i];

并将迭代包装在vec_plus:

unsigned int i;
for (i = 0; i < n; i++) { 
x[i] = x[i] + (a * y[i]);
}

相反,请使用 main(( 中迭代中的i。请注意i作为 vec_plus 的参数/参数:

double vec_plus(double* x, double* y, double a, unsigned int n, unsigned int i) {
x[i] = x[i] + (a * y[i]);
return x[i];  
}

main(( 中的 for 循环:

for (i = 0; i < n; i++) {
printf("nvec_plus_%d = %lf", i, vec_plus(x, y, a, n, i));
}

这同样适用于vec_mult函数。

最新更新