News:

MyKidsDiary.in :: Capture your kids magical moment and create your Online Private Diary for your kids

Main Menu

C++ Tutorial » Structure » return structure

Started by magesh.p, Apr 27, 2009, 09:29 PM

Previous topic - Next topic

magesh.p

Return structure from a function

#include <cmath>
#include <iostream>
using namespace std;

// define the structures
struct DataStructure
{
  float radius;
  double angle;
};

struct ResultStructure
{
  float area;
  double sine;
  double cosine;
  double tangent;
};

ResultStructure compute(struct DataStructure mystruct);

int main ()
{
    DataStructure input;
       ResultStructure output;

       input.radius = 3;

       input.angle = 0.8;

       output = compute(input);

    cout << " The area is "<< output.area << "\n";
       cout << " The sine of the angle is " << output.sine << "\n";
       cout << " The cosine of the angle is " << output.cosine << "\n";
    cout << " The tangent of the angle is " << output.tangent << "\n";
       return 0;
}

ResultStructure compute(struct DataStructure mystruct)
{
     ResultStructure answer;

        answer.area = pow(mystruct.radius,2);
        answer.sine = sin(mystruct.angle);
        answer.cosine = cos(mystruct.angle);
        answer.tangent = tan(mystruct.angle);

        return answer;
};


Output

QuoteThe area is 9
The sine of the angle is 0.717356
The cosine of the angle is 0.696707
The tangent of the angle is 1.02964

- An Proud Acumen -

Quick Reply

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it has been approved by a moderator.

Name:
Email:
Verification:
Please leave this box empty:
Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

Shortcuts: ALT+S post or ALT+P preview