有效存储三角矩阵 [英] Store triangular matrix efficiently

查看:130
本文介绍了有效存储三角矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过不将所有零存储在内存中来有效地存储一个较低的三角矩阵,所以我已经考虑过这种方式:首先,我为每一行分配内存,然后为每一行分配i + 1字节,因此,我不必担心零,但是在第一次分配时出了点问题.我究竟做错了什么?这是我的代码,并且在读取矩阵的维之后,编译器在第8行退出程序.

I need to efficiently store a lower triangular matrix by not storing all the zeroes in the memory, so I have thought about it this way: first I allocate memory for every row, then for each row I allocate i+1 bytes, so I never have to worry about the zeroes, but something is wrong at the first allocation. What am I doing wrong? This is my code, and the compiler exits the program at line 8, just after reading the dimension of the matrix.

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

int main ()
{
  int i, j, **mat1, dim;

  scanf("%d",&dim);
  *mat1 = (int**)calloc(dim, sizeof(int*));

  for(i = 0; i<dim; i++)
    mat1[i] = (int*)calloc(i+1, sizeof(int));

  for(i = 0; i < dim; i++)
    for(j = 0; j < i+1; j++)
       scanf("%d", &mat1[i][j]);

  for(i=0; i<dim; i++)
    for(j=0; j<(i+1); j++)
       printf("%d%c", mat1[i][j], j != (dim-1) ? ' ' : '\n');

  return 0;
}

编辑

好的,所以在按照您的帮助方式修改代码后,我必须读取一个上三角和一个下三角矩阵并显示它们的乘积.问题是我不将零存储在内存中,所以如果我使用传统的3-for算法,它将显示一些垃圾值.如果我将每个矩阵的其余部分初始化为0,则无法动态分配内存,因为我还会存储零,所以我没有做过我想我必须在某个地方修改代码,或者也许修改for间隔,但是无论如何,我仍然修改程序仍然在右上角输出(对于3x3矩阵)2个垃圾值.这样吗?

ok so after modifying the code the way you helped me, I have to read an upper triangular and a lower triangular matrix and show their product.The problem with this is that I don't store the zeroes in memory, so if I use the traditional 3-for algorithm, it will show some junk values.And if I initialize the rest of each of the matrixes with 0 it's useless to allocate the memory dynamically because I would also have the zeroes stored so I haven't done anything to improve the efficiency of the storage.I think I have to modify the code somewhere, or maybe the for intervals, but anyway I modify the program still outputs (for 3x3 matrix) 2 junk values in the upper right corner.How could I do this?

#include <stdio.h>
#include <stdlib.h>
int main ()
{
int i,j,k,**mat1,**mat2,**prod,dim;


printf("Give dimension: \n");
scanf("%d",&dim);

mat1 = (int**)calloc(dim,sizeof(int*));

for(i=0; i<dim; i++)
    mat1[i] = (int*)calloc(i+1,sizeof(int));

mat2 = (int**)calloc(dim,sizeof(int*));

for(i=dim-1; i>-1; i--)
    mat2[i]=(int*)calloc(i+1,sizeof(int));

prod = (int**)calloc(dim,sizeof(int*));
for(i=0; i<dim; i++)
    prod[i] = (int*)calloc(dim,sizeof(int));

printf("Give lower triangular matrix(non 0 values only): \n");
for(i=0; i<dim; i++)
    for(j=0; j<i+1; j++)
        scanf("%d",&mat1[i][j]);

printf("Give upper triangular matrix(non 0 values): \n");
for(i=0; i<dim; i++)
    for(j=i; j<dim;j++)
        scanf("%d",&mat2[i][j]);

printf("Matrix A is: \n");
for(i=0; i<dim; i++)
    for(j=0; j<dim; j++)
        printf("%d%c",j<=i?mat1[i][j]:0,j!=dim-1?' ':'\n');

printf("Matrix B is: \n");
for(i=0; i<dim; i++)
    for(j=0; j<dim; j++)
        printf("%d%c",j>=i?mat2[i][j]:0,j!=dim-1?' ':'\n');

for(i=0; i<dim; i++)
    for(j=0; j<dim; j++)
        for(k=0; k<dim; k++)
            prod[i][j]+=mat1[i][k]*mat2[k][j];


printf("The product of the two matrix is: \n");
for(i=0; i<dim; i++)
    for(j=0; j<dim; j++)
        printf("%d%c",prod[i][j],j!=dim-1?' ':'\n');


return 0;

}

推荐答案

mat1 = calloc(dim,sizeof(int*));

mat1是双指针.您需要为指针数组分配内存,以后需要分别为每个指针分配内存.无需强制转换calloc()

mat1 is a double pointer.You need to allocate memory for your array of pointers and later you need to allocate memory to each of your pointers individually.No need to cast calloc()

这篇关于有效存储三角矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆