简体   繁体   English

使用系统调用打印结果

[英]print result using system calls

For my OS class, I need to print out the result of this matrix multiplication using only system calls. 对于我的OS类,我只需要使用系统调用就可以打印出矩阵乘法的结果。 Following my lecture notes, I wrote up this piece of code. 根据我的讲义,我编写了这段代码。 I use : 我用 :

#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);
}

Now, it's printing a just a 14295680 in the console. 现在,它仅在控制台中打印14295680。 The professor gave us a file with machine code and it's printing 332833500, which seems more reasoneable. 教授给了我们一个带有机器代码的文件,它的打印内容为332833500,这似乎更加合理。

Thanks in advance. 提前致谢。

Edit: changed type on the printf call Edit2: fix R[N][N] 编辑:更改printf调用的类型编辑2:修复R [N] [N]

Just replace the sprintf value: 只需替换sprintf值:

sprintf(str,"%lld\n",R[N-1][N-1]); // = 332833500
write(1,str,strlen(str));       

instead of 代替

sprintf(str,"%lld\n",R); // this is a pointer
write(1,str,strlen(str));       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM