【问题标题】:Read buffer as matrix using X and Y values in C使用 C 中的 X 和 Y 值将缓冲区读取为矩阵
【发布时间】:2016-04-13 21:30:53
【问题描述】:

我有一个包含 256 个整数的缓冲区,因此创建了一个 16x16 矩阵。我想要完成的是读取存储在缓冲区中的值,就好像它是一个矩阵一样。 因此,如果我给出 5 和 3 的坐标,我应该得到 Y 为 5 且 Y 为 X 的值。

例如,这里是缓冲区的一小部分,但在一个 16x3 矩阵中以便于阅读,并将其视为 0 而不是 1 的起始索引)

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4
4 5 6 7 8 9 1 0 2 3 4 5 6 7 8 9

因此,如果我要尝试获取 Y = 2 和 X = 5 处的值,我应该返回值 9。

这是我已经拥有的一些代码,但我的数学是错误的。

unsigned char getTablevalue(int tableIndex, unsigned char *buffer) {
    return buffer[tableIndex];
}

void getInput(....) {
    int yValue = 2;
    int xValue = 5;
    int returnValue = 0;
    unsigned char *buffer = malloc(256 * sizeof (unsigned char));
    memset(buffer, 0, 256);

    ... Some code to fill buffer...
    returnValue = getTablevalue({what can I put here}, buffer); 

}

对此的任何帮助将不胜感激!提前谢谢你。

【问题讨论】:

  • 将 col 乘以 16 并添加行。这是你的索引。
  • yValue * 16 + xValue。当然,这完全取决于您是否正确填充了缓冲区。
  • 为什么不使用二维数组?
  • 对索引进行了一些调整,这正是我想要的。非常感谢。有时基础知识只是被忽略了......

标签: c matrix int unsigned-char


【解决方案1】:

这演示了将缓冲区用作数组并将缓冲区“数组”复制到实际数组中。

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

int main()
{
    int n;
    int x, y;
    int arr[16][16];
    int xtest =2, ytest =12; //'random values to test that it works

    /* allocate space for a buffer of 256 values in the range 0 to 255) */
    unsigned char *buffer = malloc(256 * sizeof (unsigned char));

    /* fill the buffer with sequential values from 0 to 255 */
    for (n = 0; n < 256; n++) {
        buffer[n] = n;
    }

    /* just to check the fill was OK - print out the buffer contents */
    for (n = 0; n < 256; n++) {
        printf("%d .. ", buffer[n]);
    }
    printf("\n\n");

   /* fill a 16 * 16 array with values from the buffer
       that the buffer data is arranged in 16 groups of 16 values in a 
       single sequential buffer */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            arr[x][y] = buffer[(x * 16) + y];
        }
    }

    /* print out the array */
    for (x = 0; x < 16; x++) {
        for (y = 0; y < 16; y++) {
            printf("%d\t", arr[x][y]);
        }
        printf("\n");
    }
    printf("\n\n");

    /* just print a 'random' xy value from the matrix and from the buffer
       they will be the same (we hope) */
    printf("X=%d,Y=%d, Matrix[%d][%d] is: %d ... and Buffer %d,%d is %d\n",
            xtest, ytest, xtest, ytest, arr[xtest][ytest],
            xtest, ytest, buffer[(xtest * 16) + ytest]);
    if (arr[xtest][ytest] == buffer[(xtest * 16) + ytest]) {
        printf("Wow - they ARE the same\n");
        } else {
        printf("Oh No - they are different\n");
    }
}    

样本输出
X=2,Y=12, Matrix[2][12] is: 44 ... and Buffer 2,12 is 44 Wow - they ARE the same

【讨论】:

  • 你真是太棒了,谢谢。我能够采用其中的一些东西。从缓冲区读取或移动到数组会带来更高的性能损失。将进行调用以检查值 256 次(以获取每个值)以获取特定输入。
  • 我不知道哪一个是最好的。然而,有很多关于传统数组的信息,包括它们如何传递给函数以及如何访问数组——不仅通过“[]”下标,还通过操作指向数组的指针,所以我肯定会选择传统数组- 但这都是很好的学习。如果您认为我的回答有用,请给它 +1 或接受 - 或不接受 - 您的电话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-15
  • 1970-01-01
  • 2021-05-20
  • 2014-08-22
  • 2023-03-22
相关资源
最近更新 更多