I need to write a program for class. I'm not necessarily looking for code, just help. I need to file input data in this format:
01 - 00:53 - 70
01 - 02:45 - 50
01 - 03:31 - 79
01 - 04:30 - 56
01 - 05:55 - 59
01 - 07:23 - 87
it is day/time/count. I need to take this data and calculate a max and average. The data file has maybe almost a thousand entries over the span of twenty days. Each day has a different amount of counts. I wanted to use a 3d array(array [time][count][day], but there would be empty elements messing with my math. What is the best way to go about this? Or am I wrong and the math would be fine?

Recommended Answers

All 2 Replies

t:
Why not convert the file into a ordered vector/list of time:count, using a structure
to represent the time. e.g.

struct Item
{
int day;
int timeSec; // Number of seconds from midnight
int count;
};

Then you can use a std::vector of Item, which you can defined a less than operator to
allow you to sort it, binary search works well to find stuff like the first of day 3. [You might want to keep an index file or days, or max counts per day etc, or whatever is very often accessed.]

If you have a 2D array even for each minute of every day over 20 days, think of the SIZE of the array! 20*60*24, that is already 28800 entries. If you ever need second resolution life gets very unpleasent.

I do not see the logic in using a 3D array, surely count is the data, so it should not be an index of an array in most normal circumstances [There are exceptions].
If you add counts to your array, even if all counts are below 100 you at 2.8million. It will be fine but it is getting very non-extensible.

No there is no need for a 3d array. You need to use structures. Here is some things to get you going :

struct Item{
 string day;
 string time;
 int count;
}

std::istream& operator >>(istream& stream, Item& item){
  //file format is always : "day - time - count"
  //so we read in  the day, then dump the '-'. Then read in time and dump the '-'....
  char temp = 0;
 return stream >> item.day >> temp >> item.time >>  >> temp >> item.count;
}
bool sortByDayTimeCount(const Item& lhs, const Item& rhs){
 if(lhs.day != rhs.day) return lhs.day < rhs.day; //sort by day first
 else if(lhs.time != rhs.time) return lhs.time < rhs.time; //if same day sort by time
 else return lhs.cout < rhs.count; //else sort by count
}
int main(){
 std::vector<Item> data;
 istream file("data.txt");
 Item item;
 while(file >> item){
   data.push_back(item);
 }
 std::sort(data.begin(),data.end(),sortByDatTimeCount);
 for(int i = 0; i != data.size(); ++i){ 
     cout << data.day << " - " << data.time << " - " << data.count << endl;
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.