Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the code that I figured out but it is not sorting correctly (The BHT++ a

ID: 3885761 • Letter: H

Question

Here is the code that I figured out but it is not sorting correctly (The BHT++ after each line is for another part of the question, you can delete it or just leave it):

#include <stdio.h>

#include <stdlib.h>

void printarray(int a[]){

int i;

for(i = 0; i < 6; i++){

printf("Element %i : %i ", i, a[i]);

}

}

int main(int argc, char *argv[]) {

int t0;

int t1;

int t2;

int t3;

int t5;

int j;

int arraycount = 0;

int loopcount;

int BHTcount1;

int BHTcount2;

int BHTcount3;

int BHTcount4;

int BHTcount5;

int BHTcount6;

int BHTcount7;

int BHTcount8;

int BHTcount9;

int BHTcount10;

int BHTcount11;

int BHTcount12;

int BHTcount13;

int BHTcount14;

int arrayA[6] = {21, 18, 42, 7, 9, 31};

int arrayS[6] = {21, 18, 42, 7, 9, 31};

printarray(arrayA);

//-----------------start of code-------------------

//first assembly instr. not required in c

BHTcount1++;

//put the last value of the array into t0

t0 = arrayS[5];   

BHTcount2++;

OUTERLOOP:

arraycount = 0;

BHTcount3++;

t1 = 0;   // t1 gets assigned 0

BHTcount4++;

INNERLOOP:

t2 = arrayA[arraycount];

printf(" this is t2: %i ", t2); // loading t2 with the starting address of the array (21)

BHTcount5++;

t3 = arrayA[arraycount + 1]; // loading t3 with the starting address of the array + 4 (which is the second element in the array: 18)

BHTcount6++;

BHTcount7++;

if(t2 < t3){ // if t2 < t3 then t5 = 1 else t5 = 0;

t5 = 1;

}

else{

t5 = 0;

}

BHTcount8++;

if(t5 == 0){ // if t5 == 0 then branch to CONTINUE

goto CONTINUE;

}

t1 = 1; // t1 = 1

BHTcount9++;

arrayA[arraycount + 1] = t2; // store t2 back into the 2nd spot in the array (array[1])

BHTcount10++;

arrayA[arraycount] = t3; // store t3 back into the 1st spot in the array (array[0])

BHTcount11++;

loopcount++;

printf("Array %i: ", loopcount);

printarray(arrayA);

printf(" ");

CONTINUE:

arraycount++;

BHTcount12++;

BHTcount13++;

if(arrayA[arraycount] != t0){

goto INNERLOOP;

}

BHTcount14++;

if(t1 != 0){ // if t1 is not equal to 0

goto OUTERLOOP;

}

printf("final array: ");

printarray(arrayA);

}

Transform this assembly code into C code. This code below sorts an array and the starting address of the array is stored in $s0. The array has elements: 121,18,42,7,9,31 add $t0, $50, $0 add $t0, $t0, 20 -stores starting addr of array in t0 -20/4 bytes per element space = 5 element spaces (starting address + 5 -element spaces address of the last element in the array) outterLoop: add $a0, $s0, $e add $t1, $0, $0 -stores the starting addr of array in a0 innerLoop: lw $t2, 0($a0) lw $t3, 4($a0) slt $t5, $t2, $t3 beq $t5, $0, continue addi $t1, $0, 1 sw $t2, 4($a0) sw $t3, 0($a0) t2 = array[addr+0] --t3 array addr+41 --if (t2

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>
// sorts the data in descending order
void printarray(int a[]){
int i;
for(i = 0; i < 6; i++){
printf("Element %i : %i ", i, a[i]);
}
}
int main(int argc, char *argv[]) {
int *t0; // pointer which stores the address
int t1;
int t2;
int t3;
int t5;
int arraycount = 0;
int loopcount=0;
int arrayA[6] = {21, 18, 42, 7, 9, 31};
int arrayS[6] = {21, 18, 42, 7, 9, 31};
printarray(arrayA);
//-----------------start of code-------------------
//put the last value of the array into t0
t0 =&arrayA[5];//store the address of last element in t0   
OUTERLOOP:
arraycount = 0;
t1 = 0; // t1 gets assigned 0
INNERLOOP:
t2 = arrayA[arraycount];
printf(" this is t2: %i ", t2); // loading t2 with the starting address of the array (21)
t3 = arrayA[arraycount + 1];
// loading t3 with the starting address of the array + 4 (which is the second element in the array: 18)
if(t2 < t3){ // if t2 < t3 then t5 = 1 else t5 = 0;
t5 = 1;
}
else{
t5 = 0;
}
if(t5 == 0){ // if t5 == 0 then branch to CONTINUE
goto CONTINUE;
}
t1 = 1; // t1 = 1
arrayA[arraycount + 1] = t2; // store t2 back into the 2nd spot in the array (array[1])
arrayA[arraycount] = t3; // store t3 back into the 1st spot in the array (array[0])
loopcount++;
printf("Array %i: ", loopcount);
printarray(arrayA);
printf(" ");
CONTINUE:
arraycount++;
//compare the addresses not the elements
if((arrayA+arraycount) != t0){

goto INNERLOOP;
}
if(t1 != 0){ // if t1 is not equal to 0
goto OUTERLOOP;
}
printf("final array: ");
printarray(arrayA);
}

Output

Element 0 : 21
Element 1 : 18
Element 2 : 42
Element 3 : 7
Element 4 : 9
Element 5 : 31

this is t2: 21
Arraycount0

this is t2: 18
Arraycount1
Array 1:
Element 0 : 21
Element 1 : 42
Element 2 : 18
Element 3 : 7
Element 4 : 9
Element 5 : 31

this is t2: 18
Arraycount2

this is t2: 7
Arraycount3
Array 2:
Element 0 : 21
Element 1 : 42
Element 2 : 18
Element 3 : 9
Element 4 : 7
Element 5 : 31

this is t2: 7
Arraycount4
Array 3:
Element 0 : 21
Element 1 : 42
Element 2 : 18
Element 3 : 9
Element 4 : 31
Element 5 : 7

this is t2: 21
Arraycount0
Array 4:
Element 0 : 42
Element 1 : 21
Element 2 : 18
Element 3 : 9
Element 4 : 31
Element 5 : 7

this is t2: 21
Arraycount1

this is t2: 18
Arraycount2

this is t2: 9
Arraycount3
Array 5:
Element 0 : 42
Element 1 : 21
Element 2 : 18
Element 3 : 31
Element 4 : 9
Element 5 : 7

this is t2: 9
Arraycount4

this is t2: 42
Arraycount0

this is t2: 21
Arraycount1

this is t2: 18
Arraycount2
Array 6:
Element 0 : 42
Element 1 : 21
Element 2 : 31
Element 3 : 18
Element 4 : 9
Element 5 : 7

this is t2: 18
Arraycount3

this is t2: 9
Arraycount4

this is t2: 42
Arraycount0

this is t2: 21
Arraycount1
Array 7:
Element 0 : 42
Element 1 : 31
Element 2 : 21
Element 3 : 18
Element 4 : 9
Element 5 : 7

this is t2: 21
Arraycount2

this is t2: 18
Arraycount3

this is t2: 9
Arraycount4

this is t2: 42
Arraycount0

this is t2: 31
Arraycount1

this is t2: 21
Arraycount2

this is t2: 18
Arraycount3

this is t2: 9
Arraycount4
final array:
Element 0 : 42
Element 1 : 31
Element 2 : 21
Element 3 : 18
Element 4 : 9
Element 5 : 7