-2

I was trying to print out some kind of graph of an array's integers making some "bars" of the same length so that i could later print them vertically and not horizontally. At first my code printed the graph upside down so i "inverted" the for cycle. I run the function twice and it now only works the first time or doesn't work at all or it prints the array partially the second time (everytime i run the code the elements in the arrays vary). Any ideas? I'm genuinely desperate.

before

void printArrInt(int arr[], int len) {
  int max = findMaxValue(arr, len);
  std::string bars[len];
  for (int i = 0; i < len; i++) {
    for (int j = 0; j < arr[i]; j++) {
      bars[i].append("@");
    }
    for (int j = 0; j < (max - arr[i]); j++) {
      bars[i].append(" ");
    }
  }
  for (int i = 0; i < max; i++) {
    for (int j = 0; j < len; j++) {
      std::cout << bars[j][i];
    }
    std::cout << std::endl;
  }
  std::cout << std::endl << std::endl;
}

after

void printArrInt(int arr[], int len) {
  int max = findMaxValue(arr, len);
  std::string bars[len];
  for (int i = 0; i < len; i++) {
    for (int j = 0; j < arr[i]; j++) {
      bars[i].append("@");
    }
    for (int j = 0; j < (max - arr[i]); j++) {
      bars[i].append(" ");
    }
  }
  for (int i = max; i > 0; i--) {
    for (int j = 0; j < len; j++) {
      std::cout << bars[j][i];
    }
    std::cout << std::endl;
  }
  std::cout << std::endl << std::endl;
}
Daniele
  • 3
  • 1
  • 2
    "it now only works the first time or doesn't work at all" — sounds like undefined behavior, e.g. array-out-of bounds in some random spot in the program (not necessarily `printArrInt`). Enable Address Sanitizer or run the code under Valgrind to see some potentially problems (not guaranteed). As for StackOverflow, please provide [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) in full that others can copy-paste in run, not just an excerpt. – yeputons Jul 05 '23 at 20:16
  • 2
    Sounds like you'd be better off stepping through the function with [a debugger](https://en.wikipedia.org/wiki/Debugger) so you can see exactly what's going wrong as it happens than asking a Stackoverflow question. [Here's some documentation on using the Visual Studio debugger](https://learn.microsoft.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2022) and most GUI debuggers work the same. – user4581301 Jul 05 '23 at 20:17
  • Recommendation: If you are going to continue with this question at this time, consider providing a [mre] that we out here and drop into our tools, build and run without having to make changes so we can see what you're seeing. Often just making the example reduces the noise around the mistake enough to spot and fix it without further help. – user4581301 Jul 05 '23 at 20:20
  • 2
    `int i = max; i > 0;` is **not** the "reverse" of `int i = 0; i < max;`. You are ["off by one"](https://stackoverflow.com/questions/2939869/what-is-an-off-by-one-error-and-how-do-i-fix-it) with both the starting value and the end value. – Drew Dormann Jul 05 '23 at 20:20
  • 1
    `std::string bars[len];` -- This is not valid C++. Arrays in C++ must have their size denoted by a compile-time constant, not a runtime variable. -- *I'm genuinely desperate.* -- Well, whoever taught you to use that array syntax certainly didn't care if you were learning C++ properly. – PaulMcKenzie Jul 05 '23 at 20:30
  • `std::string bars[len];` --> `std::vector bars(len);` – PaulMcKenzie Jul 05 '23 at 20:34
  • In the second version of the loop use `i - 1` as the array index instead of `i`. – Pete Becker Jul 06 '23 at 11:57

1 Answers1

0

In your first implementation, it stops just before i reaches the value max (i<max). In your second, it starts at max instead of max-1 as it should. So you may be getting an out-of-bounds error. Also in your second implementation, it will not run with i==0 but will stop at i==1 because i>0 will be evaluated after i-- and when i is 1, i-- is zero and fails the test.

Vercingatorix
  • 1,838
  • 1
  • 13
  • 22