COMP1036 Coursework Part IIC/C++

Java Python COMP1036 Coursework Part II (25 marks)

Tasks

Write a program in Hack Assembly Language that sorts an array of integers in ascending or descending order. The unsorted array contains 5 or more elements, located at a range of memory locations starting from RAM[50]. The integers in the array can be positive, negative, or zero.

The program should allow you to sort either the entire array or a portion of it. The number of elements to be sorted is determined by the integers stored in RAM[0] and RAM[1], as described below:

Read two input values,X andY, from RAM[0] and RAM[1], respectively, and output the computed result, Z, to RAM[2]. X and Y can be positive or negative. The program should function correctly regardless of whether X < Y, X > Y, or X = Y.

Different rules will be applied based on whether X and Y are even or odd integers, as follows:

(1)      IF both X and Y are even integers, THEN Z is the sum of all even integers between X and Y (inclusive).

(2)      IF both X and Y are odd integers, THEN Z is the sum of all odd integers between X and Y (inclusive).

(3)      IF one of X or Y is odd and the other is even, THEN Z is the sum of all integers between X and Y (inclusive).

(4)      IF X = Y, THEN Z = X or Z = Y.

(5)      IF Z is positive, THEN sort the array in ascending order.

(6)      IF Z is negative, THEN sort the array in descending order.

(7)      IF Z is zero, THEN no sorting should be done.

Example 1:

Given RAM[0] = X = -4; RAM[1] = Y = 2

The range of integers between X and Y (inclusive) is [-4, -3, -2, -1, 0, 1, 2].

Applying Rule (1): RAM[2] = Z = (-4) + (-2) + 0 + 2 = -4.

Applying Rule (6) for Z = -4: Sort the first 4 elements of the array in descending order.

Example 2:

Given RAM[0] = X = -5; RAM[1] = Y = 5

The range of integers between X and Y (inclusive) is [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5].

Applying Rule (2): RAM[2] = Z = (-5) + (-3) + (-1) + 1 + 3 + 5 = 0.

Applying Rule (7) for Z = 0: No sorting should be done.

Example 3:

Given RAM[0] = X = 2; RAM[1] = Y = 3

The range of integers between X and Y (inclusive) is [2, 3].

Applying Rule (3): RAM[2] = Z = 2 + 3 = 5.

Applying Rule (5) for Z = 5: Sort the first 5 elements of the array in ascending order.

Example 4:

Given RAM[0] = X = 3; RAM[1] = Y = 3

The range of integers between X and Y (inclusive) is [3].

Applying Rule (4): RAM[2] = Z = 3.

Applying Rule (5) for Z = 3: Sort the first 3 elements of the array in ascending order.

Some Input and Output Examples:

In

Out

In

Out

In

Out

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值