Question: Part 2: Struct Array Selection Sort Part 2 requires that you expand your example to sort an array of structs based on the value of

Part 2: Struct Array Selection Sort

Part 2 requires that you expand your example to sort an array of structs based on the value of one of the struct elements. A complete program in C is provided in structSort.c.

1. Compile and execute the C program so you understand what the behavior looks like.

2. Examine the program so you understand the code.

3. Translate the selectionsort, swap and printarray functions in structsort.c to RISC-V assembly using structsort.og as a starting point.

4. The provided structsort.og contains the c code as comments, labels, and other helpful comments to help you structure your code.

5. Rename structsort.og to structsort.asm

6. Do not modify structmain.asm

7. You can modify structmain.asm for debug and testing purposes.

8. Your code in structsort.asm must not depend on any edits you made to structmain.asm for proper operation.

9. Your code must be in structsort.asm.

Below is the code for structSort.c

#include

#define SIZE 10

typedef struct Node studentNode;

void swap(studentNode *arr, int i, int j);

void selectionSort(studentNode *arr, int i, int n);

void printArray(studentNode *arr, int n);

struct Node {

char name[6];

int studentid;

int coursenum;

};

studentNode studentlist[SIZE] = {

{"Dougy", 13, 2122},

{"Timmy", 15, 2122},

{"Emily", 18, 2123},

{"Jimmy", 14, 2120},

{"Kimmy", 11, 2123},

{"Carlo", 19, 2123},

{"Vicky", 22, 2120},

{"Anton", 12, 2322},

{"Brady", 10, 2120},

{"Sonya", 16, 2123}

};

int main()

{

int n = SIZE;

selectionSort(studentlist, 0, n);

printArray(studentlist, n);

return 0;

}

/* Utility function to swap values at two indices in an array*/

void swap(studentNode arr[], int i, int j)

{

studentNode temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

/* Recursive function to perform selection sort on subarray `arr[in-1]` */

void selectionSort(studentNode arr[], int i, int n)

{

/* find the minimum element in the unsorted subarray `[in-1]`

// and swap it with `arr[i]` */

int j;

int min = i;

for (j = i + 1; j < n; j++)

{

/* if `arr[j]` is less, then it is the new minimum */

if (arr[j].studentid < arr[min].studentid) {

min = j; /* update the index of minimum element */

}

}

/* swap the minimum element in subarray `arr[in-1]` with `arr[i] */

swap(arr, min, i);

if (i + 1 < n) {

selectionSort(arr, i + 1, n);

}

}

/* Function to print `n` elements of array `arr` */

void printArray(studentNode arr[], int n)

{

int i;

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

printf("%d ", arr[i].studentid);

printf("%s ", arr[i].name);

printf("%d ", arr[i].coursenum);

}

}

Below is the code for structmain.asm

.globl main

.data

#studentNode studentlist[SIZE] = { # {"Dougy", 13, 2122}, # {"Timmy", 15, 2122}, # {"Emily", 18, 2123}, # {"Jimmy", 14, 2120}, # {"Kimmy", 11, 2123}, # {"Carlo", 19, 2123}, # {"Vicky", 22, 2120}, # {"Anton", 12, 2322}, # {"Brady", 10, 2120}, # {"Sonya", 16, 2123} # }; studentlist: .string "Dougy" .word 13 .word 2122 .string "Timmy" .word 15 .word 2122 .string "Emily" .word 18 .word 2123 .string "Jimmy" .word 14 .word 2120 .string "Kimmy" .word 11 .word 2123 .string "Carlo" .word 19 .word 2123 .string "Vicky" .word 22 .word 2120 .string "Anton" .word 12 .word 2322 .string "Brady" .word 10 .word 2120 .string "Sonya" .word 16 .word 2123

.text

#int main() { main: # int n = SIZE;

# selectionSort(studentlist, 0, n); la a0, studentlist mv a1, zero li a2, 10 jal selectionSort # printArray(studentlist, n); la a0, studentlist li a1, 10 jal printArray # return 0; li a7, 10 ecall #}

Below is structsort.asm

.globl swap .globl selectionSort .globl printArray

#struct def'n for reference #struct studentNode { # char name[6]; # int studentid; # int coursenum; #};

#/* Recursive function to perform selection sort on subarray `arr[in-1]` */ #void selectionSort(studentNode arr[], int i, int n) { selectionSort: #callee setup goes here # /* find the minimum element in the unsorted subarray `[in-1]` # // and swap it with `arr[i]` */ # int j; # int min = i;

# for (j = i + 1; j < n; j++) { for1:

forloop1:

# /* if `arr[j]` is less, then it is the new minimum */ # if (arr[j].studentid < arr[min].studentid) { if1:

# min = j; /* update the index of minimum element */

# } endif1:

endfor1:

# } # /* swap the minimum element in subarray `arr[in-1]` with `arr[i] */ #caller setup goes here

# swap(arr, min, i);

#caller teardown goes here (if needed) # if (i + 1 < n) { if2:

#caller setup goes here

# selectionSort(arr, i + 1, n);

#caller teardown goes here (if needed)

# } endif2: #callee teardown goes here (if needed)

#} #/* Function to print `n` elements of array `arr` */ #void printArray(studentNode arr[], int n) { printArray: #callee setup goes here

# int i;

# for (i = 0; i < n; i++) { for2:

forloop2:

#use ecalls to implement printf # printf("%d ", arr[i].studentid);

# printf("%s ", arr[i].name);

# printf("%d ", arr[i].coursenum);

# } endfor2:

#caller teardown goes here

#}

#/* Utility function to swap values at two indices in an array*/ #void swap(studentNode arr[], int i, int j) { swap: #caller setup goes here

# studentNode temp = arr[i];

# arr[i] = arr[j];

# arr[j] = temp;

#caller teardown goes here

#}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!