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

如何用标量添加两个向量,并将结果保存在其中一个向量中,然后从C中的函数返回此向量?,c,arrays,vector,C,Arrays,Vector,我有点被一个小问题困住了: 我为考试而学习,我的任务是编写一个可以完成以下任务的程序: 有两个给定向量x和y是R^(n)的一个元素,每个向量都是长度n的双数组,其中n是n的一个元素。此外,还有一个双变量a作为R的元素 我需要编写四个函数vec_plus、vec_mult、dotprod和norm2,它们的用途如下: (到目前为止,我的问题只考虑了vec\u plus和vec\u mult) 1.vec_plus必须计算x+a*y(元素R^n)并将其保存在x 2.vec\u mult必须计


我有点被一个小问题困住了:

我为考试而学习,我的任务是编写一个可以完成以下任务的程序:
有两个给定向量xyR^(n)的一个元素,每个向量都是长度n的双数组,其中nn的一个元素。此外,还有一个双变量a作为R的元素

我需要编写四个函数vec_plus、vec_mult、dotprod和norm2,它们的用途如下:
(到目前为止,我的问题只考虑了vec\u plusvec\u mult

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

2.vec\u mult必须计算a*x(元素R^n)并将其保存在x

3.dotprod必须计算标量积并返回它。

4.norm2必须计算欧几里德范数|x| |=sqrt()并返回它。

下面是我的代码:

#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 error\n"); 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;
}

*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

*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
double vec\u plus(double*x,double*y,double a,unsigned int n){
无符号整数i;
对于(i=0;i

很明显,这不是实际产出。有人能帮我解决这个问题吗

提前感谢,祝你度过愉快的一天

vec\u 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].
}
main()
中的for循环中,没有按该顺序获得
x[0]
x[1]
x[2]
的预期值。每次迭代仅获取并打印
x[0]
的值

此外,
x[0]
在每次调用vec\u plus函数时都会发生变化:

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\u plus
的调用而更改:


您只需更改vec_plus的返回值:

return x[i];
    unsigned int i;
    for (i = 0; i < n; i++) { 
        x[i] = x[i] + (a * y[i]);
    }
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];  
}
并将迭代封装在vec_plus中:

return x[i];
    unsigned int i;
    for (i = 0; i < n; i++) { 
        x[i] = x[i] + (a * y[i]);
    }
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()中的循环:

(i=0;i{ printf(“\nvec\u plus\uu%d=%lf”,i,vec\u plus(x,y,a,n,i)); }


这同样适用于
vec\u mult
功能

vec\u 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].
}
main()
中的for循环中,没有按该顺序获得
x[0]
x[1]
x[2]
的预期值。每次迭代仅获取并打印
x[0]
的值

此外,
x[0]
在每次调用vec\u plus函数时都会发生变化:

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\u plus
的调用而更改:


您只需更改vec_plus的返回值:

return x[i];
    unsigned int i;
    for (i = 0; i < n; i++) { 
        x[i] = x[i] + (a * y[i]);
    }
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];  
}
并将迭代封装在vec_plus中:

return x[i];
    unsigned int i;
    for (i = 0; i < n; i++) { 
        x[i] = x[i] + (a * y[i]);
    }
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()中的循环:

(i=0;i{ printf(“\nvec\u plus\uu%d=%lf”,i,vec\u plus(x,y,a,n,i)); }


这同样适用于
vec\u mult
功能

您知道,您的加法运算和其他运算正在修改操作数(原始数组),对吗?也
返回*x
正在返回
x
的第一个元素。向量加法的结果应该是一个向量。您应该使用调试器来检测这些值是如何以及在何处达到这些与您预期不同的值的。@EugeneSh。我还考虑了修改加法中的操作数的问题,但我不知道如何解决这个问题。我应该在函数中声明一个新数组,并将其保存在那里,然后从那里返回吗?我可以这样做,但在任务中,我要求它保存在数组x中,这就是为什么我不知道该怎么做。我最初将我的函数vec_plus和vec_mult声明为double*函数,但不知何故,我尝试了太多新东西,我觉得我的代码更糟了。@RobertS,是的,这对我来说很有意义。但我以前从未使用过调试器,也不知道它是如何工作的。我将尝试寻找一些关于这个的教程。您知道,您的加法和其他加法正在修改操作数(原始数组),对吗?也
返回*x
正在返回
x
的第一个元素。向量加法的结果应该是一个向量。您应该使用调试器来检测这些值是如何以及在何处达到这些与您预期不同的值的。@EugeneSh。我还考虑了修改加法中的操作数的问题,但我不知道如何解决这个问题。我应该在函数中声明一个新数组,并将其保存在那里,然后从那里返回吗?我可以这样做,但在任务中,我要求它保存在数组x中,这就是为什么我不知道该怎么做。我最初声明了我的函数vec_+a