首先,我必须输入二维数组的大小(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 Z
N Z
N Z
输出
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
发布于 2019-12-08 08:44:46
对于这个答案,我不知道你要用浮点数**来做什么。不过,我只是向您展示了如何分配char并将其转换为char。字符值是带符号的还是不带符号的,这由您决定。
用于分配内存。有两种方法。最简单的一点是,您只需调用malloc两次即可。为指向float的指针的指针分配空间。然后为指针分配内存以使其浮动。基本上,指向float的指针是2D矩阵的所有索引,而mat是指向它们的指针数组。当涉及到3D时,它是不同的,但现在是这样的。
第二种方法有点难,但是第一种方法很容易理解。第二种方法是在一个malloc中为指向指针的指针和每个浮点指针分配足够的内存。该页存储器的第一个字节地址是指向指针的指针的第一个索引。而第一地址+指向指针的指针的总大小是要浮动的指针的开始。
很难用语言来解释这一点,但我希望代码能有所帮助。看过第一种方法之后,就很容易理解第二种方法了。我把调试代码留在那里,这样你就更容易理解它们是如何工作的。
方法1(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;
}https://stackoverflow.com/questions/59213995
复制相似问题