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. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s