Basic Operation on Structure in C++

Define a structure of employee having fields – empno,  name, designation, basic salary, da, hra, pf and total salary. Make an appropriate assumption about the data type.

Solution:

struct employee {
int   empno;
char name[40];
float: basic,da,hra,pf,total };

Assignment:  Write a program in C++ to read a record of employee having following fields empno, name, bsic salary,hra,da,pf, net salary. Calculate Net salary using formula net salary = basic salary + da + hra -pf. Also display this record on the screen.

solution:

#include<iostream>
#include<string.h>
using namespace std;

struct employee{
int empno;
char name[30];
int basic,da,hra,pf,net;
};
int main(){
employee e;
//input phase
cout<<“Enter Employee No :”; cin>>e.empno;
cout<<“Enter Name :”; cin>>e.name;
cout<<“Enter basic Salary :”; cin>>e.basic;
cout<<“DA : “; cin>>e.da;
cout<<“Enter Hra :”; cin>>e.hra;
cout<<“Enter PF :”; cin>>e.pf;

//processing phase
e.net = e.basic+e.da+e.hra -e.pf;
//output phase
cout<<“\n Employee DATA\n”;
cout<<“\n Emp number :”<<e.empno;
cout<<“\n Emp Name :”<<e.name;
cout<<“\n Basic Salary :”<<e.basic;
cout<<“\n DA :”<<e.da;
cout<<“\n HRA :”<<e.hra;
cout<<“\n PF :”<<e.pf;
cout<<“\n Net Salary :”<<e.net;
return 0;
}

Assignment : Create a structure of students containing roll no, the name of student and marks obtained in three subjects, Total marks. Also, write a program in C++ to read the information of 10 students in an array.  Calculate their total marks. Display the information of these 10 students in a tabular format.

Solution :

#include<iostream>
#include<string.h>
using namespace std;
struct student{
int roll;
char name[30];
int m1, m2,m3;
int total;
};

int main(){
student s[5];
int i;
//input phase
for(i=0;i<5;i++){
cout<<“\n Enter Roll No :”;
cin>>s[i].roll;
cout<<“\n Enter Name :”;
cin>>s[i].name;
cout<<“\n Marks Obtained in first subject :”;
cin>>s[i].m1;
cout<<“\n Marks Obtained in Second subject :”;
cin>>s[i].m2;
cout<<“\n Marks Obtained in third subject :”;
cin>>s[i].m3;
}
// processing phase
for(i=0;i<5;i++)
s[i].total = s[i].m1+s[i].m2+s[i].m3;

//output phase
system(“cls”);
cout<<“\t\t\t Class Result \n”;
cout<<“—————————————————————\n”;
cout<<“Roll No. Name Phy Chem Maths Total \n”;
cout<<“—————————————————————\n”;
for(i=0;i<3;i++){
cout<<s[i].roll<<“\t\t”<<s[i].name<<“\t “<<s[i].m1<<“\t”<<s[i].m2<<“\t”<<s[i].m3<<“\t”<<s[i].total<<endl;
}
cout<<“—————————————————————\n”;
cout<<“Total Records : “<<i<<“\t Made by : rakesh kumar on : 25-01-2019\n”;
cout<<“—————————————————————\n”;
return 0;
}

Passing Structure as a function Parameter- Method -1

// strcuture as a parameter
#include<iostream>
#include<string.h>
using namespace std;

struct student{
int roll;
char name[30];
};
//void print_result(int x, char y[])
//{
// cout<<“\n Roll No :”<<x;
// cout<<“\n Name :”<<y;
//}

void print_result(student x){
cout<<“\n Roll NO “<<x.roll;
cout<<“\n Name :” <<x.name;
}
int main(){
student s ={10,”Aditya”};

// print_result(s.roll, s.name);
print_result(s);
return 0;
}

Structure as a function return type Value

// strcuture as a parameter
#include<iostream>
#include<string.h>
using namespace std;

struct student{
int roll;
char name[30];
};

student read_data(){ // structure as a function return type value
student r;
cout<<“\n Enter Roll No :”;
cin>>r.roll;
cout<<“\n Enter Name :”;
cin>>r.name;
return(r);
}
int main(){
student s;
s=read_data();
cout<<“\n Roll No:”<<s.roll;
cout<<“\n Name :”<<s.name;
return 0;
}

Passing structure as Call by Value

#include<iostream>
#include<string.h>
using namespace std;

struct student{
int roll;
char name[30];
};

void change_data(student r){ // Call by value
r.roll = 10;
strcpy(r.name,”Sudhakar”);
}
int main(){
student s={20,”Rakesh Kumar”};

cout<<“\n Value Before function call\n”;
cout<<“\n Roll No:”<<s.roll;
cout<<“\n Name :”<<s.name;

change_data(s);

cout<<“\n\n Value after function call\n”;
cout<<“\n Roll No:”<<s.roll;
cout<<“\n Name :”<<s.name;
return 0;
}

Passing Structure as Call by Reference

#include<iostream>
#include<string.h>
using namespace std;

struct student{
int roll;
char name[30];
};

void change_data(student &r){    // Call by value
r.roll = 10;
strcpy(r.name,”Sudhakar”);
}
int main(){
student s={20,”Rakesh Kumar”};

cout<<“\n Value Before function call\n”;
cout<<“\n Roll No:”<<s.roll;
cout<<“\n Name :”<<s.name;

change_data(s);

cout<<“\n\n Value after function call\n”;
cout<<“\n Roll No:”<<s.roll;
cout<<“\n Name :”<<s.name;
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *