Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : Structures passing/returning
Concept Learning Code Sheets

Passing Structure variables to a function #2955

Demonstration of how structure variables can be passed to function as pass-by-value or as pass-by-reference method

#include <iostream>
using namespace std;
const double PI=3.1416;
struct Circle
{
int x,y;
float rad;
};

double CirArea(float r)
{
return PI*r*r;
}
float CirTop(float &r,int &yc)
{
return yc+r;
}

int main()
{
Circle c1={10,10,5.5};
cout<<"Area of given circle : "
<<CirArea(c1.rad)<<endl;
float r=c1.rad;
int yc=c1.y;
cout<<"Top point of given circle : "
<<CirTop(r,yc)<<endl;
return 0;
}

Passing structure to a function pass-by-value #2969

A program to demonstrate passing of structure to a function using pass by value method,

#include <iostream>
using namespace std;
struct Sale
{
int ItemID;
int qty;
float uprice;
};

float price(Sale s)
{
return s.qty*s.uprice;
}

int main()
{
Sale s1={1,7,25.0},s2={1,3,15.0};
cout<<"ItemIDtQtytUPtPrice"<<endl;
cout<<"-----------------------------"<<endl;
cout<<s1.ItemID<<"t"
<<s1.qty<<"t"
<<s1.uprice<<"t"
<<price(s1)<<endl;
cout<<s2.ItemID<<"t"
<<s2.qty<<"t"
<<s2.uprice<<"t"
<<price(s2)<<endl;
return 0;
}

Structure passed as a reference #2979

A program to demonstrate how an structure is passed as a reference argument to a function.

#include <iostream>
#include <string.h>
using namespace std;
struct Person
{
char name[40];
int age;
};

void modify(Person &p,char n[],int a=16)
{
strcpy(p.name,n);
p.age=a;
}

int main()
{
Person pers={"Lata",16};
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Arun",17);
cout<<pers.name<<", "<<pers.age<<endl;
modify(pers,"Suraj");
cout<<pers.name<<", "<<pers.age<<endl;
return 0;
}
Solved Problems

Slope of line -using structure Point passed to a function #2973

Write a program by declaring a structure Point with two integer values for i

Output Writing Structure pass by value/reference #2984

Write the output of the following program depicting structure instances being

Output Writing – Pixel structure passed by reference #2987

Write down the output of the program and give steps in arriving at such outpu


Code Sheets:3  Solved Problems:3 
Back