DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Matrice Vector

11.23.2008
| 1715 views |
  • submit to reddit
        
#include <stdio.h>
#include <stdlib.h>

int mv(int *v, int *z, int **A, int m, int n)
{
    int i,j;
    for (i=0;i<m;i++) {
        z[i] = 0;
        for (j=0;j<n;j++)
            z[i] += A[i][j] * v[j];
    }
}

int main(void)
{
    int i, j, m, n, *v, *z, **A;

    printf("n=");scanf("%d",&n);
    printf("m=");scanf("%d",&m);

    // alocare

    v = (int*) malloc (n * sizeof(int));
    z = (int*) malloc (m * sizeof(int));
    A = (int**) malloc (m * sizeof(int*));
    for (i=0;i<m;i++)
        A[i] = (int*) malloc (n*sizeof(int));

    // citire

    for (i=0;i<n;i++) {
        printf("v[%d]=",i);scanf("%d",&v[i]);
    }
    for (i=0;i<m;i++)
        for (j=0;j<n;j++) {
            printf("A[%d][%d]=",i,j);scanf("%d",&A[i][j]);
        }
    mv(v, z, A, m, n);

    // afisare

    for (i=0;i<m;i++)
        printf("%d ",z[i]);

    // dezalocare

    for (i=0;i<m;i++)
        free(A[i]);
    free(A);
    free(v);
    free(z);

    return 0;
}
    
    Tags:
  • C