2017 U2 Q5

Part a)

Client server

Part b)

  1. The user clicks on a link to a webpage from a previous webpage.
  2. The browser resolves the ip address from the server name and sets up a tcp connection to the server (usually on port 80 or 8080)
  3. The browser sends a http request to the server containing the requested resource and information about itself, e.g browser type, supported language etc
  4. The server generates a response which contains header information as well as HTML code for the browser to render.
  5. The browser interprets the code and displays the page to the user.

Part c)

The url https://www.buythings.com  would be preferred since this http protocol includes SSL (Secure sockets layer) or TLS (Transport layer security) which encrypts all HTTP information such that only the sender and the receiver can decipher the information. This is more secure, especially in the case where the http information can be intercepted by malicious entities on the network, they won’t be able to read the sensitive payment information that can be used defraud the online shopper.

Part d)

A process is a running program.

The PCB stores metadata necessary for  managing the process, e.g. process id, process state, memory allocation addresses, scheduling info (priority, time slice), pointers to it’s resources (files, other processes).

Part e)

  1. New
  2. Running
  3. Ready
  4. Waiting
  • Terminated

Part f)

Paging is were each process in an OS is allocated memory in units called pages for easier coordination and management by the operating system. The pages can be stored physically in memory , or stored on the hard disk (in the case that the process is not running at that time). If a process is stored on the disk and needs to be run, a page fault is generated so that the process is swapped back into the physical memory for processing.

Part g)

Thrashing occurs when memory resources become limited by the amount of running processes. The need for physical memory can be exceeded, thus multiple processes that are in waiting state are stored as pages on disk. For continued running of the system, processes are constantly swapped in and out of physical memory, an execution which spends a lot of time on the cpu when compared to running the processes themselves.

Part h)

  1. Because of the frequent process swapping by page faults, the system becomes inefficient and can run slowly.  (The solution is to inefficiency increase the size of physical memory by upgrading the ram size, or configure/manage the system to run less processes.)

  2. The system can become overwhelmed to the point of “crashing”, i.e stop working or responding altogether.

Part i)

Too little physical memory is available for a system that needs to run a lot of processes.


© 2023  Vedesh Kungebeharry. All rights reserved. 

2022 Jan P02 IT CSEC Q3a

Start
    // Initialization of variables....
    price = -999.99
    totalPrice = -999.99
    discountAmount = -999.99
    discountRate = 0.25
    quantity = -999
    
    // Prompt the user for input....
    output "Input Price"
    input price
    output "Input quantity"
    input quantity
    totalPrice = price * quantity
    
    // Determine the discount....
    if totalPrice > 1800 then
        discountAmount = totalPrice * discountRate
    else
        discountAmount = 0.00
    end If
    
    // output results....
    output "the Discount is " + discountAmount
Stop

Alternate solution

Note: In this solution , our selection statement does not contain an else branch, since the discountAmount is initialized to 0.

Start
    // Initialization of variables....
    price = -999.99
    totalPrice = -999.99
    
    // here, discount is set to 0.
    discountAmount = 0
    discountRate = 0.25
    quantity = -999
    
    // Prompt the user for input....
    output "Input Price"
    input price
    output "Input quantity"
    input quantity
    totalPrice = price * quantity
    
    // Determine the discount....
    if totalPrice > 1800 then
        discountAmount = totalPrice * discountRate
    else
        
        // because the discount was initialized to 0, we don't need an else section for our selection construct.
    end If
    
    // output results....
    output "the Discount is " + discountAmount
Stop

Additional resources

See flowcharts and solution in flowgorithm here:

https://drive.google.com/drive/folders/1pLQq5N6fqzs7wlFdUaehraZxkhGv2oke?usp=sharing

© 2022  Vedesh Kungebeharry. All rights reserved. 

2019 U1 Q5

Part A

i) Debugging is the process of detecting removing and/or correcting errors in code.

ii) Coded is indented every time a control structure is entered. i.e.  thus isolating blocks of logic which is easier to read and understand.

Comments are used to explain what the code is intended to accomplish, thus making it easier understand and modify

Descriptive variable names explains the purpose of each variable making code easier to understand and maintain.

Part B

//this is a complete program, but starting from main() would be sufficient.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main()
{
    //Start:declare and initialize all variables
    double redTime     =   -999.99;
    double greenTime   =   -999.99;
    double orangeTime  =   -999.99;
    bool valid=false;//used to signify that no
                    //valid inpuha been enterd
                    //at the start of execution

    //End:declare and initialize all variables

    //while input is not valid
    while(valid==false)
    {
        //ASSUMPTION: the usere only enters positive values
        //get input from user
        puts("Please enter a value for Redtime");
        scanf("%lf",&redTime);
        puts("Please enter a value for greenTime");
        scanf("%lf",&greenTime);
        puts("Please enter a value for orangeTime");
        scanf("%lf",&orangeTime);
        //evaluate each condition, and check that they are all true.
        valid = (redTime>=greenTime+orangeTime)&&
                (orangeTime>=5)&&
                (greenTime>4*orangeTime);

        //if input is invalid
        if(valid==false)
        {
            //prompt user
            puts("Invalid timings: Please re-enter\n");
        }
        //continue looping if valid == false
    }
    //if we have exited the loop, then timings are valid
    puts("Timings are valid\n");
    return 0;
}


Part C(i)

    /*Code presented in question
    int i j;
    for (i=1;<= 3;i++)
        for (j=1;j<i;j=j+1)
            printf("%d" j);
        printf(\n);
    */

//Corrected code
    int i, j;  //comma was missing between i and j
    for (i=1;i<= 3;i++)//no left operand for comparison, "i" dentifier missing
        for (j=1;j<i;j=j+1)
            printf("%d", j);//comma missing between format string and parameter
        printf("\n");  //String was not in quotes

/*Note that this code seems to contain a logical error , 
as implied by the indentation of the code.  
It seems that the 2nd for loop and the printf statement are 
2 lines of code that are meant to be executed within the first for loop, 
and should be enclosed in curly braces. 
This question asks us to correct the syntax errors only. 
One may feel the need to fix the logical error also,  
but it is not required.  */


Part C(ii)

Answer:

112

Note, if you assumed a logical error and fixed it, the output would be

1

12

Based on the code:

#include <stdio.h>

int main() {
        //Corrected code
    int i, j;  //comma was missing between i and j
    for (i=1;i<= 3;i++)//no left operand for comparison, "i" dentifier missing
    {    
        for (j=1;j<i;j=j+1)
            printf("%d", j);//comma missing between format string and parameter
        printf("\n");  //String was not in quotes
    }
    
    return 0;
}


This fix however, would render your response incorrect.

© 2021  Vedesh Kungebeharry. All rights reserved. 

CS U1 2009 U1 Q4

 

Part A

Identifying and evaluating possible solutions involves generating different approaches to solving the problem.  Solutions will include different methods of solving the problem, e.g contrasting data structures :  one solution may use arrays only, another will use a mixture of arrays and structs.  Each solution is compared against each other to determine individual advantages and disadvantages when compared to each other in terms of efficiency and how well it solves the problem.[1]


[1] https://islandclass.wordpress.com/2020/02/17/stages-in-problem-solving/

Part B

Strategy – keep track of all variables in a trace table, draw output as you go along.

Table – write all variables in the order of its logical use within main control structures, use 1 variable per column:

SIZEj
108
 7
 6
 5
  
  
  

Answer:

----------* 
---------**
--------*-*
-------*--*
------*---*
-----*----*
----*-----*
---*------*
--*-------*
-*--------*
***********

Part C

START
Sum<--0
For i<--14 to 126 DO
	If i%7 ==  0  Then
		Sum<--sum +i
	Endif
Endfor
PRINT Sum
END

Coded solution:

#include <stdio.h>
#include <stdlib.h>

int main()
{
   
    int sum = 0;
    for (int i= 14; i<=126; i++)
        if (i%7==0)
            sum+=i;
    printf("The sum is : %d", sum);

    return 0;
}

© 2021  Vedesh Kungebeharry. All rights reserved. 

2009 U1 Q3

Part a

An algorithm is a well-defined, precise, finite set  of steps for solving a problem.[1]

Part b

Sequence (procedure), Selection (if-then-else) , Iteration (Looping)

Part c

Note:  j goes from 2,4,6,… 96,98 during looping and is output to the screen. Odd numbers are not output. See the attached flowgorithm file here:

https://drive.google.com/file/d/1H21gL4E0YGtgzXgwUD-1cFuEHsmC8IY0/view?usp=sharing

Image:

https://drive.google.com/file/d/1smeXp9ArthIPi7ZY1NfnJqJrYKIJx4rK/view?usp=sharing


Part C(i)

J = 100, C=50

Part c(ii)

2. J=1
3. C=1

Note, for this part we arrive at a solution that solves the problem exactly as stated (only numbers from odd numbers from 1 to 99 and the count.  See flowgorithm file for the corrected algorithm:

Flowgorithm file

Image

Displaying 2009Q3Cii.png




Part c(iii)

c=50

Part  d

Use “Start” instead of main.  See Files Below:

Flowgorithm

Image





[1] VK. Blogged at https://islandclass.wordpress.com/2020/02/10/what-is-an-algorithm-definition/

© 2021  Vedesh Kungebeharry. All rights reserved. 

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

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