Q1: A programmer must specify the prototype of a function if the calling statement is encountered before the definition of the function. Can we write a program without specifying the prototype of any function? Give your answer with a suitable example.
ANS:
There
are two types of functions :
1.Library functions.(built-in
function).
2.User-defined
function.
In
case of library functions, we don’t specify the proto-type
of the function because they don’t include three steps that are:
1.Declare.
2.Call.
3.Define.
So,
in case of library function, we can write a program without specifying the
prototype of any functions. Its example is:
To find the square root
of the function:
#include<stdio.h>
#include<conio.h>
#include<library.h>
main()
{
Int x,y;
Printf(“enter x”);
Scanf(“%d”,&x);
y=sqrt(x/);
printf(“square root
is%d”,y);
getch();
}
But, in case of user –defined function, a
programmer has to specify the prototype of a function if the calling statement
is encountered before the definition of the function.
so,
in this case we can’t write a program without specifying the prototype of any
function because it is written at the beginning of the program , ahead of any programmer
– defined functions (including main).
Fuction prototypes are
mandatory in C.They are desirable because they further facilitate error
checking between the calls of function and corresponding function definition.
We can suggest it by following example:
#include<stdio.h>
#include<conio.h>
int fac(int
); ……. (Prototype
)
void main ……..
( Calling function )
{
Int a,f;
printf("enter
the number”);
scanf(“%d”,&a);
f=fact(a);
printf(“factorial
is%d”,f);
getch();
}
int fac(int
x) ………….. ( Called function )
{
Int f;
if (x == 1)
return(1);
else
f= x*fact(n - 1);
return(f);
}
If this prototype is provided, the compiler will catch the error in
main(). If it is omitted, then the error may go unnoticed
Q2:
“Call by value parameter passing scheme does not reflect the changes done in
the called program into the calling program.” Do you support the statement?
Give an example to explain the phenomena.
ANS: Yes , I support
the statement because whenever we called a function and passed something to it
we have always passed the ‘values’ of varable to the called function, such
functions are called ‘call by value’.In this method, the ‘value’ of each of the
acyual arguments in the calling function is copied into corresponding formal
arguments of the called function. With this method,the changes made to the
formal argument in the called function have no effect on the values of actual
arguments in the calling function. The following program illustrates the “CALL
BY VALUE” :
#include<stdio.h>
#include<conio.h>
Void swapy(int x,int
y0;
Int main()
{
Int a=10,b=20;
Swapy(a,b);
Printf(“a=%d
b=%d\n”,a,b);
getch( );
}
Void swapy(int x,int y)
{
Int t;
t=x;
x=y;
y=t;
printf(“x=%d
y=%d\n”,x,y);
}
OUTPUT:
x=20 y=10
a=10 b=20
Here, we conclude that
the values of a and b remain unchanged even after
exchanging the values of x and y.
Q3:
“If a problem is solvable by using both recursion and loops then it’s better to
use loops from system performance point of view.” Explain the fact with a
suitable example.
ANS:
If a problem is solvable by using both recursion and loops then it’s better to
use loops from system performance point of view because in case of loops , we
have specify the conditions that we want to perform in the program. It becomes
easy for the user from the system performance point of view . It tells us that
how many times we can run the loop and it depends on the user and programs
accessibility to run the loop in the specified condition.we can suggest it by
an example of factorial of the number in which we use for loop , 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.
Example:
#include<stdio.h>
#include<conio.h>
int factorial(int );
int main()
{
Int a,fact;
printf("enter the number”);
scanf(“%d”,&a);
fact=factorial(a);
printf(“factorial is%d”,fact);
getch();
}
int factorial(int
x)
{
Int f=1,i;
for(i=x,i>=1,i--)
f=f*I;
return(f);
}
OUTPUT:
Enter any
number=5
Factorial value=120
So we used the for
loop , so that we may specify the condition and get the desired result and
we use loop because it gives the output for only one runs of the program.
But , in case of recursion. Recursion is a process by which a fuction calls itself repeatedly, until some specified condition has beensatisfied. The process is used for repetitive computations in which each action is stated in terms of a previous result. Many iterative (i.e., repetitive) problems can be written in this form.In order to solve a problem recursively, two conditions must be satisfied. First, the problem must bewritten in a recursive form, and second, the problem statement must include a stopping condition.The example for it is:
#include<stdio.h>
#include<conio.h>
int fac(int );
void main
{
Int a,f;
printf("enter the number”);
scanf(“%d”,&a);
f=fact(a);
printf(“factorial is%d”,f);
getch();
}
int fac(int
x)
{
Int f;
if (x == 1)
return(1);
else
f= x*fact(n
- 1);
return(f);
}
Output:
Here, output is for
four runs of the program
Enter any number1
Factorial value=1
Enter any number2
Factorial value=2
Enter any number3
Factorial value=6
Enter any number5
Factorial value=120
Here ,it gets repeated
with the statements several times.
Thus,
from above discussions , we use to use loops from system performance point of view.
Q4.
“Preprocessing is the phase just before the compilation.” Is this statement
true? Give your answer with definition of preprocessing directives.
ANS: Yes,this statement
is true because there are 3 main stages of Compilation that are:
1.preprocessing
2.compilation
3.linking
A preprocessor is a program that processes its
input data to produce output that is used as input to another program. The
output is said to be a preprocessed form of the input data, which is often used
by some subsequent programs like compilers.
The unique feature of c language is the
preprocessor. A program can use the tools provided by preprocessor to make his
program easy to read, modify, portable and more efficient.
Preprocessor
is a program that processes the code before it passes through the compiler. It
operates under the control of preprocessor command lines and directives.
Preprocessor directives are placed in the source program before the main line
before the source code passes through the compiler it is examined by the
preprocessor for any preprocessor directives. If there is any appropriate
actions are taken then the source program is handed over to the compiler.
Preprocessor
directives follow the special syntax rules and begin with the symbol #bin
column1 and do not require any semicolon at the end. A set of commonly used
preprocessor directive
The preprocessing directives are used to define its use and they
are given below:
Preprocessor directives:
Directive |
Function |
#define |
Defines
a macro substitution |
#undef |
Undefines
a macro |
#include |
Specifies
a file to be included |
#ifdef |
Tests
for macro definition |
#endif |
Specifies
the end of #if |
#ifndef |
Tests
whether the macro is not def |
#if |
Tests
a compile time condition |
#else |
Specifies
alternatives when # if test fails |
Q5.
Suppose you have to write a program in which you have to declare 100 variables
of the same type. Take an example of same kind of problem and write the code?
ANS:
#include<stdio.h>
#include<conio.h>
main( )
{
Int a[100],i;
for(i=1; i<=100;i++)
{
Printf(“ enter elements”);
Scanf(“%d”,&a[i]);
for(i=1; i<=100;i++)
{
Printf(“elements are %d”,a[i]);
}
getch( );
}
Q6.
Write a program to initialize an array of strings and display the strings using
pointer to strings.
ANS: to
initialize an array of strings:
#include<stdio.h>
#include<conio.h>
main( )
{
Char
names[6][10]={“ram”,”sam”,”rani”,”raju”,”jiya’,”bani”};
Int I;
Char t;
Printf(“original string
is %s”,&names[2][0],&names[3],[0]);
for(i=0;i<=9;i++)
{
t=names[2][i];
names[2][i]=names[3][i];
names[3][i]=t;
}
Printf(“new is
%s%s”,&names[2][0],&names[3][0]);
getch();
}
WAP
to display the strings using pointer to strings.
:
#include<stdio.h>
#include<conio.h>
main( )
{
Char *names[
]={“ram”,”sam”,”rani”,”raju”,”jiya’,”bani”};
Char *temp;
Printf(“original string
is %s%s”,names[2][0],names[3],[0]);
temp=names[2];
names[2]=names[3];
names[3]=temp;
Printf(“new is
%s%s”,names[2],names[3]);
getch();
}
Q7.
Can we change the base address of an array at run time? Give your answer with
proper justification
ANS: It's not possible , address is an default we can't change that value.
No, we can’t change the base address of the array because base address is a constant value.
like :
int const i=5;
i++;
in this case i can't be incremented. So, the base address cann't be incremented.
Sometimes, If the compiler allocated the array at compile time, or if the array was
automatically allocated as a local variable, then no, you cannot change its base
address at run time. If you allocated the array at run time from the heap, you can
change its base address by allocating a new array, copying the old elements from
old to new and deleting the old array.
The base address of an array is the address of the first element on the array and always
appears in the lowest memory location.The second array element directly follows the
first in memory, the third element follows the second, etc. Note that there is no requirement
that the indices start at zero. They may start with any number as long as they arecontiguous.
However, for the purposes of discussion, it's easier to discuss accessing array elements if
the first index is zero.This text generally begins most arrays at index zero unless there is a
good reason to do otherwise.However, this is for consistency only. There is no efficiency
benefit one way or another to starting the array index at zero
#include<stdio.h>
#include<conio.h>
main ( )
Int array[2] [3]={1,2,3,4,,5,6};
Int base address=array;
Print f(“base addressof an array is %d\n”,base address);
getch();
}
Q8:
Write a program to explain the concept of passing array elements to a function.?
ANS: An entire array can be passed to a function as an
argument. The manner in which the array is passed differsmarkedly, however,
from that of an ordinary variable.To pass an array to a function, the array
name must appear by itself, without brackets or subscripts, as an actual
argument within the function call. The corresponding formal argument is written
in the same manner,though it must be declared as an array within the
formal argument declarations. When declaring a one dimensional array as a formal
argument, the array name is written with a pair of empty square brackets.
The size of the array is not specified within the formal argument
declaration.Some care is required when writing function prototypes that include
array arguments. An empty pair ofsquare brackets must follow the name of each
array argument, thus indicating that the argument is an array. If argument names
are not included in a function declaration, then an empty pair of square
brackets must follow the array argument data type.
THERE ARE TWO METHODS TO JUSTIFY THE CONCEPT
OF PASSING AN ARRAY ELEMENTS TO A FUNCTION:
1.PASSING ARRAY ELEMENTS
2. PASSING COMPLETE
ARRAY
1.PASSING ARRAY
ELEMENTS: It can be suggested by an example that is given below:
#include<stdio.h>
#include<conio.h>
Void display(int);
main( );
{
Int
i:
Int marks[ ]={50,60,70,80,90]
for(i=0,i<5;i++)
{
display (marks [i]);
}
getch( );
}
Void display(int m)
{
Printf(“%d”,m);
}
2. PASSING COMPLETE
ARRAY: It can be suggested by an example that is given below:
#include<stdio.h>
#include<conio>
Void display(int *,int);
main( );
{
Int num[ ]={2,3,4,5,6,7};
display (&num [0],6);
getch( );
}
Void display(int *j,int x)
{
Int i;
for(i=0,i<n,i++)
{
Printf(“elemenys =%d”,*j);
j++;
}
}
Differences between:
Whole Circle Bearing vs Quadrantal Bearing
Lintel level vs Sill level vs Plinth level
Construction Joint vsExpansion Joint
Site Engineer vs SiteSupervisor
This blog helped me a lot.I was finding the answer of some questions and this blog provided me with best answers
ReplyDeletePost a Comment