可以帮我写一下吗,要求都在下面,不会做

img

img


要求就是预先输入图上的矩阵,写两个函数,将矩阵转化为三元组形式并输出以及将三元组形式转化为矩阵再输出,使用C语言

基于new bing加以修改的编写,有帮助记得采纳一下哦:
【运行截图】

img

【代码】

#include <stdio.h>

void func1(int ch[][7], int *i, int *j, int *val, int nonZeroCount)
{
    int index = 0;
    for (int row = 0; row < 6; row++)
    {
        for (int col = 0; col < 7; col++)
        {
            if (ch[row][col] != 0)
            {
                i[index] = row;
                j[index] = col;
                val[index] = ch[row][col];
                if (index >= nonZeroCount) {
                    printf("Error: the number of nonzero elements in matrix is greater than the specified maximum value.\n");
                    return;
                }
                index++;
            }
        }
    }
}

void func2(int ch[][7], int *i, int *j, int *val, int nonZeroCount)
{
    for (int k = 0; k < nonZeroCount; k++)
    {
        ch[i[k]][j[k]] = val[k];
    }
}

int main()
{
    int a[6][7] = {
        {0,0,1,0,0,0,0},
        {0,2,0,0,0,0,0},
        {3,0,0,0,0,0,0},
        {0,0,0,5,0,0,0},
        {0,0,0,0,6,0,0},
        {0,0,0,0,0,7,4}
    };

    int nonZeroCount = 0;
    for (int row = 0; row < 6; row++)
    {
        for (int col = 0; col < 7; col++)
        {
            if (a[row][col] != 0)
            {
                nonZeroCount++;
            }
        }
    }

    int iArr[nonZeroCount];
    int jArr[nonZeroCount];
    int valArr[nonZeroCount];
    func1(a, iArr, jArr, valArr, nonZeroCount);

    
    for (int k = 0; k < nonZeroCount; k++)
    {
        printf("%d %d %d\n", iArr[k], jArr[k], valArr[k]);
    }
   
    int b[6][7] = {0};
    func2(b, iArr, jArr, valArr, nonZeroCount);

   
    for (int row = 0; row < 6; row++)
    {
        for (int col = 0; col < 7; col++)
        {
            printf("%d ", b[row][col]);
        }
        printf("\n");
    }

    return 0;
}

明天吧 , 还有下次能不能不要贴图了 , 今天为啥这么多人问问题喜欢贴图呢 ...