COMPUTER SCIENCE C++

1.e) What will be the output of the following program.

#include<iostream.h>
void main( )
{ int var1=5,var2=10;
for(int i=1,i<=2;i++)
{ cout<<var1++<<’\t’<< - - var2<<endl;
cout<<var2- -<<’\t’<<+ + var1<<endl;
}
}

Ans: Output:

5 9
9 7
7 7
7 9

f) Write definition for a function Sum Sequence( ) in C++ with two arguments/parameters – double X and int n. The function should return a value of type double and it should perform sum of the following series. 1/x- 3!/x2 + 5!/x3 – 7!/x4 +9!/x5. - ------upto n terms.

Note: The symbol ! represents Factorial of a number ie 5!= 1 X 2 X 3 X 4 X 5.

#include<iostream.h>
#include<math.h>
#include<conio.h>
double SumSequence(int x1,int n1);
void main()
{ int x;
int n;clrscr();
cout<<"Enter the vaue of X and N";
cin>>x>>n;
cout<<”\nThe sum of the series =“<<SumSequence(x,n);
getch();
}
double SumSequence(int x1,int n1)
{ double sum=0;
int c=0;
for(int i=1;i<=(2*n1);i=i+2)
{ int f=1;
for(int j=1;j<=i;j++)
{ f=f*j;
}
c=c+1;
if(c%2==1)
{ sum=sum+f/(pow(x1,c));
}
else
{ sum=sum-f/(pow(x1,c));
}
}
return sum;
}

2003 Annual Paper:

1.a) What is the difference between global variables and local variables? Give an example to illustrate the same.

2

Ans: The local variables are the variables defined within any function (or block) and are hence accessible only within the block in which they are declared. In contrast to local variables, variables declared outside of all the functions in a program are called global variables. These variables are defined outside of any function, so theyare accessible to all functions. These functions perform various operations on the data. They are also known as External Variables.

Eg:

#include<iostream.h>
int a,b;
void main()
{ float f;
---;
---;
}

In the above program segment, a and b are global variables, we can access a and b from any function. f is local variable to function main( ), we can access f from main( ) only.

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )