2009 U1 Q2 Part C

Part i

The instruction Set is the set of machine code instructions that a CPU is designed to understand/process

The Instruction format is  the layout convention chosen to represent instruction types and their corresponding operands for a given instruction set.

Part ii

Any three: ADD, LOAD, STORE, JUMP, JUMPIF, IN, COMPARE

Part iii

OPCODEOperand 1Operand 2

We could reserve 4 bits for the opcode which will facilitate a total of 24  or 16 instructions.

The remaining 12 bits can be  split, using 6 bits for operand 1 an 6 bits for operand 2 .

Part iv

Recall: Direct addressing means that the address of the operand is stored in the instruction

Fetch –

  • Copy instruction from memory , which is specified by the PC,  to the  CIR (Current Instruction Register).
  • increment the PC (Program Counter)

Decode –

  • Determine operands and operation.
    •  Since Direct addressing is used, the MAR will store the address of the operand which was decoded and copied from the CIR.
    • The Data from Memory stored at the location specified by the MAR is copied to the MDR (Memory Data Register)

Execute –

  • Control is passed to the appropriate logic circuit based on the operation opcode. and the results of the operation on the data stored in the MDR, is stored in the AC (accumulator)

© 2021  Vedesh Kungebeharry. All rights reserved. 

CAPE 2012 U1 Q3 Solution

2012 U1 Q3

Part A

Algorithms are used to solve computational problems and are a technique for precisely documenting the solution.

Part B


i) Bounded, since the number of iterations are constant every time the algorithm is applied. In this case, the loop is executed 16 times.

ii) Unbounded, since the number of iterations is not set and is dependent on the value of a sentinel variable, in this case X     

Part C) 

START
rcount, bcount,gcount,ncount <--0
for i<-- 1 to 150 do
	input vote
	if (vote = "red") then
		rcount++
	else if (vote = "green") then
		gcount++
	else if (vote = "blue") then
		bcount++
	else
		ncount++
	endif
endfor

output "total votes for color  = " + rcount+gcount+bcount
output rcount " voted for red"
output bcount " voted for blue"
output gcount " voted for green"
END

Part d

(Flowgorithm file here: https://drive.google.com/drive/folders/1pazPgOeE2OA75OPIpUScROX302NyBhrX?usp=drive_link)

Alternatively, in Pseudocode:

© 2020  Vedesh Kungebeharry. All rights reserved.

Bounded Iteration Example

Task

Create an algorithm in flowchart AND pseudocode to accomplish the following task:

“output the squares of all numbers from 2 to 75 inclusive”.

Flowchart Solution

Flowchart
Solution as expected by CXC CSEC and NEC

The solution was also generated in Flowgorithm and is shown below:

Flowchart - 2

Similar solution generated in Flowgorithm

Pseudocode

The solution in PSEUDOCODE is shown below:

Start
For i = 2 to 75
Output i , ” Squared is ” , i * i
EndFor
End

BONUS: Coded solutions!

Pascal

The pascal code for the above algorithm is shown below:

Pascal Code

Solution in bloodshed Dev Pascal

Copy-able code:
program Squares;
uses
crt;
var
i:integer;
begin
for i:=2 to 75 do
writeln (i, ‘ squared is ‘ , i*i);
writeln();
writeln(‘Press any key to continue…’);
readkey();
end.

C# Solution

The following is a solution in C#:

C# Code

Solution created using Visual Studio Community Edition

See here for instructions on setting up Visual Studio to start using C#

Copy-able code:
using System;
namespace Squares
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 75; i++)
Console.WriteLine(i + ” squared is ” + (i * i));
//pause execution here
Console.WriteLine(“press any key to continue….”);
Console.ReadKey();
}
}
}
© 2018 Vedesh Kungebeharry. All rights reserved

Swap Two Integers

A student reproduced a programming question for me today:

Write a program which accepts 2 integers separated by a space. If the first integer is less than the second integer, output a message stating this fact e.g “The first number is smaller”, else swap the values of both variables which contain the integers respectively.

Before the program exits, print both integers respectively.

Analysis

A flowchart for the solution is shown below. Note that, input is obtained on separate lines, a limitation of the software used to create the chart, flowgorithm:

Swap - Main

Solution 1

Below is a suggested solution written in pascal code:

Swapping 2 variables

Copy-able pascal code here:


program swap;
uses crt;
var
num1,num2,temp:integer;

begin

writeln(‘Please enter 2 unequal integers separated by a space…’) ;
//read data separated by a space
readln (num1,num2);

if (num1>num2) then //we swap the numbers using a tempoary varible
begin
temp:=num1;
num1:=num2;
num2:=temp;
end //notice that there is no semicolon on this line.
else//print message
writeln(‘num1 is less than num2’);

writeln();
writeln(num1,’ ‘,num2) ;

writeln(‘Press any key to exit…’);
readkey;
end.


Solution 2

Below is a suggested solution written in c# code (generated by flowgorithm and edited to accept input of the two integers on one line):

Swap - C# Code

Copy-able c# code here:


using System;

public class Program
{
public static void Main()
{

int num1, num2, temp;
string anykey;

Console.WriteLine(“Please enter 2 unequal integers separated by a space…”);

//Read line, and split it by whitespace into an array of strings
string[] tokens = Console.ReadLine().Split();
//Parse element 0
num1 = int.Parse(tokens[0]);
//Parse element 1
num2 = int.Parse(tokens[1]);

if (num1 > num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}
else
{
Console.WriteLine(“num1 is less than num2” + “\n”);
}
Console.WriteLine(num1 + ” ” + num2 + “\n”);
Console.WriteLine(“Press any key to exit.” + “\n”);
anykey = Console.ReadLine();
}
}
© 2018 Vedesh Kungebeharry. All rights reserved