Maxwell's Daemon
Maxwell's Daemon

Reputation: 631

print result using system calls

For my OS class, I need to print out the result of this matrix multiplication using only system calls. 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. The professor gave us a file with machine code and it's printing 332833500, which seems more reasoneable.

Thanks in advance.

Edit: changed type on the printf call Edit2: fix R[N][N]

Upvotes: 0

Views: 317

Answers (1)

J. Piquard
J. Piquard

Reputation: 1663

Just replace the sprintf value:

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

Upvotes: 1

Related Questions