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:
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".
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!
Get Answers For Free
Most questions answered within 1 hours.