Question

Manipulating structured data Purpose This two-part assignment involves manipulating "structured" data, similar to what might be...

Manipulating structured data

Purpose

This two-part assignment involves manipulating "structured" data, similar to what might be analyzed with a spreadsheet. This first part requires you to develop pseudocode, which allows you to think about the operations needed without having to follow precise C++ syntax. Pseudocode captures the major operations such as initializations, calculations, loops and decisions. Assignment 6 provided a pseudocode example. The second part of this assignment will translate the pseudocode into C++.

The problem

Every week a company generates a text file containing one line per employee. The lines contain the employee's first name, last name, and total minutes spent during the last week using a browser to view external web sites. These three fields are separated by "white space" (blanks and/or tab characters). The current number of employees is stored in the symbolic value NUM_EMPLOYEES.

Develop an algorithm to:

  1. Read each of the NUM_EMPLOYEES lines into an array entry with first name, last name and browser minutes fields.
  2. Compute and display the average number of minutes viewing external web sites for all employees.
  3. Display the first name, last name, and minutes spent viewing external web sites for all employees who exceed more than twice the average.

Unlike C++, pseudocode does not require you to declare variables before they are used. You can just simply state an operation such as:

    read first name, last name and browser minutes into employee[i]

Don't you wish that the C++ compiler was as forgiving with syntax? The pseudocode does not need to be concerned with the C++-specific details of reading from or writing to the console. However you do need to include all the arithmetic and loop conditions needed to perform calculations rather than just say something like "compute the average browser minutes".

Homework Answers

Answer #1

The pseudocode is like this:

//this is the function which will perform all tasks in the problem
PERFORM_TASK(filename, NUM_EMPLOYEES){

    create an object of fstream class and open data file,filename

    create a 2D array emp_info[NUM_EMPLOYEES][3] 
    for(i = 1 to NUM_EMPLOYEES){

        read a line from the file and store it in temp variable

        tokenize the temp string with " " as delimeter  and read first name, last name and browser minutes into emp_info[i]
    }

    initialize a variable SUM as 0
    for(j = 1 to NUM_EMPLOYEES){
        SUM += emp_info[i][2]
    }

    AVERAGE = SUM / NUM_EMPLOYEES
    display(AVERAGE)

    for(j = 1 to NUM_EMPLOYEES){
        
        if(emp_info[2] > 2*AVERAGE){
            show first name, last name and minutes spent in viewing external sites
        }
    }
}

C++ program is below:

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

//macro to define no of employees in data file
#define NUM_EMPLOYEES 5

int main(){

    string filename = "data.txt";

    //create a fstream object to open files
    fstream data_file;

    //open data file
    data_file.open(filename);

    //check if file is open or not

    //if file is open
    if(data_file.is_open()){

        //creating emp_info array to store info from file to array
        string emp_info[NUM_EMPLOYEES][3];

        //loop over to load data of NUM_EMPLOYEES to emp_info
        for(int i = 0; i < NUM_EMPLOYEES; i++){

            string temp;
            //read a line from the file
            getline(data_file, temp);


            char* new_line = new char[temp.length() + 1];
            for(int i = 0; i < temp.length(); i++){
                new_line[i] = temp[i];
            }
            //tokenizing data with whitespace as delimeter
            //reading the first name
            char* token = strtok(new_line, " ");
            //storing the first name
            emp_info[i][0] = token;

            //reading the last name
            token = strtok(NULL, " ");
            //storing the last name
            emp_info[i][1] = token;

            //reading the minutes of viewing the external websites
            token = strtok(NULL, " ");
            //storing the minutes
            //we are storing minutes laso in string form here
            emp_info[i][2] = token;

        }

        int sum = 0;

        //finding sum of viewing minutes of all employees
        for(int j = 0; j < NUM_EMPLOYEES; j++){
            sum = sum + stoi(emp_info[j][2]);  //converting string to int
        }

        //calculating average
        double average = float(sum) / NUM_EMPLOYEES;

        cout<<"The avergae viewing minutes are "<<average<<endl;



        cout<<"Following are the employees with vuewing minutes more "<<2*average<<":"<<endl;
        //find the employees having viewing minutes more than twice the average
        for(int k = 0; k < NUM_EMPLOYEES; k++){
            if(stoi(emp_info[k][2]) > 2*average){
                cout<<emp_info[k][0]<<"\t"<<emp_info[k][1]<<"\t"<<emp_info[k][2]<<endl;
            }
        }
    }
    //if here is some error in opening file
    else{
        cout<<"Error in opening "<<filename<<endl;
    }


    return 0;
}

data.txt file:

Amit Rawat 100
Ajay Bisht 1500
Bhism rocket 3000
Om sai 600
emily sed 2000

Output:

if it helps you, do upvote as it motivates us a lot!

Know the answer?
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
Similar Questions
This is C++ Note, for part 2 of this assignment, you DO NOT NEED to use...
This is C++ Note, for part 2 of this assignment, you DO NOT NEED to use arrays or vectors. All changes, calculations, etc should be performed in place ( in the file). You may need one or two structures that temporary hold data needed to be displayed, changed, etc. Part 2: Binary Files Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or...
Use a few sentences to describe the problem given below . Also, Identify the nouns and...
Use a few sentences to describe the problem given below . Also, Identify the nouns and verbs used in the below project descriptions.  Identified nouns, list the ones that are necessary to define variables in your program. For each variable, specify its name, data type, and what information it is used to store. Write the pseudo code algorithm (i.e. algorithm steps) to solve this problem. (For the base salaries and commission rates use constants instead to keep these values. Use the...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
Case 13 ✍ It Was Really So Simple Background Information Brenda Galway leaned back in her...
Case 13 ✍ It Was Really So Simple Background Information Brenda Galway leaned back in her chair, sighed heavily, and slowly rubbed her eyes in big circular motions. “I don’t need all this aggravation,” she thought to herself. She had just finished reviewing the report she had requested from her new employee, Bill Stanley. The entire report was incorrect and would have to be redone. Brenda supervised Unit B of the Audit Department. The Unit B team had earned the...
What tools could AA leaders have used to increase their awareness of internal and external issues?...
What tools could AA leaders have used to increase their awareness of internal and external issues? ???ALASKA AIRLINES: NAVIGATING CHANGE In the autumn of 2007, Alaska Airlines executives adjourned at the end of a long and stressful day in the midst of a multi-day strategic planning session. Most headed outside to relax, unwind and enjoy a bonfire on the shore of Semiahmoo Spit, outside the meeting venue in Blaine, a seaport town in northwest Washington state. Meanwhile, several members of...
Amazon CEO Jeff Bezos’ 2020 letter to shareholders details the company’s plans to combat the coronavirus...
Amazon CEO Jeff Bezos’ 2020 letter to shareholders details the company’s plans to combat the coronavirus Amazon CEO Jeff Bezos just published his annual letter to shareholders, and the efforts his company is taking to address the COVID-19 crisis were front and center. He outlined the company’s plans to build a lab to test employees and the social distancing measures its implemented in its facilities among other initiatives. Other than the coronavirus pandemic, Bezos also discussed the retail giant’s efforts...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT