使用系统调用打印结果
print result using system calls
对于我的 OS class,我需要仅使用系统调用打印出该矩阵乘法的结果。根据我的讲义,我写了这段代码。我使用:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1000
// Matrix
long long int A[N][N],B[N][N],R[N][N];
int main(int argc, char *argv[])
{
int x,y,z;
char str[100];
/* Matrix inicialization */
for(y=0;y<N;y++)
for(x=0;x<N;x++)
{
A[y][x]=x;
B[y][x]=y;
R[y][x]=0;
}
/* Matrix multiplication */
for(y=0;y<N;y++)
for(z=0;z<N;z++)
for(x=0;x<N;x++)
{
R[y][x]+= A[y][z] * B[z][x];
}
//System calls for printing the result
sprintf(str,"%lld\n",R);
write(1,str,strlen(str));
exit(0);
}
现在,它在控制台中只打印了一个 14295680。教授给了我们一个机器码文件,打印出来的是332833500,看起来比较合理
提前致谢。
编辑:更改了 printf 调用的类型
Edit2:修复 R[N][N]
只需替换 sprintf 值:
sprintf(str,"%lld\n",R[N-1][N-1]); // = 332833500
write(1,str,strlen(str));
而不是
sprintf(str,"%lld\n",R); // this is a pointer
write(1,str,strlen(str));
对于我的 OS class,我需要仅使用系统调用打印出该矩阵乘法的结果。根据我的讲义,我写了这段代码。我使用:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 1000
// Matrix
long long int A[N][N],B[N][N],R[N][N];
int main(int argc, char *argv[])
{
int x,y,z;
char str[100];
/* Matrix inicialization */
for(y=0;y<N;y++)
for(x=0;x<N;x++)
{
A[y][x]=x;
B[y][x]=y;
R[y][x]=0;
}
/* Matrix multiplication */
for(y=0;y<N;y++)
for(z=0;z<N;z++)
for(x=0;x<N;x++)
{
R[y][x]+= A[y][z] * B[z][x];
}
//System calls for printing the result
sprintf(str,"%lld\n",R);
write(1,str,strlen(str));
exit(0);
}
现在,它在控制台中只打印了一个 14295680。教授给了我们一个机器码文件,打印出来的是332833500,看起来比较合理
提前致谢。
编辑:更改了 printf 调用的类型 Edit2:修复 R[N][N]
只需替换 sprintf 值:
sprintf(str,"%lld\n",R[N-1][N-1]); // = 332833500
write(1,str,strlen(str));
而不是
sprintf(str,"%lld\n",R); // this is a pointer
write(1,str,strlen(str));