如何连接Matrix的元素? [英] How to concatenate Matrix's elements ?

查看:61
本文介绍了如何连接Matrix的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有没有办法用简单的printf打印矩阵的元素?

我想要的结果是例如:

如果mat = {0,1,2,3}

结果必须是:0123。


我试过这个代码:


/ *********************************** *************** ************* /

#include< stdio.h>

#include< stdlib.h>

#include< string.h>


#define size 10

int main(无效)

{

//声明

int i = 0;

int mat [size] = {0,0,0,0};

char convert [size] = {0,0,0,0};


for(i = 0; i< 4; i ++)

{

mat [i] = mat [i] + i + 1;

printf(" mat [%d] =%d \ n",i,mat [i]);


//将int转换为字符串

sprintf(转换,"%s",(char *)& mat [i]);

//连接矩阵的元素

// strcat(转换[i + 1],con vert [i]);


}


printf(" Matrix等于%s \ n",(char *) &转换);


返回(0);

}


/ ****** ******************************************** ****** ******* /


执行后我得到这个

$ ./matrix

mat [0] = 1

垫[1] = 2

垫[2] = 3

垫[3] = 4

矩阵等于?


如果我激活该行:

strcat(转换[i + 1],转换[i]);

我得到

matrix.c:在函数中?main?:

matrix.c:23:警告:传递?strcat的参数1?在没有投射的情况下从整数制作指针



matrix.c:23:警告:传递?strcat的参数2?在没有演员的情况下从整数制作指针



执行时:

./matrix

mat [0] = 1

分段错误。

解决方案

./ matrix

mat [0] = 1

垫[1] = 2

垫[2] = 3

垫[3] = 4

矩阵等于?


如果我激活该行:

strcat(转换[i + 1],转换[i]) ;

我得到

matrix.c:在函数中?main?:

matrix.c:23:警告:传递参数1? strcat的?在没有投射的情况下从整数制作指针



matrix.c:23:警告:传递?strcat的参数2?在没有演员的情况下从整数制作指针



执行时:

./matrix

mat [0] = 1

分段错误。


Nezhate说:


大家好,

有没有办法用简单的printf打印矩阵的元素?



是。


我想要的结果是例如:

如果mat = {0,1,2,3}

结果必须是:0123。



#include< stdio.h>


int main(无效)

{

int mat = {0,1,2,3,};

size_t len = sizeof mat / sizeof mat [0];

size_t idx = 0;

while(idx< len)

{

printf("%d",mat [idx ++]) ;

}

putchar(''\ n'');

返回0;

}


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http: //万维网。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


2008年7月21日星期一15:49:51 +0530,Nezhate< ma ************ @ gmail.com>

写道:


int mat [size] = {0,0,0,0} ;

char convert [size] = {0,0,0,0};



因为看起来你想要输出为字符串,你需要有大小+

1个元素(对于\0) 。如果你想将聚合初始化为0,

这就足够了:

int mat [size] = {0};

char convert [size] ="" ;;

以空字符串开头。


for(i = 0; i< 4; i ++)



为什么你认为你宣布了SIZE? (不要使用魔法数字)


sprintf(convert,"%s",(char *)& mat [i]) ;



mat [i]是一个int。 & mat [i]是指向int的指针。你将它投射到char

*并要求sprintf假设它是一个字符串(以null结尾的数组

字符)。

< blockquote class =post_quotes>
//连接矩阵的元素

// strcat(convert [i + 1],convert [i]);



convert [i + 1]是一个字符;所以是转换[i]。 strcat需要char *和

const char *。

strcat在sprintf之后?你的逻辑很复杂。


难怪你的代码无效。


#include< stdio.h>

#include< string.h>


#define SIZE 4

int

main( void){

int mat [SIZE] = {0,1,2,3};

char convert [SIZE + 1];

int count;

for(count = 1; count< SIZE; count ++){

sprintf(convert + count,"%d",mat [count] );

}


printf(矩阵是%s \ n,转换);

返回0 ;

}



-

没有什么值得拥有的。


- 发表于新闻://freenews.netfront.net - 对 ne**@netfront.net 的投诉 -


Hi all,
Is there any way to print a matrix''s elements using a simple printf ?
what I want as result is for example:
if mat ={0,1,2,3}
result must be: "0123".

I tried this code:

/************************************************** *************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define size 10

int main (void)
{
// Declaration
int i = 0;
int mat[size]= {0,0,0,0};
char convert[size]= {0,0,0,0};

for (i=0;i<4;i++)
{
mat[i]=mat[i]+i+1;
printf("mat[%d]=%d\n",i,mat[i]);

// Convert int to string
sprintf(convert,"%s",(char *)&mat[i]);
// concatenate matrix''s elements
// strcat(convert[i+1],convert[i]);

}

printf ("Matrix is equal to %s\n",(char *)&convert);

return (0);
}

/************************************************** *************/

After execution I get this
$ ./matrix
mat[0]=1
mat[1]=2
mat[2]=3
mat[3]=4
Matrix is equal to 

If I activate the line :
strcat(convert[i+1],convert[i]);
I get
matrix.c: In function ?main?:
matrix.c:23: warning: passing argument 1 of ?strcat? makes pointer
from integer without a cast
matrix.c:23: warning: passing argument 2 of ?strcat? makes pointer
from integer without a cast

And when executing :
./matrix
mat[0]=1
Segmentation fault.

解决方案

./matrix
mat[0]=1
mat[1]=2
mat[2]=3
mat[3]=4
Matrix is equal to 

If I activate the line :
strcat(convert[i+1],convert[i]);
I get
matrix.c: In function ?main?:
matrix.c:23: warning: passing argument 1 of ?strcat? makes pointer
from integer without a cast
matrix.c:23: warning: passing argument 2 of ?strcat? makes pointer
from integer without a cast

And when executing :
./matrix
mat[0]=1
Segmentation fault.


Nezhate said:

Hi all,
Is there any way to print a matrix''s elements using a simple printf ?

Yes.

what I want as result is for example:
if mat ={0,1,2,3}
result must be: "0123".

#include <stdio.h>

int main(void)
{
int mat = { 0, 1, 2, 3, };
size_t len = sizeof mat / sizeof mat[0];
size_t idx = 0;
while(idx < len)
{
printf("%d", mat[idx++]);
}
putchar(''\n'');
return 0;
}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Mon, 21 Jul 2008 15:49:51 +0530, Nezhate <ma************@gmail.com>
wrote:

int mat[size]= {0,0,0,0};
char convert[size]= {0,0,0,0};

Since it looks like you want the output as string, you need to have size +
1 elements (for the \0). If you want the aggregate to be initialize to 0,
this would suffice:
int mat[size] = {0};
char convert[size] = "";
Start with an empty string.

for (i=0;i<4;i++)

Why do you think you declared SIZE? (Don''t use magic numbers)

sprintf(convert,"%s",(char *)&mat[i]);

mat[i] is an int. &mat[i] is pointer to an int. You are casting it to char
* and asking sprintf to assume it is a string(null-terminated array of
characters).

// concatenate matrix''s elements
// strcat(convert[i+1],convert[i]);

convert[i+1] is a character; so is convert[i]. strcat requires char * and
const char *.
strcat after sprintf? Your logic is convoluted.

No wonder your code is not working.

#include <stdio.h>
#include <string.h>

#define SIZE 4

int
main (void) {
int mat[SIZE] = {0, 1, 2, 3};
char convert[SIZE + 1];
int count;
for (count = 1; count < SIZE; count++) {
sprintf (convert + count, "%d", mat[count]);
}

printf ("The matrix is %s\n", convert);
return 0;
}



--
Nothing worth having comes easy.

-- Posted on news://freenews.netfront.net - Complaints to ne**@netfront.net --


这篇关于如何连接Matrix的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆