Answers Part-A: Cse-Assignment-3

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

CSE-ASSIGNMENT-3

Homework Title / No. :CSE-3

Course Code : 1255E

Course Instructor :girish_kumar.

Date of Allotment :21/9/2010

Date of submission : 30/9/2010

Student’s Roll No:13

Section No. :A6003

Declaration:
Student’s Signature :B.NAGA VIJAY REDDY

ANSWERS
PART-A
1.ANS; No. We cannot write a program without specifying the prototype of any function.Function
prototypes are usually written at the beginning of the program,ahead of any programmer defined
functions.

For example:

int func1()
{
return 10;
}

int func2()
{
return func1()*29;
}

int func3()
{
return func1() / func2();
}
int main()
{
func1();
func2();
func3();

return 0;
}.

2.ANS: Whenever we call a function and pass something to it, we always pass the ‘values’ of variables to
the called function. Such function calls are called “calls by value”. By this, we mean is, on calling a
function we are passing values of variables to it.

Sum=calsum(a,b,c);

F=factor(a);

A simple program to depict this is written below:

/*to swap two numbers*/

#include<stdio.h>

Main(){

Int a=10; Int b=20;

Swap(a,b)

Printf(“%d%d”, &a, &b);

Swap(int x,int y)

{ int t;

t=x;

x=y;

y=t;

printf(“%d%d”, &x, &y);

}
3.ANS : If a function within a program calls the same function itself. It is the process of defining
something in terms of itself. By using the recursion we have to check the condition many times but in
case of loops it check the condition one time and it works the whole program.

For example= n factorial is n*n-1*n-2*……*1 .this can be expressed as n!=n*n-1! Where(!) stands for
factorial.

Recursive function of calculate factorial value=

/*to calculate factorial*/

# include<stdio.h>

Int fact(int);

Void main()

Int a, fact;

Printf (“enter no,”);

Scanf(“%d”, a);

Fact=fact(a);

Printf(“factorial=%d\n”, fact);

Int rec(int x)

Int f;

If(x==1)

Return( 1);

Else

F=x*rec(x-1);

Return (f );

Now let us consider the non-recursive function for calculating factorial.

/*to calculate factorial*/


#include<stdio.h>

Int factorial(int);

Main(){

Int a; fact;

Printf(“\nenter any number”);

Scanf(“%d”, &a);

Fact=factorial(a);

Printf(“factorial=%d”, fact);

Int factorial(int x)

{ int f=1; int I;

For(int i=x;i>=1;i--)

F=f*I;

Return(f);}

From here we conclude that we can only write the recursive function if and only if the concept of
functions using loops is clear to us. Recursion is a strange and complicated method. So it’s better to use
loops than the recursive functions.

4.ANS: The C preprocessor is a collection of special statements, called directives, that are executed at the
beginning of the compilation process. The #include and #define statements are preprocessor derivatives.
Additional preprocessor derivatives are #if, #elif, #else, #endif, #ifndef, #line and #undef. The
preprocessor also includes three special operators: defined, # and ##.Preprocessor directives usually
appear at the beginning of a program, though this is not a firm requirement. Thus, a preprocessor directive
may appear anywhere within the program. However, the directive will apply only to the portion of the
program following its appearance.

The #if, #elif, #else and #endif directives are used most frequently. They permit conditional
complication of the source program, depending on the value of one or more true or false conditions. They
are sometimes used in conjunction with the defined operator, which is used to determine whether or not a
symbolic constant or a macro identifier has been defined within the program.

PART-B
5.ANS:

#include<stdio.h>

Void main()

Int a(10),b;

Printf(“Enter the the number of seriesto be added”);

For(int i=0;i<10;i++)

Scanf(“%d”,&a(i));

c=0;

For(int i=0;i<10;i++)

b=b+a(i);

Printf(“%d%”,b);

6.ANS: #include <stdio.h>

char strA(80)= "A string to be used for my program";


char strB(80);

int main(void)
{

char *pA;
char *pB;
puts(strA);
pA = strA;
puts(pA);
pB = strB;
putchar('');
while(*pA != '')
{
*pB++ = *pA++;
}
*pB = '';
puts(strB);
return 0;
}

7.A:no, we cannot change the base address of any array , base address of the array is the address of the
first element in the array when once the memory location is allocated to the any variable its address is
permanent and you cannot change it for any cost . for example:int a(3)={1,2,3} here a(0)=1 its address
will be the base address its address be some 7890 so the base address of the a(3)=7890 and its address
will not be changed .so we can finally conclude that the base address of array can not be changed.

8.ANS :Array elements can be passed through a function by calling the function by the value ,or by
reference .In the call by value ,we pass the value of array elements through the function ,where as in the
call by reference, we pass address of array elements to the function .

#include <stdio.h>

Void display (int);

Int main()

Int a;

Int marks( )={66,67,78,89,90,98};

For (a=0;a<=6;a++)

Display(marks(a));

Return 0;

Void display (int m )

Printf(“%d”,m);

And here is output==

66,67,78,89,90,98
THE END

You might also like