【问题标题】:find all the elements of the matrix that are equal to the sum of the coordinates (i+j)找到矩阵中所有等于坐标和 (i+j) 的元素
【发布时间】:2017-05-07 20:55:14
【问题描述】:

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

这是我的代码:

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");
}

【问题讨论】:

    标签: c matrix linked-server


    【解决方案1】:

    您将 head 和 head->next 都指向 new_node!这是荒谬的。移除 head->next = new_node.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2020-06-05
      • 2020-01-10
      • 1970-01-01
      相关资源
      最近更新 更多