EXERCISE - 3

A) Write a program to implement call by value and call by reference using reference variable.
B) Write a program to illustrate scope resolution, new and delete Operators.
C) Write a program to illustrate Storage classes.
D) Write a program to illustrate Enumerations.

A) AIM: Write a program to implement call by value and call by reference using reference variable.
CALL BY VALUE
SOURCE CODE:
#include<iostream>
using namespace std;
void swap(int,int);
int main()
{
    int a,b;
    cout<<"Enter a and b values:";
    cin>>a>>b;
    cout<<"Before swapping a="<<a<<"\tb="<<b;
    swap(a,b);
    cout<<"\nAfter swapping a="<<a<<"\tb="<<b;
}
void swap(int a,int b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}
OUTPUT:
Enter a and b values: 3 6
Before swapping    a=3    b=6
After swapping    a=3    b=6
CALL BY REFERENCE
SOURCE CODE:
#include<iostream>
using namespace std;
void swap(int*,int*);
int main()
{
    int a,b;
    cout<<"Enter a and b values:";
    cin>>a>>b;
    cout<<"Before swapping a="<<a<<"\tb="<<b;
    swap(&a,&b);
    cout<<"\nAfter swapping a="<<a<<"\tb="<<b;
}
void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}
OUTPUT:
Enter a and b values: 3 6
Before swapping    a=3    b=6
After swapping    a=6    b=3
B) AIM: Write a program to illustrate scope resolution, new and delete Operators.
SOURCE CODE:
#include<iostream>
using namespace std;
int a=10;
int main()
{
    int a=5,n,*ptr,i;
    cout<<"Value of local variable:"<<a<<endl;
    cout<<"Value of global variable:"<<::a<<endl;
    cout<<"Enter number of elements:";
    cin>>n;
    ptr=new int[n];
    cout<<"Enter "<<n<<" elements:"<<endl;
    for(i=0;i<n;i++)
        cin>>ptr[i];
    cout<<"Elements are:"<<endl;
    for(i=0;i<n;i++)
        cout<<ptr[i]<<"\t";
    delete ptr;
}
OUTPUT:
Value of local variable:5
Value of global variable:10
Enter number of elements:4
Enter 4 elements:
3
7
1
9
Elements are:
3    7    1    9
C) AIM: Write a program to illustrate Storage classes.
SOURCE CODE:
#include<iostream>
using namespace std;
int g;
void display()
{
    static int s;
    register int r;
    r=3;
    s=s+r*2;
    cout<<"Inside display function"<<endl;
    cout<<"g = "<<g<<endl;
    cout<<"s = "<<s<<endl;
    cout<<"r = "<<r<<endl;
}
int main()
{
    int a;
    g=10;
    a=5;
    display();
    cout<<"Inside main"<<endl;
    cout<<"a = "<<a<<endl;
    cout<<"g = "<<g<<endl;
    display();
}
OUTPUT:
Inside display function
g = 10
s = 6
r = 3
Inside main
a = 5
g = 10
Inside display function
g = 10
s = 12
r = 3
D) AIM: Write a program to illustrate Enumerations.
SOURCE CODE:
#include <iostream>
using namespace std;
enum week { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };
int main()
{
    week today;
    today = Monday;
    cout<< "Day " << today+1;
    return 0;
}
OUTPUT:
Day 2

No comments:

Post a Comment

Total Pageviews