tags:

views:

100

answers:

2
+1  A: 

My first suggestion is that the prof wants a list of Record - rather than hardcoding in 19, use a vector and fill it as you go.

vector<Record> records;

My second suggestion is to use a loop to process each month.

vector<string> months;
months.push_back("Jan");
months.push_back("Feb");

for (int i = 0; i < months.size(); ++i) {
  string month = months[i];
  ReadInMonthBudget(&records, month); // Then use a function to read in each month file. 
}

Then you can work on collecting your stats:

for (int i = 0; i < months.size(); ++i) {
  string month = months[i];
  cout << "Min spender in month " << month << ": " << FindMinSpenderInMonth(records, month);
}

Using functions will make your code much easier to understand (and write), because it breaks the problem down into much smaller chunks that are easier to solve.

Start by getting it working, then improve on it. If this is a college level class, your professor may want you to come up with a better than O(n^2) solution.

Stephen
+1  A: 

The first thing I recommend you is improving the legibility of your program. Try adding functions that will perform the simple operations for you, for example, reading a single line of input into the struct (BTW, there is one more field in the struct that in the input).

Declare your variables close to where you need them, there is no point in declaring upfront everything you will need, you can even forget what it was by the time you actually use it. Use data structures that simplify the code (instead of N min_month, max_month variables just use a vector of minimum / maximum values indexed by month; use vectors instead of arrays; if you find that you need lookup tables, consider using maps...)

But first of all, implement your algorithm with pen and paper. I had a professor that used to say that until you know how to implement a process with pen and paper there was no point in even considering implementing it in a computer as it would just fail in a lot faster and be harder to diagnose.

David Rodríguez - dribeas
BTW: If you can grab a copy of Accelerated C++, I would start reading it... It has some examples similar to what you want and offers an has an idiomatic approach
David Rodríguez - dribeas