Q1: How can we change the control of execution of a program? Give one example.

Ans:  Changing the control of execution  of program, might involve one of the following situations:

1. JUMPING : transferring control from one point to another point in the same program unit.

2. BRANCHING: executing one group of instructions depending on the outcome of a decision.

3.LOOPING: executing a group of instructions repetitively either for a given number of times or until the required conditions are met.

The various flow control statements cluubed in the following categories:

1.DECISION MAKING STATEMENTS:

a.). if  statement.

b.). if-else  statement.

c.) .else - if  statement.

d.). switch statement.

2. LOOPING STAEMENTS:

a.). for  statement.

b.). while statement.

c.) .do-while statement.

3. JUMPING STAEMENTS:

a.). break statement.

b.). continue  statement.

c.). goto statement.

Thus , these statement provides the less time in changing the control of execution of a program.we can take an  example of  goto statement, to show the change of control of execution of a program:

#include<stdio.h>

#include<conio.h>

#include<process.h>

Main( )

{

Int x;

Printf(“enter x”);

Scanf(“%d”,&x);

If(x<=5)

goto so;

else

printf(“good”);

exit( );

so:

printf(“not good”);

getch( );

}

 

Q2: “A condition in the if statement can be either simple condition or a compound condition. How can a compound condition be checked using if construct?  Justify your answer with a suitable example program.

ANS : The if - else statement is used to carry out a logical test and then take one of two possible actions,depending on the outcome of the test (i.e., whether the outcome is true or false). If  the condition evaluates to a non –zero value, then the statements in the statement  - block are executed, other-wise they are passed by.The statement can be either simple or compound. In practice, it is often a compound statement which may include other control statements.In an IF  statement we   check both condition. A simple condition  uses simple if and for a compound condition, we use if-else or ladder if else.If multiple statements are to be executed, then they must be placed within braces.its illustrated by following example:

To calculate the bonus:

#include<stdio.h>

#include<conio.h>

Int main( )

{

Int  bonus,cy,yoj,yos;

Printf(“enter current year and year of joining “);

Scanf(“%d%d”,&cy,&yoj);

yos=cy – voj;

if(yos > 3)

{

bonus = 2500;

printf(“bonus=Rs.%d \n”,bonus);

}

Return 0;

}

Here ,the two statements to be executed on the satisfaction of the condition have been enclosed within a pair of braces, if the pair of braces is not used, then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on satisfaction of the condition . in other words , we can say that the default scope of the if statements is the immediately next statement after it. The condition may represent a relation expression, a logical expression, a numeric variable or a numeric constant .Thus, a compound statement can be checked using if statement.


Q3: “The time taken to solve a problem using if-else if construct will definitely be more than the time taken to solve the same problem using simple if statement.” Is this statement true? J ustify the statement by using suitable example

ANS: Yes, its true.

Conditional execution is applied mostly in the body of if statements. Conditional execution is disabled for code sequences which contain function calls, as on function return the flags are destroyed.It is therefore beneficial to keep the bodies of if and else statements as simple as possible, so that they can be conditionalized.
A program using if-else if we writt in ladder if else . In this statement we only use  simple if. We use it for every given condition in a program

Example:-

#include<stdio.h>

#include<cnonio.h>

Main()

{

Int n;

Printf(“enter no.”);

Scanf(“%d”,&n);

If(n>1)

Printf(“positive”);

If(n<1)

Printf(“negative”);

If(n=0)

Printf(“zero”);

getch();

}


Q4. Write a program to convert an integer number into binary number and vice-versa.

Sol: ANS- firstly , we will convert Binary into decimal integer:

#include<stdio.h>

#include<conio.h>

void main()

{

long num,bnum,dec =0,base=1,rem;

clrscr();

printf(“enter a binary number”);

scanf(“%ld”,&num);

bnum=num;

while(num>0)

{

rem=num%10;

dec=dec+rem*base;

num=num/10;

base=base*2;

}

printf(“binary number is=%ld\n”,bnum);

printf(“its decimal equivalent is=%d\n”,dec);

getch();

}

OUTPUT IS:

enter a binary number

1101110

binary nbr is =1101110

its decimal equivalent is =110

/*decimal to binary*/

#include<stdio.h>

#include<conio.h>

void main()

{

long num,dnum,rem,base=1,bin=0;

clrscr();

printf(“enter a decimal number\n”);

scanf(“%ld”,&num);

dnum=num;

while(num>0)

{

rem=num%2;

bin=bin+rem*base;

num=num/2

base=base*10;

}

printf(“input nbr is =%d\n“,dnum);

printf(“its binary equivalent =%ld\n”,bin);

getch();

}

OUTPUT IS:

enter a decimal integer

75

input nbr is =75

its binary equivalent =1001011


Q5. I wrote a program in C. I want some statements of the program to have iterative nature. One of my classmates suggested me to repeat those specific statements as the number of times as I want them to be executed. Can you suggest me any alternate solution. Explain the concept with a suitable example program.

ANS: the word “LOOP” means repetition , so we can make use of loops for repeating statements several times. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program. They are:

 (a)Using a for statement

                 (b)Using a while statement

                 (c)Using a do-while statement

Here we will consider any loop out of three , we choose while loop.  The while statement is used to carry out looping operations, in which a group of statements is executed repeatedly, until some condition has been satisfied.

EXAMPLE USING WHILE LOOP:  to print HELLO statement hundred times using while loop

#include<stdio.h>

#include<conio.h>

                 main( )

{

int i;

clrscr();

for(i=1;i<=100;i++)

{

printf(“hello”);

}

getch( );

}

We can also do repititon by do – while statement: When a loop is constructed using the while statement . The test for continuation of the loop is carried out at the beginning of each pass. Sometimes, however, it is desirable to have a loop with the test for continuation at the end of each pass. This can be accomplished by means of the do - while statement.

EXAMPLE USING DO - WHILE LOOP:  to print HELLO statement hundred times using do-while loop:

#include<stdio.h>

#include<conio.h>

void main( )

{

int  i;

do

{

printf(“hello”);

I = i+1;

}

while(i< =100)

getch( );

}

 

Q6. “A Turbo C program displays an error message when I write a program using while loops without specifying the condition, but this is not the case with for loops.” Explain the fact with a proper example

ANS: when we use WHILE loops, it does pre – testing , i.e., firstly it checks the condition , then it prints the statement that we have printed.WHILE loops are better because it does testing of the specified condition first.

 Example: An example that will print all the numbers from 1 to 10 using while loop:

#include<stdio.h>

#include<conio.h>

 main( )

{

int  i;

i=1;

while(i<=10)

{

Printf(“%d”,i);

i++;

}

getch( );

}

Here the given test condition is evaluated and if the condition is true then the body of the loop is executed. After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes false and the control is transferred out of the loop. On exit, the program continues with the statements immediately after the body of the loop. The body of the loop may have one or more statements. The braces are needed only if the body contained two are more statements


In other case of for loops we have:

In case of FOR loops, it is suited for the problems where the number of times a statement or  statement block will be executed in advance .

The for loop provides a more concise loop control structure. The general form of the for loop is:



for (initialization; test condition; increment) 

body of the loop 
}

When the control enters for loop the variables used in for loop is initialized with the starting value such as I=0,count=0. The value which was initialized is then checked with the given test condition. The test condition is a relational expression, such as I < 5 that checks whether the given condition is satisfied or not if the given condition is satisfied the control enters the body of the loop or else it will exit the loop. The body of the loop is entered only if the test condition is satisfied and after the completion of the execution of the loop the control is transferred back to the increment part of the loop. The control variable is incremented using an assignment statement such as I=I+1 or simply I++ and the new value of the control variable is again tested to check whether it satisfies the loop condition. If the value of the control variable satisfies then the body of the loop is again executed. The process goes on till the control variable fails to satisfy the condition.

 Example:    to print the series starting from 1 and ending at 100 ..

 #include<stdio.h>

#include<conio.h>

 main( )

{

int  i;

for(i=1;i<=100;i++)

{

Printf(“%d”,i);

}

getch( );

}

 

Q7. “Using the concept of functions in the source code minimizes the typing efforts but increases the execution time.” Comment on the statement with a suitable example. 

ANS: while using functions, we develop a program in a very large and complex, its is always recommended to split into two or more functions(sub- programs)that can be developed separately, tested separately and then integerated.writing functions avoids rewritimg the same code over and over but the functions in the source code minimizes the typing efforts because using functions we declare , call, define them .thats why it provides the source code and minimizes typing efforts. It increases the execution time because the process of execution starts in declaring , calling , defining the function takes time. Example is:

 Functions are also called Sub routines 
* The invocation of a subroutine (rather than using in-line code) imposes some computational overhead in the call mechanism itself

* The subroutine typically requires standard housekeeping code—both at entry to, and exit from, the function (function prologue and epilogue—usually saving general purpose registers and return address as a minimum).

Q8: In turbo C, what will be the program behavior if the programmer does not specify the return type of a function? Explain properly with an example.

ANS:

THE RETURN STATEMENT SERVES TWO WAYS:

1.).On executing the return statement, it immediately transfers the control back to the calling function.

2.). it returns the value present in the parantheses after RETURN, to the calling function.

But , if the programmer does not specify the return type of a function ,then our program will not be specified with the function we have given.

It can be under stood by an simple example:

 We have  to find out square of a floating point number using a function. This  follows

#include<stdio.h>

float square (float);

void main()

{

float a,b;

printf(“\n enter any number”);

scanf(“%f”,&a);

b=square(a);

printf(“\nsquare of %f is %f”,a,b);

return 0;

}

float square (float x)

{

float y;

y=x*x;

return(y);

}

And here are three sample runs of this program….

enter any number 3

square of 3 is 9.000000

enter any number 1.5

square of 1.5 is 2.250000

enter any number 2.5

square of 2.5 is 6.250000

Since we are returning a float value from this function we have indicated the return type of the square() function as float in the prototype declaration as well as in the function definition. Had we dropped float from the prototype and the definition, the compiler would have assumed that square () is supposed to return an integer value.

 

 

 

 




 








Differences between:

OPC cement vs PPC cement

Hydraulic lime vs Fatlime

Bolt vs Screw

Concrete vs Mortar

CPM vs PERT

Pier vs Abutment

Culvert vs Bridge

Whole Circle Bearing vs Quadrantal Bearing

Lintel level vs Sill level vs Plinth level

Construction Joint vsExpansion Joint

One way slab vs two wayslab

Site Engineer vs SiteSupervisor




For Testing:

Slump cone test, Silt content, Water absorption, Bulking of sand
Compaction factor test of concrete
Los Angeles Abrasion test on Aggregates

Post a Comment

Previous Post Next Post