1
\$\begingroup\$

First string includes 4 integers: N, M, A, B (1 ≤ N, M ≤ 300, A, B ≤ 1000). Each next line include M-symbols. Symbol . is a clean cell, * and ** are dirty.

I need to find the sum for cleaning if A its sum of ** cell, B is sum of *.

What I can do better?

#include <iostream>
#include <string>
#include <vector>

using std::cin;
using std::cout;
using std::string;
using std::vector;

int main() {
  int n, m, a, b;
  cin >> n >> m >> a >> b;

  vector<string> parquet;

  int itr = 0;

  while (itr < n) {
    string cell;
    cin >> cell;
    parquet.push_back(cell);
    itr++;
  }

  int sum = 0;

  itr = 0;

  while (itr < parquet.size()) {
    for (int i = 0; i < parquet[itr].size(); i++) {
      if (parquet[itr][i] == '*') {
        if (parquet[itr][i + 1] == '*') {
          i++;
          sum += a;
        } else {
          sum += b;
        }
      }
    }

    itr++;
  }

  cout << sum;
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Any row that ends with ".*" is going to cause you to access out of bounds coordinates. \$\endgroup\$ Commented Mar 26, 2015 at 16:24
  • \$\begingroup\$ @twohundredping I can change my if statement to if (parquet[i][j + 1] != EOF && parquet[i][j + 1] == '*'), right? \$\endgroup\$
    – rel1x
    Commented Mar 26, 2015 at 18:09
  • 1
    \$\begingroup\$ The quickest change to the current structure would be to change if (parquet[itr][i + 1] == '*') to if(i + 1 < parquet[itr].size() && parquet[itr][i+1] == '*'). \$\endgroup\$ Commented Mar 26, 2015 at 20:46

1 Answer 1

2
\$\begingroup\$

You can refine your algorithm by getting rid of all these counters and while loops and replace them with a for loop such as this:

for (int i = 0; i < parquet.size(); i++)
    parquet[i] = ... // do whatever

Similarly, you can re-write the summing algorithm as:

for (int i = 0; i < parquet.size(); i++){
    for (int j = 0; j < parquet[i].size(); j++) {
      if (parquet[i][j] == '*') && (parquet[i][j + 1] == '*') {
          j++;
          sum += a;
      else 
          sum += b;
      }
    }
}

That said, I notice you're naming your counters as "iter" (which I assume is a short for iterator). Are you trying to access your vector in a typical STL manner (i.e.: using iterators?). If that was your intent, you can try something like:

vector<string>::iterator myIterator;
for (myIterator = parquet.begin(); myIterator != parquet.end(); myIterator++)
// Do whatever

That is, of course, assuming your procedure works as intended. If not, please state otherwise.

\$\endgroup\$
3
  • \$\begingroup\$ Hi! Thank you for answer, and why for loop better than while? \$\endgroup\$
    – rel1x
    Commented Mar 26, 2015 at 17:16
  • 1
    \$\begingroup\$ Also worth noting C++11's range-based for loop \$\endgroup\$
    – glampert
    Commented Mar 26, 2015 at 17:41
  • 2
    \$\begingroup\$ @rel1x We generally use for loops when we know how many iterations there will be in advance. In both loops you know the iterations in advance and hence should use a for loop. The for loop also allows you to scope the iterating variable to the loop if desired and prevents you from forgetting to increment the counter at the end of the loop (as you can do with a while loop). \$\endgroup\$ Commented Mar 26, 2015 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.