c - 找到矩阵中等于坐标 (i+j) 之和的所有元素

标签 c matrix linked-server

我可以扫描矩阵并确定大小,但输出为空白。

这是我的代码:

int createTripplesArrayAndList(int** mat, int row, int colum, Tripple **arr, Node** ls) {
    int i, j, count=0, k = 0;
    Node* head = *ls;
    Node* tmp=NULL;

    //count how many elements would be in the required array\list
    for (i = 0; i < row; i++) {
        for (j = 0; j < colum; j++) {
            if (mat[i][j] == i + j) {
                count++;
            }
        }
    }

    //going over the matrix and add the relevant elements to the array and to the list
    (*arr) = (Tripple*)calloc(count, sizeof(Tripple));
    for (i = 0; i < row; i++) {
        for (j = 0; j < colum; j++) {
            if (mat[i][j] == i + j) {
                //add element to array
                (*arr)[k].argument = mat[i][j];
                (*arr)[k].i = i;
                (*arr)[k].j = j;

                //add element to the list
                if (!head) {
                    tmp = (Node*)malloc(sizeof(Node));
                    tmp->value = (*arr)[k];
                    tmp->next = NULL;

                    head = tmp;
                } else {
                    Node *new_node = (Node*)malloc(sizeof(Node));
                    new_node->value = (*arr)[k];
                    new_node->next = NULL;
                    head->next = new_node;
                    head = new_node;
                }
                k++;
            }
        }
    }

    *ls = tmp;
    return count;
}

void printTrripleArrAndList(Tripple* arr, int n, Node** ls) {
    int i, j;
    Node* curr = *ls;

    printf("The tripple list is: ");
    while (curr != NULL) {
        printf("%d+%d=%d  ->", curr->value.i, curr->value.j, curr->value.argument);
        curr = curr->next;
    }

    printf("\nThe tripple array is: ");
    for (i = 0; i < n; i++) {
        printf("%d+%d = %d\n", arr[i].i,arr[i].j,arr[i].argument);
    }
}

int ** createMat(int*n,int*m) {
    int** mat = NULL;
    int i, j, row, column;

    //allocate and the matrix
    printf("please enter size of row and colum: ");
    scanf("%d%d", &row, &column);
    mat = (int**)calloc(row, sizeof(int*));
    for (i = 0; i < row; i++) {
        mat[i] = (int*)calloc(column, sizeof(int));
    }

    //fill the matrix
    printf("enter numbers for the matrix: ");
    for (i = 0; i < row; i++) {
        for (j = 0; j < column; j++) {
            scanf("%d", &mat[i][j]);
        }
    }

    *n = row;
    *m = column;

    return mat;
}

我曾经收到一条错误消息,说类型 Node* 的值不能分配给类型 list* 的实体,所以我不得不将它们都更改为 Node* 有问题的地方是:

head->next = new_node;

和:

curr = curr->下一个;

请帮忙。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M 2
#define N 3
#define R 4

typedef struct Tripple {
    int i;
    int j;
    int argument;
} Tripple;

typedef struct Node {
    Tripple value;
    struct Node* next;
} Node;

int* powerArray(int n);
void printPowArr(int* p, int n);
int** MatrixMultiplication(int firstMat[M][N], int secondMat[N][R]);
void printMatrix(int** p);
int createTripplesArrayAndList(int** mat, int row, int colum, Tripple **arr, Node** ls);
void printTrripleArrAndList(Tripple* arr, int n, Node** ls);
int** createMat(int* n, int*m);

这是我的主要内容:

void main() {
    int *p;
    int** newMat;

    int p1[M][N] = { { 1, 4, 2 }, { 0, 3, 1 } };
    int p2[N][R] = { { 1, 3, 1, 4 }, { 0, 2, 6, 4 }, { 4, 1, 0, 7 } };
    int **mat=NULL;
    int count;
    int n = 0, m = 0, i = 0, j = 0;
    p = powerArray(10);
    printPowArr(p, 10);



    newMat = MatrixMultiplication(p1, p2);
    printMatrix(newMat);


    Tripple* arr=NULL;
    Node* ls=NULL;
    mat=createMat(&n,&m);

    count = createTripplesArrayAndList(mat, n, m, &arr, &ls);
    printTrripleArrAndList(arr, count, &ls);
    system("pause");
}

最佳答案

你同时指向 head 和 head->next to new_node!这是荒谬的。删除 head->next = new_node。

关于c - 找到矩阵中等于坐标 (i+j) 之和的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43836630/

相关文章:

java - 添加和乘以二维矩阵

sql-server - 查询一个链接的sql server

c - 如何在 C 中动态加载 Rust 库?

c - 了解malloc的不同方法

c cygwin- abored(核心已转储)

c - 如何使用 MPI_Scatterv 将矩阵的行发送到所有进程?

c - 数值稳定正向替换

mysql - SELECT 和 INSERT 的 OPENQUERY 有何不同?

.net - .NET 程序中的分布式事务(MS-DTC 配置?!?)还是通过链接服务器在存储过程中?

c++ - C/C++ 指针和数组帮助