【问题标题】:Allocating 2d array (type char) to prepare memory for float calculation分配二维数组(类型 char)为浮点计算准备内存
【发布时间】:2020-03-31 11:56:22
【问题描述】:

首先我必须输入二维数组的大小(n 和 m),以及教授的位置和教授的严格性。 然后我必须扫描矩阵。

我的问题是我有类型 char 矩阵,但我需要将矩阵转换为浮点数,因为我需要浮点数。

  for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            scanf(" %c", &mat[i][j] );
        }
    }
    scanf(" %c", &dane);

    **pass(mat, n,m, prof, strictness);

    system("pause");
    return 0;
}


float **pass(char **mat, int n, int m, int prof, int strictness){




    mat=(float **)calloc(n, sizeof(float *));
    if(mat==NULL)
    {
        printf("\nWRONG");

    }
    int i;
    for(i=0;i<n;i++)
    {
        mat[i]=(float *)calloc(m, sizeof(float));
    }
    return mat;
}

这是分配内存的正确方法和良好的开始继续吗?

输入: 4 4 2 30

Z N N Z

X X X Z

N N N Z

N N N Z

输出

0.00 0.00 0.00 0.00

29.67 0.00 96.75 0.00

0.00 0.00 0.00 0.00

0.00 0.00 0.00 0.00

【问题讨论】:

  • 这只是部分代码,我问这是一个好的开始,我分配内存是否好
  • 开头不完整的粘贴内容没有显示mat的声明。 为什么如果你需要一个float 矩阵,你有一个char 矩阵?不,除非您处理原始二进制数据,否则没有明智的方式在它们之间进行转换或复制。
  • 字符矩阵[MAXR][MAXS]={};我有 char 矩阵,因为我需要输入字母,然后计算每个 X 与 Z 和 N 的距离
  • 因此,首先解析输入,然后将您拥有的任何字符串转换为数字,然后再执行其他任何操作。
  • 我的问题是,如果我 calloc 内存 2d 矩阵中的每个点都将为 0,我对此无能为力,我应该先计算距离,然后再计算 calloc 还是?

标签: c function for-loop pointers


【解决方案1】:

对于这个答案,我不知道你打算用浮动 ** 做什么。尽管如此,我只是向您展示如何分配 char 并将其转换为它。 char 值是有符号还是无符号取决于您。

用于分配内存。有两种方法。最简单的一个是您可以对 malloc 进行两次调用。为指向浮点指针的指针分配空间。然后为要浮动的指针分配内存。基本上,指向 float 的指针是 2D 矩阵的所有索引,而 mat 是指向它们的指针数组。当涉及到 3D 时,情况有所不同,但现在是这样。

第二种方法有点难,但是你理解第一种方法很容易。第二种方法是为指向指针的指针和一个 malloc 中的每个浮点指针分配足够的内存。该内存页的第一个字节地址是指针的第一个索引。而第一个地址 + 指针的总大小是浮点指针的开头。

很难用语言来解释这一点,但我希望代码有所帮助。看了第一种方法后,就很容易理解第二种方法了。我将调试代码留在了那里,以便您更容易理解它们是如何工作的。

方法一(2 malloc):

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

float **pass(float **mat, int n, int m, int prof, int strictness);

int main(){
  int n, m, memIndex = 0;
  char dane;
  float ** mat;
  float * matIndexes;
  // Used unsigned char or signed char as approriate.
  unsigned char ctemp;

  puts("Enter n size:");
  scanf(" %d%*[^\n]", &n);

  puts("Enter m size:");
  scanf("%d%*[^\n]", &m);

  // n && m have to be at least 1
  if ( n < 1 || m < 1 ){
    puts("User input value is wrong.");
    return 1;
  }

  // Mat total size equal float pointer size times how many index.
  mat = (float **) malloc( sizeof(float*) * n );

  // Mat indexes total size equal float size multiply by how many index will be in the 2D matrix. size(2, 2) = [0][0], [0][1],[1][1] = 4 indexes. size(1, 1) = [0][0] = 1;
  matIndexes = (float *) malloc ( sizeof(float) * n * m );


  // mat && matIndexes can't be null.
  if ( mat == NULL || matIndexes == NULL ) {
    puts("Error when memory location.");
    return 1;
  }

  // Getting the char part
  for(int i=0;i<n;i++){
    mat[i] = &matIndexes[memIndex];
    for(int j=0;j<m;j++){
      scanf(" %c", &ctemp );
      matIndexes[memIndex++] = ctemp;
    }
  }


  /* Print out the 2D Array. */
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      printf("mat[%d][%d] = [%f] ", i,j,mat[i][j] );
    }
    puts("");
  }


  //scanf(" %c%*[^\n]", &dane);
  //**pass(mat, n,m, prof, strictness);
  /*
   * free mat[0] is also okay.
   */
  free(matIndexes);
  free(mat);

  puts("done");
  // system("pause");
  return 0;
}

float **pass(float **mat, int n, int m, int prof, int strictness){
    /* 
     * Your code goes here. 
     * I just left the return NULL so it
     * does not generate an error.
     */

    /*
    mat=(float **)calloc(n, sizeof(float *));
    if(mat==NULL)
    {
        printf("\nWRONG");

    }
    int i;
    for(i=0;i<n;i++)
    {
        mat[i]=(float *)calloc(m, sizeof(float));
    }
    return mat;
    */


    return NULL;
}

方法2(1个malloc):

#include <stdio.h>
#include <stdlib.h>  
#include <stdint.h>  // <- For uintptr_t

float **pass(float **mat, int n, int m, int prof, int strictness);
int main(){
  int n, m, memIndex = 0;
  char dane;
  float ** mat;
  float * matIndexes;
  void * mtemp;
  // Used unsigned char or signed char as approriate.
  unsigned char ctemp;

  puts("Enter n size:");
  scanf(" %d%*[^\n]", &n);

  puts("Enter m size:");
  scanf("%d%*[^\n]", &m);

  // n && m have to be at least 1
  if ( n < 1 || m < 1 ){
    puts("User input value is wrong.");
    return 1;
  }

  // Get enough spaces for both mat and matIndexes
  mtemp = malloc( (sizeof(float*) * n)
                 +(sizeof(float) * (n * m))
                );

  // if mtemp == NULL, fail memory allocation
  if ( mtemp == NULL ) {
    puts("Error when memory location.");
    return 1;
  }

  // Split up the space.
  mat = (float**)mtemp;
  matIndexes = (float *) ((char*)mtemp + n * sizeof(float*));

  // Getting the char part
  for(int i=0;i<n;i++){
    mat[i] = &matIndexes[memIndex];
    for(int j=0;j<m;j++){
      scanf(" %c", &ctemp );
      matIndexes[memIndex++] = ctemp;
    }
  }

  /* Print out the 2D Array. */
  puts("");
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      printf("mat[%d][%d] = [%f] ", i,j,mat[i][j] );
    }
    puts("\n");
  }

  /* Checking to see if the address allocate right matIndexes[0] should be mat + matSize */
  /* The correct cast to see the address is uintptr_t */
  puts("Checking to see address is right: ");
  printf("mat: %zu\n", (uintptr_t) mat);
  printf("matIndexes: %zu\n", (uintptr_t) matIndexes);



  //scanf(" %c%*[^\n]", &dane);
  //**pass(mat, n,m, prof, strictness);
  /*
   * free mat will free everything
   */

  free(mat);

  puts("\nDone!");
  // system("pause");
  return 0;
}

【讨论】:

    猜你喜欢
    • 2012-09-04
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    相关资源
    最近更新 更多