Question

Write a C++ program that reads a flat file of the following format. Input 42 1...

Write a C++ program that reads a flat file of the following format.

Input

42

1 John Curry 5 2345.34 345.40 569.89 4560.45 450.00

2 Abigail Right 0

3 Jack Monstrom 2 34569.00 3456.45

….

42 Evelyn Madim 0

Instructions

The number in the first row indicates the number of rows/managers in the file. In each row, the first number is the UIN of the manager followed by his first name and last name, the number of customers and the revenue for a month from each of his customers.

a)       Write a function that prints the file into another file named Revenues. Name the file Copy_Read

b)      Write a function that calculates the average revenue and the total revenue for each manager. The function should return the UIN of the manager and pass the Average revenue and Total Revenue for the manager as references.

c)       Write a function that uses the function in b to create an array of average revenue and an array of total revenue for all managers. – You can assume that the file may not have more than 100 rows.

d)      Write a function that uses the function in b to write a file that is similar to the read file but at each row, the last two numbers are the average and Total Revenue. The new file should also have headers for all the columns and the columns should be aligned. You can assume that no first or last name is bigger than 15 characters. Name the file Revenues

e)      Write a function that finds the two top managers in terms of total revenue and returns their UINs

f)        Write a function that calculates the two bottom managers in terms of total revenue and returns their UINs

g)       Write a function that calculates the Total Revenue for all managers.

h)      Write a function that prints a report in a file. Name the file Final_Report.

The report should include:

  1. The total Revenue for all managers.
  2. The total number of customers.
  3. The Names along with the average and total revenue and number of customers for the two top managers.
  4. The names along with the average and total revenue and number of customers for the lagging two bottom managers.

    For this program you can use either arrays or structures.

    Should be one .cpp file and one executable file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ PROGRAM:

#include <iostream>
#include<fstream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<iomanip>
using namespace std;
#define MAX 100
struct Manager{
string UIN;
string first_name;
string last_name;
int count;
double* revenue;

double avg_revenue;
double total_revenue;

};
void printManager(struct Manager array[],int cnt){
cout<<"\n\n";
for(int i=0;i<cnt;i++)
{
cout<<"\n"<<setw(3)<<array[i].UIN<<setw(15)<<array[i].first_name<<setw(15)<<array[i].last_name<<setw(10)<<array[i].count;
for(int j=0;j<array[i].count;j++)
{
cout<<setw(10)<<array[i].revenue[j];
}

cout<<setw(10)<<array[i].avg_revenue<<setw(10)<<array[i].total_revenue;
}

}

vector<string> getWords(char* line){
vector<string> v;
string word="";

for(size_t i=0;i<strlen(line);i++){

if(line[i]==' '){
v.push_back(word);
word="";
}
else {
word=word+line[i];
}

}
v.push_back(word);
return v;

}


void copyFile(char* inputFile,char* outFile){
ifstream myfile (inputFile);
ofstream output (outFile);
char line[100];


if (myfile.is_open()){
while ( myfile.getline (line,100)){
output<<line<<"\n";
}
}

myfile.close();
}


string calculateAvgAndTotalRevenue(struct Manager &manager,double& avg,double& total)
{
double sum=0;
double average=0;
for(int i=0;i<manager.count;i++){
sum+=manager.revenue[i];
}
if(manager.count!=0)
average=sum/manager.count;

manager.total_revenue=sum;
manager.avg_revenue=average;

avg=average;
total=sum;

return manager.UIN;
}


void createArray(struct Manager manager[MAX],int cnt){
double* avgArray=new double[cnt];
double* totalArray=new double[cnt];

for(int i=0;i<cnt;i++){
calculateAvgAndTotalRevenue(manager[i],avgArray[i],totalArray[i]);
}

cout<<"\nUIN\tTotal\tAvg\n";
for(int i=0;i<cnt;i++){
cout<<"\n"<<manager[i].UIN<<"\t"<<totalArray[i]<<"\t"<<avgArray[i];
}
}

string topManagerTotal(struct Manager manager[MAX], int cnt){
double first=-1;
double second=-1;
int first_UIN=-1;
int second_UIN=-1;

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

if (manager[i].total_revenue > first)
{
second = first;
first = manager[i].total_revenue;
second_UIN=first_UIN;
first_UIN=i;
}

else if (manager[i].total_revenue > second && manager[i].total_revenue != first){
second = manager[i].total_revenue;
second_UIN=i;
}
}

string answer="";
answer+=manager[first_UIN].UIN+"\t";
answer+=manager[second_UIN].UIN;
return answer;
}

string bottomManagerTotal(struct Manager manager[MAX], int cnt){
double first=99999;
double second=99999;
int first_UIN=-1;
int second_UIN=-1;

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

if (manager[i].total_revenue < first)
{
second = first;
first = manager[i].total_revenue;
second_UIN=first_UIN;
first_UIN=i;
}

else if (manager[i].total_revenue < second && manager[i].total_revenue != first){
second = manager[i].total_revenue;
second_UIN=i;
}
}

string answer="";
answer+=manager[first_UIN].UIN+"\t";
answer+=manager[second_UIN].UIN;
return answer;
}


double totalRevenue(struct Manager manager[MAX],int cnt){
double total=0;
for(int i=0;i<cnt;i++){
total+=manager[i].total_revenue;
}
return total;
}


void writeFinalOutput(struct Manager array[MAX],int cnt){

char* outputFile="Revenue.txt";
ofstream output (outputFile);

output<<cnt<<"\n";

for(int i=0;i<cnt;i++){
output<<"\n"<<setw(3)<<array[i].UIN<<setw(15)<<array[i].first_name<<setw(15)<<array[i].last_name<<setw(10)<<array[i].count;
for(int j=0;j<array[i].count;j++)
{
output<<setw(10)<<array[i].revenue[j];
}

output<<setw(10)<<array[i].avg_revenue<<setw(10)<<array[i].total_revenue;

}

}


int main()
{
struct Manager managers[MAX];
int managerIndex=0;
char* inputFile="input.txt";
char* outputFile=" Copy_Read.txt";
char line[100];

copyFile(inputFile,outputFile);

ifstream myfile (inputFile);
ofstream output (outputFile);

int managerCnt=0;
int linecount=0;

if (myfile.is_open()){
while ( myfile.getline (line,100)){
vector<string> v;
linecount++;

if(linecount==1)
{
managerCnt=atoi(line);
cout<<"\nManagerCnt = "<<managerCnt;
continue;
}
v=getWords(line);

managers[managerIndex].UIN=v[0];
managers[managerIndex].first_name=v[1];
managers[managerIndex].last_name=v[2];
char char_array[10];
strcpy(char_array, v[3].c_str());
int cnt=atoi(char_array);
managers[managerIndex].count=cnt;
managers[managerIndex].revenue=new double[cnt];
for(int i=0;i<cnt;i++){
strcpy(char_array, v[4+i].c_str());
managers[managerIndex].revenue[i]=atof(char_array);
}
managers[managerIndex].avg_revenue=0;
managers[managerIndex].total_revenue=0;

managerIndex++;

}
}

createArray(managers,managerIndex);
printManager(managers,managerIndex);
myfile.close();

cout<<"\n\n\nFirst TOP two UIN of managers with High Total Salary =\t"<<topManagerTotal(managers,managerIndex);
cout<<"\nBOTTOM two UIN of managers with High Total Salary =\t"<<bottomManagerTotal(managers,managerIndex);
cout<<"\nTotal Revenues of all managers = "<<totalRevenue(managers,managerIndex);

copyFile(inputFile,outputFile);
writeFinalOutput(managers,managerIndex);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads a flat file of the following format. Input 42 1...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • The picture is given in a PPM file and your program should put the converted one...

    The picture is given in a PPM file and your program should put the converted one into another PPM file. •Use argv[1] for the given file and argv[2] for the converted file.In addition, you can use a temporary file called tmp.ppm. •The number of rows and columns are not fixed numbers. •The converted file should also follow the PPM format with the above simplification, and can be converted subsequently. •Read the pixel matrix into a buffer. •For each row i(...

  • C++ programming language Write a program that asks the user for a file name. The file...

    C++ programming language Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in...

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a...

    Program 5 Due 10/25 C-String and Two-dimensional Array Use An input data file starts with a student's name (on one line). Then, for each course the student took last semester, the file has 2 data lines. The course name is on the first line. The second line has the student's grade average (0 to 100) and the number of credits for the course Sample data: Jon P. Washington, Jr. Computer Science I 81 4 PreCalculus 75 3 Biology I 88...

  • You must assume that the data file and your four function subprograms are located inside the working directory (forder). Please write the main program and each of the four function subprograms. 1. Th...

    You must assume that the data file and your four function subprograms are located inside the working directory (forder). Please write the main program and each of the four function subprograms. 1. The data in the following table are to be read from the file "grades.txt"and processed The grades are in percentages. The format of the data is shown below First Name ID Number Last Name Grades 001AA Tam 78.50 oe 86.45 001AB Gabriel Stuart Thus, there are 4 columns...

  • Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a...

    Create a java project. Create a class called cls2DarrayProcessor.java. with the following: Reads numbers from a pre-defined text file. A: Text file name should be data.txt B: Text file should be in the same workspace directory of your project's folder C: Text file name should be STORED in a String and called: String strFile ='./data.txt' Defines a 2-D array of integers with dimensions of 5 rows by 5 columns. Inserts the data from the text file to the 2-D array...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT