Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cs-142:computers [2015/01/07 16:13] (current)
ryancha created
Line 1: Line 1:
 +This example alludes to in the [[Teams#​Think_before_programming|"​Think before programming"​]] section of the Teams lab page about computer usage. We'll say that the file contains the usage for computers on campus for a week's period. Say that you have a file that contains something like the teams lab does with the first column being the computer ID, the second column being the number of users who have used that computer, and the third column being the total number of hours. For example:
  
 + ​NOSHHZONE001 25 145
 + ​NOSHHZONE002 29 152
 + ​NOSHHZONE003 27 165
 + ​NOSHHZONE004 18 35
 +
 +You need to read in the file, create a class to store the data (presumably a Computer class), create a vector<​Computer *> to store the instances of computer and then use the objects stored in the vector to answer questions like:
 +#Which computer had the most users during the week?
 +#Which computer had the highest average number of hours per user (i.e. total hours / total users)?
 +#What is the average number of hours per user across all computers in the file?
 +
 +
 +<code cpp>
 +// Test Case 1
 +//  INPUT: testcase.txt
 +// NOSHHZONE001 25 145
 +// NOSHHZONE002 29 152
 +// NOSHHZONE003 1 1000 ​ <-- Test case category: One user
 +// NOSHHZONE004 0 0 ​    <​-- Test case category: No users, no hours
 +// NOSHHZONE005 1 0 ​    <​-- Test case category: One user, no hours
 +//
 +// EXPECTED OUTPUT:
 +// Which computer had the most users during the week? NOSHHZONE002
 +// Which computer had the highest average number of hours per user (i.e. total hours / total users)? NOSHHZONE003
 +// What is the average number of hours per user across all computers in the file? 23.16 hours
 +
 +#include <​iostream>​
 +#include <​fstream>​
 +#include <​vector>​
 +
 +using namespace std;
 +
 +// HERE IS MY CLASS DEFINITION
 +class Computer{
 +public:
 + Computer();​
 + Computer(string new_id, int new_num_hours,​ int new_num_users);​
 + string get_id() const;
 + int get_num_hours() const;
 + int get_num_users() const;
 +
 +private:
 + string id;
 + int num_hours;
 + int num_users;
 +};
 +
 +// HERE I DEFINE MY CLASS CONSTRUCTORS
 +Computer::​Computer(string new_id, int new_num_users,​ int new_num_hours){
 + id = new_id;
 + num_users = new_num_users;​
 + num_hours = new_num_hours;​
 +}
 +
 +// HERE I DEFINE MY CLASS FUNCTIONS
 +string Computer::​get_id() const{
 + return id;
 +}
 +
 +int Computer::​get_num_hours() const{
 + return num_hours;
 +}
 +int Computer::​get_num_users() const{
 + return num_users;
 +}
 +
 +// HERE IS MY MAIN FUNCTION
 +int main(){
 + // GET THE DATA
 + // Ask user for filename
 + cout << "​Please,​ dear user, give me a filename:";​
 +
 + // Get the input
 + string filename;
 + cin >> filename;
 + cout << endl << "​Opening " << filename << endl;
 +
 + // Open the file
 + ifstream in_file;
 + in_file.open(filename.c_str());​
 +
 + // Read in the file
 + string computer_id;​
 + int num_users;
 + int num_hours;
 + vector<​Computer *> computers;
 + while(in_file >> computer_id >> num_users >> num_hours){
 + // Parse the data in the file
 + //cout << "You made to this point in the code"<<​endl;​
 + cout << computer_id << " had " << num_users << " users (" << num_hours << "​)"​ << endl;
 + // Use a class to store the data
 + Computer * new_computer = new Computer(computer_id,​ num_users, num_hours);
 + cout << new_computer->​get_id() << " had " << new_computer->​get_num_users() << " users (" << new_computer->​get_num_hours() << "​)"​ << endl;
 + computers.push_back(new_computer);​
 + }
 +
 + // ANALYZE THE DATA
 +        // Answer question 1, computer with most hours
 + string computer_id_with_most_hours = "No Computer";​
 + int max_hours = 0;
 + // Look through the hours
 + for(int i = 0; i < computers.size();​ i++){
 + // Compare and find the computer with the most hours
 + if(computers[i]->​get_num_hours() > max_hours){
 + max_hours = computers[i]->​get_num_hours();​
 + computer_id_with_most_hours = computers[i]->​get_id();​
 + }
 + }
 + // print out computer with most hours (answer question 1)
 + cout << "The computer with the most hours is " << computer_id_with_most_hours << "​("​ << max_hours << "​)"​ << endl;
 +
 +        // Answer question 2
 +        // Answer question 3
 +}</​code>​
cs-142/computers.txt ยท Last modified: 2015/01/07 16:13 by ryancha
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0