CIS 314 Assignment 3


Rate this product

CIS 314 Assignment 3 – 100/100 points
Please submit individual source files for coding exercises (see naming conventions below) and a
single solution document for non-coding exercises (.txt or .pdf only). Your code and answers
need to be documented to the point that the graders can understand your thought process.
Full credit will not be awarded if sufficient work is not shown.
1. [80] Write a C program with 5 functions:
• (15) int* readArray(int length): allocate an array of ints of size length on the heap using
malloc(), read length ints from the user to fill the array, return the array.
• (20) void swap(int *xp, int *yp): swaps the int values stored at the xp and yp pointers.
• (20) void sortArray(int *array, int length): sorts the input array using Selection Sort
(Google it) by repeatedly calling your swap function as appropriate.
• (15) void printArray(int *array, int length): prints the array (e.g., “[ 1, 3, 5, 7 ]”).
• (10) int main(): read in an int length from the user, call readArray to create an array, call
sortArray to sort it, call printArray to print the result, then free the heap memory used
by the array.
Name your source file sort.c.
2. [20] B&O’H 3.54.
A function with prototype
int decode2(int x, int y, int z);
is compiled into IA32 assembly code. The body of the code is as follows:
x at %ebp+8, y at %ebp+12, z at %ebp+16
1 movl 12(%ebp), %edx
2 subl 16(%ebp), %edx
3 movl %edx, %eax
4 sall $31, %eax
5 sarl $31, %eax
6 imull 8(%ebp), %edx
7 xorl %edx, %eax
Parameters x, y, and z are stored at memory locations with offsets 8, 12, and 16 relative
to the address in register %ebp. The code stores the return value in register %eax.
Write C code for decode2 that will have an effect equivalent to our assembly code.
For example:
printf(“%d\n”, decode2(1, 2, 4));
printf(“%d\n”, decode2(-4, -8, -12));
Also write a main() function to test your function . Name your source file 3-54.c
Zip the source files and solution document (if applicable), name the .zip file <Your Full (e.g.,, and upload the .zip file to Canvas (see
Assignments section for submission link).

Open chat
Need help?
Can we help you?