Hi,
I am trying to create a program in C that will multiply a matrix with a vector. Both the size of the vector and dimensions of the matrix are given by the user. I am having the most trouble trying to declare and use the 2D array, which is my matrix. I just don't get it. Here is what I have so far. there are probably a lot of errors because I don't quite understand how to use pointers correctly.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
int s, z; /*s is number of columns, z number of rows and length of vector*/
int i, x, y, ns, nz;
double *X, *E, *a; /*X is input vector, E is result vektor, a is matrix*/
FILE *fd;
/*Vektor*/
fd = fopen("vektor3_2.dat", "r");
fscanf(fd, "%d", &z);
nz=z;
if (z < 2) return 2;
X = malloc(z * sizeof(*X));
E = malloc(z * sizeof(*E));
a = malloc(z * s * sizeof(*a));
for (i = 0; i < z; i++)
fscanf(fd, "%lf", &X[z]);
/*Matrix*/
fscanf(fd, "%d", &s);
ns=s;
nz=z;
for (z = 0; z<nz; z++)
for (s=0; s<ns; s++)
fscanf(fd, "%lf", &a[z][s]);
fclose(fd);
/*Multiplication*/
for (z=0; z<nz; z++)
for (s=0; s<ns; s++)
E[z] = E[z] + a[z][s] * X[z];
/*result out*/
fd = fopen("Ergebnis3_2.dat", "w");
for(i=0; i<z; i++)
printf("%lf\n", E[z]);
}