I kind of got stuck here with a little problem:
I study for my exam and my task was to write a program that can do the following:
There are two given vectors x and y who are an element of R^(n) and each of them is a double array of the length n, with n being an element of N. In addition there is a double variable a as element of R given.
I need to write four functions vec_plus, vec_mult, dotprod and norm2, which have the following purpose:
(My problems so far are only considering vec_plus and vec_mult)
1. vec_plus has to calculate x + a * y (element of R^n) and save it in x!
2. vec_mult has to calculate a * x (element of R^n) and save it in x!
3. dotprod has to calculate the scalar product <x,y> and return it.
4. norm2 has to calculate the euclidean norm ||x|| = sqrt(<x,x>) and return it.
Now here is my code:
#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;
}
I have a feeling that I might have messed up somewhere in the memory allocation but I am not able to detect my error and I am also not sure.
As example I will show you my input, the output I'd expect and the actual output.
*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
Now here is the way, how I imagined my code would work when I wrote it:
Lets say I took the same input as mentioned.
*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
What I imagine happens in this part of the code:
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
Now obviously this is not the actual output. Can someone please help me fix this problem?
Thanks in advance and have a great day!