简体   繁体   English

矩阵作为2d指针C

[英]matrix as 2d pointer C

I have a 2d pointer and i use it as a matrix. 我有一个二维指针,我用它作为矩阵。 For every line full of even numbers i want to add another line that will have the sum of elements. 对于每行充满偶数的行,我想添加另一行,行中将包含元素总和。 for ex: 例如:

initial 初始

 1  1  1

12 14 16

 3  3  3

final 最后

 1  1  1

12 14 16

 3  5  7

 3  3  3

i have the following code and i think i have a shifting error can somedoy give me some sugestions. 我有下面的代码,我想我有一个转移的错误可以一定程度上给我一些建议。

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

int suma(int x)     //sum function
{
    int s = 0, r = 0;
    while (x != 0)
    {
        r = x % 10;
        s = s + r;
        x = x / 10;
    }
    return x;
}

int main(void)
{
    int i, j, n, m, p, q, ii, **a, par = 0,r;
    printf("nr de rows\n");
    scanf("%d", &m);
    printf("columns\n");
    scanf("%d", &n);
    a = (int**)malloc(m*sizeof(int*));
    for (i = 0; i < m; i++)
    {
        a[i] = (int*)malloc(n*sizeof(int));
    }
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("a[%d][%d]=", i + 1, j + 1);
            scanf("%d", &a[i][j]);
        }
    }
    printf("\n\n\nmatricea initiala\n");    //initial matrix
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            if (a[i][j] % 2 == 0)
            {
                par++;
            }
            else
            {
                par = 0;
            }
        }
        if (par == n)
        {
            m++;
            a = (int**)realloc(a, m*sizeof(int*));
            a[m] = (int*)malloc(n*sizeof(int));
            for (p = m; p >i; p--)
            {
                for (q = 0; q < n; q++)
                {
                    a[p][q] = a[p - 1][q];
                }
            }
            par = 0;
            for (r = 0; r < n; r++)
            {
                a[i + 1][r] = suma(a[i][r]);
            }
            i--;
        }
        par = 0;
    }
    printf("\nmat finala\n");       //final matrix
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%d\t", a[i][j]);
        }
        printf("\n");
    }
}

fix point: 修复点:

1. return x; 1. return x; --> return s; -> return s;
2. for (i = 0; i < m; i++) --> for (i = m-1; i >= 0; --i) : The process from the bottom to the top 2. for (i = 0; i < m; i++) -> for (i = m-1; i >= 0; --i) :从下到上的过程
3.at after realloc, a[m] = (int*)malloc(n*sizeof(int)); 3.在重新分配之后, a[m] = (int*)malloc(n*sizeof(int)); --> a[m-1] = (int*)malloc(n*sizeof(int)); -> a[m-1] = (int*)malloc(n*sizeof(int)); : a[m] is out of bounds. :a [m]超出范围。
4. for (p = m; p >i; p--) --> for (p = m-1; p >i+1; p--) 4. for (p = m; p >i; p--) -- for (p = m; p >i; p--) -> for (p = m-1; p >i+1; p--)
5. i--; 5.我- i--; --> //i--; -> //i--; : It is no longer needed. :不再需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM