struct that stores the number of rows and columns of the matrix and of a vector

Asked

Viewed 188 times

0

I used a struct to store the number of rows (m) and columns (n) of the matrix and another to store the number of columns of the vector (n). I make the product of two matrices in function prod() and the product of a matrix with a vector in the function solve(), but the output is always 0. I want the vector generated from the matrix product by the vector to appear in the output. I don’t know how to change the code to make him do what I want, I think the mistake is in the third for which stores the calculated values. I think the error is here, in the third for of function prod:

C.p_data[iA.n+j] = C.p_data[iA.n+j]+(A.p_data[i*A.m+k]B.p_data[kB.n+j]);

but I don’t know how to change the indexes to correctly store the calculated value.

Follows the code:

#include<stdio.h>
#include<stdlib.h>

// define tipo matriz com campos linha, coluna e ponteiro
typedef struct{
  int m;
  int n;
  double *p_data;
}matriz_t;

// define tipo vetor, uma struct com campos coluna e ponteiro 
typedef struct{
  int n;
  double *p_data;
}vetor_t;

//prototipo das fucncoes
matriz_t le_matriz();
vetor_t le_vetor();
matriz_t prod(matriz_t A,matriz_t B);
vetor_t solve(matriz_t C,vetor_t a);
void mostra_vetor(vetor_t b);

int main(){
  matriz_t A,B,C;
  vetor_t a,b;
  A = le_matriz();
  B = le_matriz();
  a = le_vetor();
  C = prod(A,B);
  b = solve(C,a);
  mostra_vetor(b);
  return 0;
}

matriz_t le_matriz(){
  matriz_t A;
  int i,j;

  printf("tam M1");

  // armazena n de linhas e de colunas da matriz
  scanf("%d",&A.m);
  scanf("%d",&A.n);

  printf("\n");

  A.p_data = malloc(A.m*A.n*sizeof(double));

  printf("elem m1:\n");  

  // armazena elementos matriz 
  for(i=0;i<A.m;i++){
     for(j=0;j<A.n;j++){
        scanf("%lf", &A.p_data[i*A.n+j]);
     }
  }

  printf("\n");

  return A; 
}

vetor_t le_vetor(){
  vetor_t a;
  int i;

  printf("tam v1\n");

  // armazena tamanho vetor 
  scanf("%d",&a.n);

  a.p_data = malloc(a.n*sizeof(double));

  printf("\n");
  printf("elemv1:\n");

  // armazena elementos do vetor
  for(i=0;i<a.n;i++){
     scanf("%lf",&a.p_data[i*a.n]);
  }

  printf("\n");

  return a;
}

matriz_t prod(matriz_t A, matriz_t B){
  matriz_t C;
  int i,j,k;

  C.p_data = malloc(A.m*B.n*sizeof(double));

  // condicao para o produto matriz matriz
  if(A.n == B.m){
    // percorre coluna matriz A
    for(i=0;i<A.n;i++){
       // percorre linha matriz B
       for(j=0;j<B.m;j++){
         // inicializa terceira matriz com 0
     C.p_data[i*A.n+j] =0;
          // calcula e armazena o resultado numa 3 matriz
          for(k=0;k<B.n;k++){
             C.p_data[i*A.n+j] = C.p_data[i*A.n+j]+(A.p_data[i*A.m+k]*B.p_data[k*B.n+j]); 
          }
       }
    }

    return C;
  }
}

vetor_t solve(matriz_t C, vetor_t a){
  vetor_t b;
  int i,j,k;

  b.p_data = malloc(C.m*a.n*sizeof(double));

  // condicao do produto matriz vetor
  if(C.m == a.n){
   // percorre linha matriz
   for(i=0;i<C.m;i++){
     // percorre coluna vetor
     for(j=0;j<a.n;j++){
       // terceira matriz para receber os valores calculados
       b.p_data[i*C.n+k] = 0;
        for(k=0;k<a.n;k++){
           b.p_data[i] = b.p_data[i]+(C.p_data[i*C.n+k]*a.p_data[k*a.n+j]);
        }
     }
   }

   return b;
  }
}

void mostra_vetor(vetor_t b){
  int i;

  // mostra valores do vetor
  for(i=0;i<b.n;i++){
     printf("%lf\n",b.p_data[i]);
  }
}
  • https://ideone.com/8lbncD

  • 2

    Please do the [tour] to understand the basics of how the site works. Here you will find primary tips for formatting your question. [pt.so] uses the Markdown format, but if you don’t know, you will learn the basics on the tour.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.