0

If anyone's had experience with this and can tell me where I'm going massively wrong that would be greatly appretiated.

So far I am doing the "Advanced C++ Programming" course on Udemy by John Purcel and I'm currently doing the fractal project close to the end of the entire course.

My main code that I have so far looks like:

#include <iostream>
#include <stdint.h>
#include <memory>
#include <math.h>
#include "Mandelbrot.h"
#include "Bitmap.h"

using namespace std;
using namespace graphics;

int main() {

    int const WIDTH = 800;
    int const HEIGHT = 600;

    Bitmap bitmap(WIDTH, HEIGHT);

    double min = 999999;
    double max = -999999;

    unique_ptr<int[]> histogram(new int[Mandelbrot::MAX_ITERATIONS] { }); //If you want to validate this, make a for loop that outputs the number of iterations of the histogram.
    unique_ptr<int[]> fractal(new int[WIDTH * HEIGHT] { });

    for (int yaxis = 0; yaxis < HEIGHT; yaxis++) {
        for (int xaxis = 0; xaxis < WIDTH; xaxis++) {
            double xFractal { (xaxis - WIDTH / 2 - 200) * 2.0 / WIDTH };
            double yFractal { (yaxis - HEIGHT / 2) * 2.0 / HEIGHT };

            int iterations = Mandelbrot::getIterations(xFractal, yFractal);

            fractal[yaxis * WIDTH + xaxis] = iterations;

            if (iterations == Mandelbrot::MAX_ITERATIONS) {
                histogram[iterations]++;
            }

        }
    }

    int total = 0;
    for (int i = 0; i < Mandelbrot::MAX_ITERATIONS; i++) {
        total += histogram[i];
    }

    for (int yaxis = 0; yaxis < HEIGHT; yaxis++) {
        for (int xaxis = 0; xaxis < WIDTH; xaxis++) {

            uint8_t red = 0;
            uint8_t green = 0;
            uint8_t blue = 0;

            int iterations = fractal[yaxis * WIDTH * xaxis];

            if (iterations != Mandelbrot::MAX_ITERATIONS) {
                double hue = 0.0;

                for (int i = 0; i <= iterations; i++) {
                    hue += ((double) histogram[i]) / total;
                }

                green = pow(255, hue);

            }

            bitmap.setPixel(xaxis, yaxis, red, green, blue);

        }
    }

    bitmap.write("test.bmp");

    cout << "File has been Built." << endl;
    return 0;
}

What should happen after building and running this is that the cout statement should be printed in the terminal and I should get the "test.bmp" file appearing in the project file. But every time that I'm running this, the program terminates. For no apparent reason. All the other header files and cpp files have been left alone for quite some time since I was able to follow the course and get them working 100% of the time but suddenly it doesn't want to.

When building I get 2 warnings for unused variables but that shouldn't be of worry. The MAX_ITERATIONS number can be anything. I've tried 1000, 2000, 4000 and even 100. But after all the iterations have been complete the terminal...well...terminates and then nothing is output.

I am using the Eclipse IDE, GNU Compiler and I do have the flag set so I'm able to use C++11 code.

Hopefully this is enough information.

Hope to hear back soon from any of you.

Ghost
  • 1
  • Try adding a breakpoint on the `cout` line and see if it hits that? – silentPlanet Jul 22 '20 at 10:28
  • _"the terminal...well...terminates"_ - you mean the terminal closes and you can't see the output? That's what a terminal usually does when the program it was running has finished. – Lukas-T Jul 22 '20 at 10:29
  • Please provide debugging details! Add `std::cout`s in your code to find out where exactly it goes wrong. Also, [Why is "using namespace std" bad?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Nikita Demodov Jul 22 '20 at 10:30
  • You loop over `yaxis` like so, `for (int yaxis = 0; yaxis < HEIGHT; yaxis++) {`, but when trying to calculate index in array, you do it, like so: `fractal[yaxis * WIDTH + xaxis] = iterations;` Notice a problem? – Algirdas Preidžius Jul 22 '20 at 10:36
  • Do you know what is problem with Udemy? They do not care about quality of they courses, anyone can publish there and claim to be pro. I've take a pick on [code associated with this course](https://github.com/caveofprogramming/advanced-cplusplus) and I would not trust this author (only by quickly reviewing his code). – Marek R Jul 22 '20 at 10:36
  • 1
    You should also take a closer look at `int iterations = fractal[yaxis * WIDTH * xaxis];` seems like a typo there. – Lukas-T Jul 22 '20 at 10:43
  • it supose to be `fractal[yaxis * WIDTH + xaxis];` Note `+` instead `*` – Marek R Jul 22 '20 at 10:46
  • anyway learn how to use debugger ASAP. Beginners should use it constantly. The more experience programmer is then less needs this tool. If you do not know it then "advanced" courses is not for you. – Marek R Jul 22 '20 at 10:49
  • 3
    A quick glance at the shown code reveals multiple array overruns, memory corruption, and undefined behavior -- and not just the obvious typo in `fractal` -- due to unsafe programming practices. This is why the only practical way to learn C++ is with the help of a quality textbook that teaches safe programming practices, instead of some web site where any clown can post any random musings on C++ that enters their head. Anyone can put together a web site that says anything. It takes real money to edit a real book, and a publisher will not risk their money unless they are certain of its quality. – Sam Varshavchik Jul 22 '20 at 10:49
  • Thanks guys, Turns out that it was just down to a few typo errors in the code. Thanks to Algirdas and churill who pointed that out to me. And in regards to the udemy course, It's not originally from there. The course is from caveofprogramming if that makes any difference. Edit 1: Also thanks to Marek R for showing the problem that was in my code and then correcting it – Ghost Jul 22 '20 at 10:50
  • 1
    @Ghost Using `std::vector` instead of `unique_ptr`, and thus able to use `std::vector::at()` would have pinpointed the error easily, and probably no need for a question to be posted (I'm assuming you would have picked up on the cause of the error yourself). I agree with SamV, BTW. – PaulMcKenzie Jul 22 '20 at 10:54

0 Answers0