***Write these programs in c++ and include console comments for every line*** Ta
ID: 3594986 • Letter: #
Question
***Write these programs in c++ and include console comments for every line***
Task 8.2. Write a function that reorders the values in three integer variables such that the values are in ascending order. Assume that the following function prototype is used. void reorder(int *a,int *b,int *c); where a, b, and c are pointers to the three variables. Write a main function to test the reorder function Task 8.3. Use the following structure for this problem. struct Emp char EmpName [25]; int id; float Salary; l; Declare an array called employee[5] of type struct Emp. Use GetData() to read in values for the array and use PrintData()to print out the array elements and use GetAverage() function to calculate the average salary of the employees. // function prototypes struct Emp GetData); void PrintData (struct Emp); double GetAverage (struct Emp [], int);Explanation / Answer
#include<iostream>
using namespace std;
//Function to reorder three numbers in ascending order
void reorder(int *a, int *b, int *c)
{
//Temporary variable for swapping
int t;
//Checks if value at a is greater than value at b then swap a and b value
if(*a > *b)
{
//Swapping process
//Stores value at a in t
t = *a;
//Stores value at b in value at a
*a = *b;
//Stores value of t in value at b
*b = t;
}//End of if
//Checks if value at b is greater than value at c then swap b and c value
if(*b > *c)
{
//Swapping process
//Stores value at b in t
t = *b;
//Stores value at c in value at b
*b = *c;
//Stores value of t in value at c
*c = t;
}//End of if
//Checks if value at a is greater than value at c then swap a and c value
if(*a > *b)
{
//Swapping process
//Stores value at a in t
t = *a;
//Stores value at b in value at a
*a = *b;
//Stores value of t in value at b
*b = t;
}//End of if
}//End of function
//Main function definition
int main()
{
//To store three numbers
int a, b, c;
//Accept three numbers
cout<<" Enter First Number: ";
cin>>a;
cout<<" Enter Second Number: ";
cin>>b;
cout<<" Enter Third number: ";
cin>>c;
//Displays values
cout<<" Before reorder: First = "<<a<<" Second = "<<b<<" Third = "<<c;
//Calls the function to reorder in ascending order
reorder(&a, &b, &c);
//Displays values
cout<<" After reorder: First = "<<a<<" Second = "<<b<<" Third = "<<c;
}//End of main function
Sample Run 1:
Enter First Number: 10
Enter Second Number: 2
Enter Third number: 3
Before reorder: First = 10 Second = 2 Third = 3
After reorder: First = 2 Second = 3 Third = 10
Sample Run 2:
Enter First Number: 2
Enter Second Number: 20
Enter Third number: 10
Before reorder: First = 2 Second = 20 Third = 10
After reorder: First = 2 Second = 10 Third = 20
Sample Run 3:
Enter First Number: 11
Enter Second Number: 2
Enter Third number: 30
Before reorder: First = 11 Second = 2 Third = 30
After reorder: First = 2 Second = 11 Third = 30
------------------------------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
//Structure Emp definition
struct Emp
{
//To store employee name
char EmpName[25];
//To store employee id
int id;
//To store employee salary
float salary;
};//End of structure
//Function to accept employee data and return Emp object
struct Emp GetData()
{
//Creates a temporary Emp object
struct Emp e;
//Accepts employee data
cout<<" Enter employee name: ";
cin>>e.EmpName;
cout<<" Enter employee id: ";
cin>>e.id;
cout<<" Enter employee salary: ";
cin>>e.salary;
//Returns the employee object
return e;
}//End of function
//Function takes structure object as parameter and displays employee information
void PrintData(struct Emp e)
{
cout<<" Employee name: "<<e.EmpName<<" Employee id: "<<e.id<<" Employee salary: "<<e.salary;
}//End of function
//Function takes an array of structure object and length
//Calculates the average salary and returns it
double GetAverage(struct Emp e[], int len)
{
//To store total and average salary
double tot, avg;
//Loops till length
for(int c = 0; c < len; c++)
//Calculates total salary
tot += e[c].salary;
//Calculates average salary
avg = tot / len;
//Returns average salary
return avg;
}//End of function
//Main function definition
int main()
{
//Declares an array of objects for employee
struct Emp e[5];
//Accepts data for each employee
cout<<" Enter employee data ";
for(int c = 0; c < 5; c++)
e[c] = GetData();
//Accepts data of each employee
cout<<" Employee Information ";
for(int c = 0; c < 5; c++)
PrintData(e[c]);
//Displays average salary by calling the function
cout<<" Average Salary: "<<GetAverage(e, 5);
}//End of main function
Sample Run:
Enter employee data
Enter employee name: Pyari
Enter employee id: 101
Enter employee salary: 1000
Enter employee name: Mohan
Enter employee id: 102
Enter employee salary: 2000
Enter employee name: Ram
Enter employee id: 105
Enter employee salary: 4000
Enter employee name: Sasmita
Enter employee id: 109
Enter employee salary: 5000
Enter employee name: Bishnu
Enter employee id: 110
Enter employee salary: 8000
Employee Information
Employee name: Pyari
Employee id: 101
Employee salary: 1000
Employee name: Mohan
Employee id: 102
Employee salary: 2000
Employee name: Ram
Employee id: 105
Employee salary: 4000
Employee name: Sasmita
Employee id: 109
Employee salary: 5000
Employee name: Bishnu
Employee id: 110
Employee salary: 8000
Average Salary: 4000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.