【问题标题】:Using functions to lesser repetitiveness使用函数减少重复
【发布时间】:2023-01-31 21:07:58
【问题描述】:

我已经研究这个问题一段时间了:基本上我需要将 for 循环放在一个函数中以便我可以调用它,但是我不知道如何让函数返回一个二维数组,我想要通过创建一维数组来解决这个问题,但问题是我的任务是计算矩阵对角线下的数字总和,所以我需要它首先是二维的,然后它只能变成一维的。有没有人有办法解决吗?

也许我的思考过程是错误的,有人可以建议如何将 for 循环放入函数中?如果里面没有 if 子句,那么我可能会有想法,但现在我真的没有了。

#include <math.h>
#include <stdio.h>
#include <stdlib.h> // libraries added from example
#include <time.h>

//(*) For a square matrix calculate the sum of elements under the main diagonal excluding it.
#define A -10
#define B 10

int main() {
    void enter(int *x, int *y);
    int get_random(int lbound, int ubound); // telling the programs that functions are declared
    int r;
    int c;
    int row, col, sum = 0;
    enter(&r, &c); // calling the function
    srand48(time(NULL)); //Call srand48 with current time reported by `time` casted to a long integer.
    // srand48 is used to reinitialize the most recent 48-bit value in this storage
    int array[r][c]; // we decided its gonna be r rows and c columns
    int line[r * c]; // turning 2d into 1d array
    for (row = 0; row < r; ++row) // we cycle numeration of rows of matrix
    {
        for (col = 0; col < c; col++) // we cycle numeration of columns of matrix
        {
            array[row][col] = get_random(B, A);// filling array with random numbers, taken from example
            printf("%d ", array[row][col]);
            if (row > col) { //since we want the sum numbers below the diagonal row>col must be true
                sum = sum + array[row][col];// if row>col then we add the number to our sum;
            };
        }
        printf("\n"); // this is to break line after row 1,2 col 3, so it looks nicer
    }
    for (row = 0; row < r; ++row) // we cycle numeration of rows of matrix
    {
        for (col = 0; col < c; col++) // we cycle numeration of columns of matrix
        {
            line[row * r + col] = array[row][col];
        }
    }
    printf("the array in 1D: ");
    for (row = 0; row < r * c; row++) {
        printf("%d ", line[row]);
    }
    printf("\n");
    printf("sum of array below the diagonal: %d\n", sum);

    return 0;
}

void enter(int *x, int *y) { // we have to use pointers if we want more then one return from a function

    printf("How man rows in array?  "); // just like the last lab we decide how big the matrix will be
    scanf("%d", x); // we use x instead of &x because we need the address of the number not the value
    printf("How man columns in array? ");
    scanf("%d", y); // we use y instead of &y because we need the address of the number not the value
}

int get_random(int lbound, int ubound) {
    return mrand48() % (ubound - lbound + 1) + lbound; // function for generating random numbers
}

必须满足以下条件:

  1. 用户决定方阵的大小

  2. 矩阵必须用随机数填充

  3. 函数调用的数组必须是一维使用i*N+j,不能传递二维数组

【问题讨论】:

  • 您可以使用 malloc 分配它,而不是将数组创建为本地/自动变量,这将为您提供一个指针。该指针可以由您的函数返回。但是,您必须考虑到如果调用者不知道数组的大小,调用者就不可能以任何有用的方式进行解释。也许调用者最好提供大小。选择...

标签: c


【解决方案1】:

根本不用理会二维数组,只需执行以下操作:

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

int *
make_array(size_t size)
{
        int *a = malloc(sizeof *a * size * size);
        int *t = a;
        if( a == NULL ){
                perror("malloc");
                exit(1);
        }
        for(int r = 0; r < size; r += 1 ){
                for(int c = 0; c < size; c += 1 ){
                        *t++ = rand() % 32 - 16;
                }
        }
        return a;
}

int
trace(int *a, size_t s)
{
        int sum = 0;
        for( size_t i = 0; i < s; i += 1 ){
                sum += *a;
                a += s + 1;
        }
        return sum;
}

int
main(int argc, char **argv)
{
        srand(time(NULL));
        size_t s = argc > 1 ? strtol(argv[1], NULL, 0) : 5;
        int *a = make_array(s);
        for( int i = 0; i < s; i +=1 ){
                for( int j = 0; j < s; j+= 1 ){
                        printf("%3d  ", a[i * s + j]);
                }
                putchar('
');
        }
        printf("trace: %d
", trace(a, s));
}

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2021-03-09
    • 2017-08-25
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多