int main tream of each of column programs write your answer
Solution
A)
#include <iostream>
using namespace std;
void fun(int x)
{
static int i = 0;
cout << x << endl; // outputs that passed in an argument
cout << i++ << endl; // Incremented each time the function is called and i is not initialised to 0 everytimes
}
int main()
{
int i = 0;
for(i=0;i<3;++i)
fun(i);
cout << i << endl; //becoimes 3 when i is incre,ented and contion fails in for loop
return 0;
}
Output : 0 0 1 1 2 2 3
B)
#include <iostream>
using namespace std;
void fun(int a[] , int size)
{
for(int i=0;i<size;++i)
for(int j=i;j<size;j++)
a[i] = a[i]+1; // when i=0, inner loops runs size(5) times, so a[0] = 5 , similarly when i=1 loop runs 4
// 4 times hence a[1] = 4 and so on
}
int main()
{
const int size = 5;
int a[5] = {0};
fun(a,size);
for(int i=0;i<size;++i)
cout << a[i] << endl;
return 0;
}
Output : 5 4 3 2 1
C)
#include <iostream>
using namespace std;
void f3(int y)
{
y = y+3; //y becomes 13
cout << y << endl; //Outputs 13
}
void f2(int n)
{
n+=2; //n becomes 10
f3(n); // 10 is passed to f3
cout << n << endl; // Outputs 10
}
void f1(int x)
{
x++; //here local x becomes as 8
f2(x); //8 is passed to function
cout << x << endl; //After f2 coompletes outpout will be 8
}
int main()
{
int x = 7;
f1(x);
cout << x << endl; // x will remain as 7
return 0;
}
Output : 13 10 8 7
D)
#include <iostream>
using namespace std;
void f1(int &x, int y)
{
x = 4;
y = 3;
}
int main()
{
int n = 1;
int m=2;
f1(n,m);
cout << n <<\" \" << m << endl;
return 0;
}
Output : n will change to 4 and m will remain as 2 . Its because n is passed as rererence and m is passed by value
n = 4 , m = 2.
Thanks and let me know if there is anything.

