Sunday, January 23, 2011

CPP_Swape & Sum of typedef struct

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct{
                int a;
                int b;
                int result;
                } ReccoreType;
int main()
{   ReccoreType Rec;
    int Sums(int,int);  //local variable
    void swap(int *,int *); //parameter is integer
    system("clear");
    cout<<"\nInput a,b(in Record) : ";
    cin>>Rec.a>>Rec.b;
    cout<<"\nBefore swap (in Record)a="<<Rec.a<<", b="<<Rec.b<<endl;
    swap(&Rec.a,&Rec.b);    //Call by referrence
    cout<<"\nAfter swap (in Record) a="<<Rec.a<<", b="<<Rec.b<<endl;
    cout<<"\nSum (in Record) a+b = "<<Sums(Rec.a,Rec.b)<<endl<<endl;
    return 0;
}   //Call by value
//----------------------------------------------------
void swap(int *x,int *y)//Call by referrence
{   int z;  //local variable
    z=*x;
    *x=*y;
    *y=z;
}
//-----------------------------------------------------
int Sums(int x,int y) //Call by value
{   //int result;
    //result = x+y;
    return (x+y);
}

No comments:

Post a Comment