Use the same scenario that we dealt in Problem 1. But now, there is a change in
ID: 3835175 • Letter: U
Question
Use the same scenario that we dealt in Problem 1. But now, there is a change in requirement. You need to define an array of Employee instances, not just one instant. Assume array size of 20. This is how you can do it Employee emp[20]; Now. read the parameters from the file and assign it to suitable variable. In Problem 1, since you had one instance, you were continuously updating the variable as you read the next line. In this problem, since you are using arrays, you can store each line/set of parameters in different array location. This is how you can do it, for example, emp[i].salary=read_salary;//Assign salary read from file to salary variable of emp(i) structure instance Now, write a programmer defined function of void return type that will sort the array elements in the order of increasing salary. Pass your array to this function. Display the sorted list in the main function.Explanation / Answer
//Program using C++ to sort the employee's salary using bubble sort
#include<iostream>
using namespace std;
void sortarr(int a[20]){
int i,j,temp;
for(i=1;i<20;++i)
{
for(j=0;j<(20-i);++j){
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"Employee Salary after bubble sort:";
for(i=0;i<20;++i)
cout<<" "<<a[i];
}
int main()
{
int a[20],n,i,j;
cout<<"Enter the Salary of employees: ";
for(i=0;i<20;++i)
cin>>a[i];
sortarr(a);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.