Hi everybody!
Im an absolute beginner in c coding. I still have problems with dyn allocation and pointers.
could somebody help me with this? It calculates the skalar product of 2 vectors a and b with n dimensions.
can´t get this to work.
Code:
/* Skalarproduct dyn alloc. 14.2 */
#include <stdio.h>
#include <stdlib.h>
void main()
{
int n, j, i; /* n=dimension*/
float *a, *b, skaprod=0.0;
printf("Enter dimension of the vector?-->");
scanf("%d", &n);
a= (float*)malloc(n*sizeof(float));
b= (float*)malloc(n*sizeof(float));
for (i=0; i<n; i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(j=0; j<n; j++)
{
printf("b[%d]=",j);
scanf("%f",&b[j]);
}
for(i=0;i<n;i++)
skaprod=skaprod+(a[i]*b[i]);
printf("the result is-->%f\n",skaprod);
free(a);
free(b);
}
my compiler is lcc-win32 and I think its not easy to handle for beginners.