College

Problem:

A list of jobs and salaries was compiled in a text file by an internet job service company. Write a program that uses a loop to read the job titles and salary ranges from this file and store the jobs and salary ranges into two arrays (using `string` and `double` as variable types, respectively). The maximum number of jobs that can be processed by this program is 20.

When done, the C++ program should:

a. Calculate and display the average salary of the different jobs using the formula \((\text{low} + \text{high})/2.0\) into a one-dimensional array (`avgSalaries[]`).

b. Identify and display the lowest and highest average salary from `avgSalaries[]` (include job titles).

c. Sort all the jobs in average salary order and then display the sorted jobs with salary ranges and corresponding average salary.

d. Provide the user with the option to write all calculated results of parts a, b, and c to the console or to the output file "job-stat.txt".

No non-constant global variables should be used, and no pointers should be used in the solution.

This is the input file format:

```
Administrative Assistant
29000 - 56000
Software Engineer
61000 - 126000
Project Manager, (Unspecified Type / General)
49000 - 112000
Operations Manager
42000 - 103000
Office Manager
34000 - 69000
Financial Analyst
46000 - 81000
Software Developer
50000 - 104000
Customer Service Representative (CSR)
28000 - 54000
Senior Software Engineer
85000 - 156000
Executive Assistant
39000 - 79000
```

Answer :

Here's a C++ program that reads job titles and salary ranges from a text file, performs the required calculations and sorting, and provides the option to display the results on the console or write them to an output file:

cpp

Copy code

#include <iostream>

#include

#include

#include

const int MAX_JOBS = 20;

// Structure to hold job details

struct Job {

std::string title;

double lowSalary;

double highSalary;

double avgSalary;

};

void readJobsFromFile(Job jobs[], int& numJobs) {

std::ifstream inputFile("jobdata.txt");

if (!inputFile) {

std::cerr << "Error opening file." << std::endl;

exit(1);

}

std::string line;

int count = 0;

while (getline(inputFile, line)) {

jobs[count].title = line;

getline(inputFile, line);

jobs[count].lowSalary = std::stod(line.substr(0, line.find('-')));

jobs[count].highSalary = std::stod(line.substr(line.find('-') + 1));

jobs[count].avgSalary = (jobs[count].lowSalary + jobs[count].highSalary) / 2.0;

count++;

if (count == MAX_JOBS)

break;

}

numJobs = count;

inputFile.close();

}

void calculateAverageSalaries(const Job jobs[], const int numJobs, double avgSalaries[]) {

for (int i = 0; i < numJobs; i++) {

avgSalaries[i] = jobs[i].avgSalary;

}

}

void displayAverageSalaries(const Job jobs[], const double avgSalaries[], const int numJobs) {

std::cout << "Average Salaries:\n";

for (int i = 0; i < numJobs; i++) {

std::cout << jobs[i].title << ": " << avgSalaries[i] << std::endl;

}

std::cout << std::endl;

}

void displayLowestHighestAverageSalary(const Job jobs[], const double avgSalaries[], const int numJobs) {

double lowest = avgSalaries[0];

double highest = avgSalaries[0];

std::string lowestTitle = jobs[0].title;

std::string highestTitle = jobs[0].title;

for (int i = 1; i < numJobs; i++) {

if (avgSalaries[i] < lowest) {

lowest = avgSalaries[i];

lowestTitle = jobs[i].title;

}

if (avgSalaries[i] > highest) {

highest = avgSalaries[i];

highestTitle = jobs[i].title;

}

}

std::cout << "Lowest Average Salary: " << lowestTitle << " (" << lowest << ")\n";

std::cout << "Highest Average Salary: " << highestTitle << " (" << highest << ")\n";

std::cout << std::endl;

}

bool compareJobsByAvgSalary(const Job& job1, const Job& job2) {

return job1.avgSalary < job2.avgSalary;

}

void sortJobsByAvgSalary(Job jobs[], const int numJobs) {

std::sort(jobs, jobs + numJobs, compareJobsByAvgSalary);

}

void displaySortedJobs(const Job jobs[], const int numJobs) {

std::cout << "Sorted Jobs:\n";

for (int i = 0; i < numJobs; i++) {

std::cout << jobs[i].title << " (" << jobs[i].lowSalary << " - " << jobs[i].highSalary << ")\n";

}

std::cout << std::endl;

}

void writeResultsToFile(const Job jobs[], const double avgSalaries[], const int numJobs) {

std::ofstream outputFile("job-stat.txt");

if (!outputFile) {

std::cerr << "Error creating output file." << std::endl;

exit(1);

}

outputFile << "Average Salaries:\n";

for (int i = 0; i < numJobs; i++) {

outputFile << jobs[i].title << ": " << avgSalaries[i] << std::endl;

}

outputFile << std::endl;

double lowest = avgSalaries[0];

double highest = avgSalaries[0];

std::string lowestTitle = jobs[0].title;

std::string highestTitle = jobs[0].title;

for (int i = 1; i < numJobs; i++) {

if (avgSalaries[i] < lowest) {

lowest = avgSalaries[i];

lowestTitle = jobs[i].title;

}

if (avgSalaries[i] > highest) {

highest = avgSalaries[i];

highestTitle = jobs[i].title;

}

}

outputFile << "Lowest Average Salary: " << lowestTitle << " (" << lowest << ")\n";

outputFile << "Highest Average Salary: " << highestTitle << " (" << highest << ")\n";

outputFile << std::endl;

outputFile << "Sorted Jobs:\n";

for (int i = 0; i < numJobs; i++) {

outputFile << jobs[i].title << " (" << jobs[i].lowSalary << " - " << jobs[i].highSalary << ")\n";

}

outputFile.close();

}

int main() {

Job jobs[MAX_JOBS

To know more about C++ program click the link below:

https://brainly.com/question/16528579

#SPJ11