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.