Skip to main content

Structured Programming - set 1 - 2015 solve


 

Set : 1

Question( a):  

What do you understand by constant ,variable,and  keyword? Discuss the scope of a variable.

 

Variables: In a typical C program we have to do a lot of computation. Of course

there will be storing of some data in different locations of memory in computer.

These memory locations are identified by their address like 56234.variables are

nothing but the name of the locations or addresses of memory which is given by

programmer.

 

constant : x=3,we have stored the constant value 3 at x location. The name of

that location is x. It’s a variable. We can also store another value at x location.

Here X = variable

3 = constant

 

keywords :Keywords are the words whose meaning is already explained to the

compiler. They cannot be used as a variable name.

  

Scope of  a  variable:

In general, the scope is defined as the extent up to which something can be

worked with. In programming also the scope of a variable is defined as the extent

of the program code within which the variable can we accessed or declared or

worked with. There are mainly two types of variable scopes:

1. Local Variables

2. Global Variables

 



Question (b)

Distinguish between the following:

      1.   Syntactic error and semantic error.

      2.   Runtime error and logical error.

 

Answer:

scroll left-right to get the full table in mobile screen

Syntactic error

Semantic error

1. Errors that  occur during the      parsing of input code is called Syntactic error.

1. Errors that  occur during the execution of the code is called semantic error. After it has been parsed as grammatically correct.

2. Syntactic   errors are caused by grammatically  incorrect  statements.

2. The semantic error can arises using the wrong variable or using wrong operator or doing operation in wrong order.

3. Some of the common syntactic errors  are:

•missing or misplaced.

•missing return type for a procedure.

•missing or duplicate variable declaration.

 

 3.If there is a semantic error in a program, it will run successfully, in the sense that the computer will not generate any error messages, but it will not do the right thing.

4.In context of a programming language syntax refers to the structure of the program without concerning about it’s meaning.

4.Semantics term in a programming language is used to figure out the relationship among the syntax and the model of computation used in the program

5. Identify syntactic errors are easy.

tricky

 

5. Identifying semantic errors can be

 

 

2.   Distinguish between  Runtime error and logical error.

  

 

        

         Run-time error

            

 

Logical error

 

1.Errors such as mismatch of data types or array out of bound error are known as runtime errors

1. The errors related with the logic of the program execution are called logical error

 2.Run time errors are generally go undetected by the compiler so programs with run-time error will run but produce erroneous results.

2.These errors are not detected by the compiler and are primarily due to a poor understanding of the problem or a lack of clarity of hierarchy of operators. Such errors cause incorrect result.

3.A runtime error is an error that occurs due to an illegal operation in the program.

3. A logical error is an error that occurs due to a fault in the algorithm of the program.

4.Cause the program to stop execution or to crush.

4.do not cause the program to stop execution,but it will give a wrong output.

5.Can occur due to reasons such as dividing a number by zero or because of accessing a memory location that is not available.

5.can occur deu to wrong use of operators and an inappropriate sequence of instructions.



Question (c)

Write a C program  to display the real, imaginary and  equal roots of  quadratic  equation    ax2 + bx + c = 0

ANSWER: 

scroll left-right to get the full program code in mobile screen

 #include<stdio.h>


#include<conio.h>

#include<math.h>

void main()

{

    float a,b,c,d,r1,r2;

    clrscr();

    printf("Please input the value of a,b&c:");

    scanf("%f%f%f",&a,&b,&c);

    d=(b*b)-(4*a*c);

    if(d<0)
    {

        printf(" Roots are imaginary.\n");

    }
    else if(d==0)
    {

        printf("Both Roots are equal.\n");

        r1=-b/(2*a);

        printf("Equal roots of equation:%.3f",r1);

    }
    else
    {

        printf("Roots are real number.\n");

        r1=(-b+sqrt(d))/(2*a);

        r2=(-b-sqrt(d))/(2*a);

        printf(" Root1 = %.3f\n",r1);

        printf(" Root2` = %.3f\n",r2);
    }

    getch( );

}   








Question(d):

 What are the features of C preprocessor? Give the difference macros and functions.

Answer :

The C preprocessor provides four separate facilities:

 

Inclusion of header files: These are files of declarations that can be substituted into your program.

 

Macro expansion: we can define macros, which are abbreviations for arbitrary

fragments of C code,  and  then the C preprocessor  will replace the macros with their definitions  throughout the program.

Conditional compilation: Using special preprocessing directives, you can include or exclude parts of the program  according   to  various  conditions, compiler intermediate .

Line control:  If  we  use a program to combine or rearrange  source  files into aj intermediate file which is then compiled, you can use line control to inform the compiler of where each source line originally came form.

Difference between macro and function :

                       Macro

                          Function

 1.Macro  is  preprocessed.

 

1. Function  is   Compiled.

2. No type  checking is

Read.

2. Type  checking  is  done.

3. Code lengthincreases

(Expands).

3. Code  length  remains  same.

4. Use of macro  can have side

effects.

4. No side  effects.

5. Speed of  execution is   faster.

5. Speed of  execution is

slower.

6. Before   compilation   Macro name is   replaced by   Macro value.

6.  During  function  call, Transfer of  control takes

place.

7. Useful where small code

appears  frequently

7. Useful where large code

appears  frequently

8. Generally   Macros are of

one line code.

8. Function can  be  of  any

number of   lines.

9. Compiler  dose  not  check

Macro  errors.

9. Compiler  dose  not  check

Function  errors.