Thursday, September 5, 2019

CS2100: Tutorial 2

Tutorial 1
1.
int readArray(int arr[], int limit){ 
    int input = 0;
    for(int i = 0 ;i < limit ; i++){
     scanf("%d", &input );
    if(input> = 0)
     arr[i] = input;
     else
     break;
    }
}


void reverseArray(int arr[], int size)
{
    int temp , i , j
    i =0;
    j = size - 1;
   while (i<j){
     temp = arr[i]
     arr[i] = arr [ j]
     arr[j] = temp;
     i++;
     j --;

   }

}

c)
i) Recursion
ii) To make it easier, make it nice nice,
 "Make people less stupid" - Joshua wee shing hao

d)
1. if (size< = 1) return;
else
2. temp = arr[0]
arr[0] = arr[size - 1]
arr[size-1] = temp;
rev(arr+1,size - 2)

Q2.
int has 4 bytes and double is 8 bytes

Q3.
a)
8 boxes in consequitive with the first and 5th box being num and dec
b)
Swap by value:

Won't change the address nor swap the memory
c)
temp = *a
*a = *b
* b=temp
Will swap

// this is true swap

Q4.
mult $s1, $s2
mflo $to
add $s3, $s0, $t0  #d = a+b*
addi $t0 , $0 , 3 #store 3 into the register
DIV $s3, $t0 #d/3
MFLO $s4


Division
lo -> Quotient
Hi -> remainder

Multiplication
Lo -> Lower 32 bits
Hi -> Upper 32 bits
(Sometimes when we multiply may overshot 32 bits so there's two registers to handle it)

No comments:

Post a Comment