Search This Blog

Monday, 4 June 2018

CS201 Introduction to Programming - Programming Lab 6 WEEK 8

//Adding structure variable values using function
#include <iostream>
using namespace std;

//Structure with two Data Members
struct MyStruct{
       int i;
       float f;
};
//ms3 = {22,2002}, // To show different methods of initializing structure
//ms4 = {2,2};

//Function to add two structure variables     
MyStruct add(MyStruct ms1, MyStruct ms2){
         MyStruct ms3;
         ms3.i = ms1.i + ms2.i;
         ms3.f = ms1.f + ms2.f;
         return ms3;
}

main(){
       //Initialize structure variable values
       MyStruct ms1 = {22,2002};
       struct MyStruct ms2 = {2,2};
     
       cout<< "values of data members of first structure variable\n\n";
       cout << ms1.i << "\t" << ms1.f << endl;
     
       cout<< "values of data members of Second structure variable\n\n";
       cout << ms2.i << "\t" << ms2.f <<endl;
     
       MyStruct ms3= add(ms1,ms2);
       cout<< "values of data members of structure variable returned from function\n\n";
       cout << ms3.i << "\t" << ms3.f <<endl;
     
       system("pause");     
}

No comments:

Post a Comment