See the video:
© 2021 Vedesh Kungebeharry. All rights reserved.
See the video:
© 2021 Vedesh Kungebeharry. All rights reserved.
Download the following flowgorithm files and complete the tasks outlined in the comments for each flowgorithm file.
See the video below for more context
© 2021 Vedesh Kungebeharry. All rights reserved.
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.
//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;
}
/*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. */
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.
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/
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:
| SIZE | j |
| 10 | 8 |
| 7 | |
| 6 | |
| 5 | |
Answer:
----------*
---------**
--------*-*
-------*--*
------*---*
-----*----*
----*-----*
---*------*
--*-------*
-*--------*
***********
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.
An algorithm is a well-defined, precise, finite set of steps for solving a problem.[1]
Sequence (procedure), Selection (if-then-else) , Iteration (Looping)
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
J = 100, C=50
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:
c=50
Use “Start” instead of main. See Files Below:
[1] VK. Blogged at https://islandclass.wordpress.com/2020/02/10/what-is-an-algorithm-definition/
© 2021 Vedesh Kungebeharry. All rights reserved.
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.
Any three: ADD, LOAD, STORE, JUMP, JUMPIF, IN, COMPARE
| OPCODE | Operand 1 | Operand 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 .
Recall: Direct addressing means that the address of the operand is stored in the instruction
Fetch –
Decode –
Execute –
© 2021 Vedesh Kungebeharry. All rights reserved.
See the steps in the video below:
© 2021 Vedesh Kungebeharry. All rights reserved.
( this content is AKA: SBA Guidance – Trace Tables And Dry Runs )
Generally , we use the following as a guide when generating data for use with trace tables:
Download the files here: https://drive.google.com/drive/folders/1kKuCWbUajYe52ME_obPRSjxbyqxHEprg?usp=sharing
Use the file “Problem Solving and Programming – Demo – 04 Filled Tables” to complete
See the class video which offers a detailed explanation on the approach:
© 2021 Vedesh Kungebeharry. All rights reserved.
Task: How can you drop an egg from a height of 3 meters without breaking it?
Steps taken in solving the problem
What are the constraints?
The ground must be hard, no adding of sand
No use of Drones
No use of Parachutes.
Definition of problem : Drop an uncooked egg from 3m high onto hard ground without breaking it.
The process of generating ideas to solve the problem
Determine what is needed for EACH solution. e.g rubber bands, string , balloons etc.
Compare each solution across similar criteria:
e.g. availability of resources, cost, how well each solution is expected to solve the problem, the complexity of the solution.
After we have thoroughly evaluated the solutions, we choose one of them and implement it.
© 2021 Vedesh Kungebeharry. All rights reserved.
Pre-Requisite : Read (Campbell, Information Technology for CSEC – 3rd Edition, 2020, pp. 272-273)
Observe the attached flowgorithm file, or the pseudocode below:
Start
// Here we set up 2 numbers to experiment with our relational operators....
FirstNum = 7
SecondNum = 5
// Comparison: First number is greater than second number
greaterThan = FirstNum > SecondNum
output "' " + FirstNum + " is greater than " + SecondNum + " ' evaluates to " + greaterThan
// Comparison: First number is greater or equal to second number
greaterThanOrEqualTo = FirstNum >= SecondNum
output "' " + FirstNum + " is greater than or equal to " + SecondNum + " ' evaluates to " + greaterThanOrEqualTo
// Comparison: First number is equal to second number
equalTo = FirstNum = SecondNum
output "' " + FirstNum + " is equal to " + SecondNum + " ' evaluates to " + equalTo
// Comparison: First number is not equal to second number
notEqualTo = FirstNum ≠ SecondNum
output "' " + FirstNum + " is not equal to " + SecondNum + " ' evaluates to " + notEqualTo
// Comparison: First number is less than second number
lessThan = FirstNum < SecondNum
output "' " + FirstNum + " is less than " + SecondNum + " ' evaluates to " + lessThan
// Comparison: First number is less than or equal to second number
lessThanOrEqualTo = FirstNum <= SecondNum
output "' " + FirstNum + " is less than or equal to " + SecondNum + " ' evaluates to " + lessThanOrEqualTo
Stop
The flowgorithm file or the pseudocode above should output:
‘ 7 is greater than 5 ‘ evaluates to true
‘ 7 is greater than or equal to 5 ‘ evaluates to true
‘ 7 is equal to 5 ‘ evaluates to false
‘ 7 is not equal to 5 ‘ evaluates to true
‘ 7 is less than 5 ‘ evaluates to false
‘ 7 is less than or equal to 5 ‘ evaluates to false
Task:
In your notebook or personal notes, determine the output of the algorithm when FirstNum and SecondNum Is updated as follows:
© 2021 Vedesh Kungebeharry. All rights reserved.