Question

C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional...

C++ Programming Programming

Project Outcomes:

 Understand and use C++ functions for procedural abstraction and Functional Decomposition

 Develop some functions for generating numerical data from numerical inputs

 Understand and utilize file input and file output to perform computing tasks

Requirements:

1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc.

2. The program should prompt the user for the output file name only. Obviously, this should be taken care of in a function.

3. Sales Records are in a text file named TheSales.txt. You MUST use this file name for the input.

a. The first entry will be an integer that tells how many sales people there are.

b. Following that will be an integer that tells you how many weeks of data are in the file.

c. Following that integer will be a sales person's first name, middle initial, and last name,

d. Followed by the daily sales for that employee for the proper number of weeks.

e. Each week will have FIVE days of sales data as doubles. So, a file for 3 sales people and 2 weeks might look like this:

3

2

firstName1 A lastName1

20.00 25.00 30.90 40.00 55.50 20.00 25.00 30.90 40.00 55.50

firstname2 B lastName2

30.00 24.00 45.00 67.00 65.50 56.90 87.00 43.50 56.98 55.40

firstName3 C lastName3

62.00 34.50 12.50 34.00 34.90 70.00 80.00 90.00 65.00 39.00 4.

The program must be able to handle however many salespeople are specified (1 to 10 salespeople) and however many weeks of data are specified (1 to 10 weeks). The data must be organized exactly as specified above. Be sure to read the file that way. 5. The output will go to an output file name that you get from the user, with a format that you must invent. The file must contain the following, all of which must be properly labeled so that it is easy to know what the numbers refer to

a. The number of sales people that were processed.

b. The number of weeks of sales that were processed.

c. The last name only of each salesperson followed by total and average sales for each week.

d. Grand total sales and average sales per week for all salespersons over all weeks.

6. If the input file or output file opening fails, print an error message and exit.

Implementation Notes: You will need to declare several variables. Try to use the least number of variables possible. You may assume that all weeks will have 5 days of sales reported. You can assume the correct number of names and middle initials will be there and the correct number of sales figures.

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

// C++ program to calculate the total and average sales

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

#define MAX_SALESPERSON 10

#define MAX_WEEK 10

#define NUM_DAYS_IN_WEEK 5

// function declaration

void readFile(ifstream &fin, string name[][3],double data[][MAX_WEEK][NUM_DAYS_IN_WEEK], int &num_salesperson, int &num_week);

void writeFile(string name[][3],double data[][MAX_WEEK][NUM_DAYS_IN_WEEK],int num_salesperson, int num_week);

void calTotalSales(double data[][MAX_WEEK][NUM_DAYS_IN_WEEK], double total_sales[][MAX_WEEK], int num_salesperson, int num_week);

int main() {

               ifstream fin("TheSales.txt"); // open input file, provide full path to file

               int num_salesperson = 0, num_week=0;

               string name[MAX_SALESPERSON][3];

               double data[MAX_SALESPERSON][MAX_WEEK][NUM_DAYS_IN_WEEK];

               // if file can be opened

               if(fin.is_open())

               {

                              readFile(fin,name,data,num_salesperson,num_week); // read data from file to array

                              writeFile(name,data,num_salesperson,num_week); // write data from array to output file

                              fin.close(); //close the file

               }else

                              cout<<"Unable to open TheSales.txt"<<endl;

               return 0;

}

// function to read data from input file to arrays

void readFile(ifstream &fin, string name[][3],double data[][MAX_WEEK][NUM_DAYS_IN_WEEK], int &num_salesperson, int &num_week)

{

               fin>>num_salesperson>>num_week;

               int salesperson=0,week=0,day=0;

               while(!fin.eof()) // read till the end of file

               {

                              fin>>name[salesperson][0]>>name[salesperson][1]>>name[salesperson][2];

                              week = 0;

                              while(week < num_week)

                              {

                                             day = 0;

                                             while(day < NUM_DAYS_IN_WEEK)

                                             {

                                                            fin>>data[salesperson][week][day];

                                                            day++;

                                             }

                                             week++;

                              }

                              salesperson++;

               }

}

// function to calculate the total sales for each salesperson for each week

void calTotalSales(double data[][MAX_WEEK][NUM_DAYS_IN_WEEK], double total_sales[][MAX_WEEK], int num_salesperson, int num_week)

{

               for(int i=0;i<num_salesperson;i++)

               {

                              for(int j=0;j<num_week;j++)

                              {

                                             total_sales[i][j] = 0;

                                             for(int k=0;k<NUM_DAYS_IN_WEEK;k++)

                                             {

                                                            total_sales[i][j] += data[i][j][k];

                                             }

                              }

               }

}

// function to write the data for total and average sales to output file

void writeFile(string name[][3],double data[][MAX_WEEK][NUM_DAYS_IN_WEEK],int num_salesperson, int num_week)

{

               string filename;

               cout<<"Enter the name of the output file : ";

               cin>>filename;

               ofstream fout(filename.c_str());

               if(fout.is_open())

               {

                              double total_sales[num_salesperson][MAX_WEEK];

                              fout<<"Number of sales people processed : "<<num_salesperson<<endl;

                              fout<<"Number of weeks of sales processed : "<<num_week<<endl;

                              calTotalSales(data,total_sales,num_salesperson, num_week);

                              fout<<left<<setw(30)<<"Lastname"<<left<<setw(10)<<"Week"<<left<<setw(15)<<"Total Sales"<<left<<setw(15)<<"Average Sales"<<endl<<string(100,'-')<<endl;

                              for(int i=0;i<num_salesperson;i++)

                              {

                                             for(int j=0;j<num_week;j++)

                                             {

                                                            fout<<left<<setw(30)<<name[i][2]<<left<<setw(10)<<(j+1)<<left<<setw(15)<<fixed<<setprecision(2)<<total_sales[i][j]<<left<<left<<setw(15)

                                                                                          <<(total_sales[i][j])/NUM_DAYS_IN_WEEK<<endl;

                                             }

                              }

                              fout<<endl<<left<<setw(10)<<"Week"<<left<<setw(15)<<"Total Sales"<<left<<setw(15)<<"Average Sales"<<endl<<string(50,'-')<<endl;

                              double grand_total = 0;

                              for(int i=0;i<num_week;i++)

                              {

                                             grand_total = 0;

                                             for(int j=0;j<num_salesperson;j++)

                                                            grand_total += total_sales[j][i];

                                             fout<<left<<setw(10)<<(i+1)<<left<<setw(15)<<fixed<<setprecision(2)<<grand_total<<left<<setw(15)<<(grand_total/num_salesperson)<<endl;

                              }

                              fout.close();

               }else

                              cout<<"Unable to open output file : "<<filename<<endl;

}

//end of program

Output:

Input file:

Console:

Output File:

Add a comment
Know the answer?
Add Answer to:
C++ Programming Programming Project Outcomes:  Understand and use C++ functions for procedural abstraction and Functional...
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
  • C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales...

    C++ Objective: Write a program to read a pre-specified file containing salespersons' names and daily sales for some number of weeks. It will create an output file with a file name you have gotten from the user that will hold a summary of sales data. Specifications: 1. You should do a functional decomposition to identify the functions that you will use in the program. These should include reading and writing the files, tallying and showing statistics, etc. 2. The program...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an...

    C- PROGRAMMING PROJECT #4 Design and Write a C program to calculate an average of an array of numbers and produce the following output: 1. Your first and last name 2. Course Number 3. C Project Number 4. All the numbers in the array 5. The sum of all the numbers in the array 6. The count of all the numbers in the array 7. The average of all the numbers in the array Double-space after lines 3, 4, 5,...

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • CIS 1111 Programming Files

    Description:In this assignment, you will write a program in C++ that uses files and nested loops to create a file for real estate agents home sales and then read the sales from the file and calculates the average sale for each agent. Each agent sold 4 homes.  Use a nested loop to write each agent’s sales to a file. Then read the data from the file in order to display the agent’s average sale and the average for all sales....

  • (C++ programming) Need help with homework. Write a program that can be used to gather statistical...

    (C++ programming) Need help with homework. Write a program that can be used to gather statistical data about the number of hours per week college students play video games. The program should perform the following steps: 1). Read data from the input file into a dynamically allocated array. The first number in the input file, n, represents the number of students that were surveyed. First read this number then use it to dynamically allocate an array of n integers. On...

  • Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read...

    Programming Assignment 1 Structures, arrays of structures, functions, header files, multiple code files Program description: Read and process a file containing customer purchase data for books. The books available for purchase will be read from a separate data file. Process the customer sales and produce a report of the sales and the remaining book inventory. You are to read a data file (customerList.txt, provided) containing customer book purchasing data. Create a structure to contain the information. The structure will contain...

  • c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did...

    c++ CSI Lab 6 This program is to be written to accomplish same objectives, as did the program Except this time we modularize our program by writing functions, instead of writing the entire code in main. The program must have the following functions (names and purposes given below). Fot o tentoutefill datere nedefremfite You will have to decide as to what arguments must be passed to the functions and whether such passing must be by value or by reference or...

  • have to create five different functions above and call it in the main fucntion. Project Exam...

    have to create five different functions above and call it in the main fucntion. Project Exam Statistics A CIS 22A class has two midterm exams with a score between 0 and 100 each. Fractional scores, such as 88.3 are not allowed. The students' ids and midterm exam scores are stored in a text file as shown below // id exam1 exam2 DH232 89 92 Write a program that reads data from an input file named exams.txt, calculates the average of...

  • Using C++ programming. This exercise will test your knowledge of passing structures to functions. Create a...

    Using C++ programming. This exercise will test your knowledge of passing structures to functions. Create a structure that stores information about various plumbers that you are getting repair estimates from. As you can see from the output, the structure must store the name, phone number, and estimate. Since you are only getting estimates from two companies, only two structure variables are needed. Functions: 1) Get data from the user. This function must be called TWICE. 2) Print the summary you...

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
ADVERTISEMENT