0

When running the code below I get the following errors. Can you please explain to me why and what I should do? I picked up the code relative to the library online but it won't work. I'm using DevC++ 5.11.

  1. default_random_engine is not a member of 'std'
  2. uniform_real_distribution is not a member of 'std'
  3. 'generator' was not declared in this scope
  4. 'distribution' was not declared in this scope

Also, how do I count time in C++?

    #include <iostream>
    #include <random> 

    int main() {        

    int i, j, n, nmax;   
    std::default_random_engine generator;   
    std::uniform_real_distribution<double> distribution(0.0,1.0);                                   
    nmax = 3000;    

    for (n=200; n<nmax; i+=200) {       
       double x[n], y[n], A[n][n];      
       for (i=0; i<n; i++) {            
          x[i] = distribution(generator);           
          y[i] = 0;             
          for (j=0; j<n; j++) {
             A[i][j] = distribution(generator);             
          }         
       }        
       // start counting time 
       for (i=0; i<n; i++) {            
          for (j=0; j<n; j++) {
             y[i] = A[i,j] * x[j];          
          }         
       }        
       // stop counting time        
       // total_time = stop - start         
       // std::cout << total_time   
    } 
    return 0;    
    }
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
vprado
  • 3
  • 2
  • 2
    apart from the `A[i,j]` which should be `A[i][j]` there are no other compiler errors: cannot reproduce: https://godbolt.org/g/g1Rf9C – bolov Mar 28 '18 at 01:29
  • 1
    Make sure you compile with `-std=c++11`. Here's how to do it in DevC++: https://stackoverflow.com/questions/16951376/how-to-change-mode-from-c98-mode-in-dev-c-to-a-mode-that-supports-c0x-ran – Aziz Mar 28 '18 at 01:29
  • 2
    make sure you enable C++11 – bolov Mar 28 '18 at 01:29
  • "DevC++ 5.11" That's kind of irrelevant. That's the IDE. The compiler is what matters. – bolov Mar 28 '18 at 01:30
  • @bolov Well, VLAs apart: https://godbolt.org/g/sKEHko – Bob__ Mar 28 '18 at 14:24

1 Answers1

0

Bugs in code:

1. default_random_engine and uniform_real_distribution are not C++98 standards so all four errors are related with this. You should install C++11 compiler on your machine. Here if you play with standards in options section you will understand what I mean.

2. Change A[i,j] to A[i][j].

3. You should change i+=200 to n+=200 in the very first for loop.

4. You can create 2D vector this way:
vector<vector<int>> vec(row_num, vector<int>(col_num));

And as of calculation of execution time you could use <time.h>. Here is working code using std::vector:

#include <iostream>
#include <random> 
#include <time.h>
#include <vector>

int main()
{
    int nmax;
    std::default_random_engine generator;
    std::uniform_real_distribution<double> distribution(0.0,1.0);
    nmax = 3000;

    for (int n=200; n < nmax; n+=200) {
        std::vector<double> x(n);
        std::vector<double> y(n);
        std::vector< std::vector<double> > A(n, std::vector<double>(n));

        for (int i=0; i<n; i++) {
            x[i] = distribution(generator);
            y[i] = 0;

            for (int j=0; j<n; j++) {
                A[i][j] = distribution(generator);
            }
        }

        // start counting time
        clock_t start = clock();
        for (int i=0; i<n; i++) {
            for (int j=0; j<n; j++) {
                y[i] = A[i][j] * x[j];
            }
        }
        // stop counting time
        clock_t stop = clock();
        // total_time = stop - start
        double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
        std::cout << "Elapsed time: " << elapsed << std::endl;
    }
    return 0;
}
Eziz Durdyyev
  • 1,110
  • 2
  • 16
  • 34
  • Hey Eziz, many thanks for your tips! The code runs but after 2nd iteration I get a error msg: "Process exited after 3.299 seconds with return value 3221225725". Any clues? The compiler gets no errors and no warnings. – vprado Mar 29 '18 at 11:01
  • @vprado `double x[n], y[n], A[n][n];` are all variable length arrays, a non standard feature supported by some compilers (for C99 compatibility). They are allocated on the stack, and given that your last iteration would declare an 2800x2800 array of `int`s, it runs out of space. Use `std::vector` instead. – Bob__ Mar 29 '18 at 12:37
  • @vprado, you are welcome. Sorry, my bad. @Bob__ is right, `x[n]` is VLA, and it is not C++ standard. You can use `vector` or go with dynamic memory allocation. – Eziz Durdyyev Mar 29 '18 at 12:55
  • Ok Eziz, I'm still in trouble! I included the vector libray and declared x and y as vectors of doubles `vector`. But what about the A matrix? I tried declaring it as a vector of a vector of doubles `vector>`, but it won't work. – vprado Apr 01 '18 at 16:02
  • @vprado, I edited the code. I hope it will help this time. Cheers )) – Eziz Durdyyev Apr 02 '18 at 19:29
  • @EzizDurdyyev, thanks again! That's close to what I came up with after researching the net. I also tried the ublas matrix but the compiler couldn't find the library and wouldn't run. I still have to understand many things about C and C++. I ran the code w/o compiling optimizations for nmax = 40.000 to see if I'd observe the processing performance being impaired due to full cache memory (my processor has 3.5 Mb L2+L3 cache size and each double in C++ occupies 4 bytes) but it didn't happen... I still have to figure out why... Anyway, thanks again! I learned a lot with you. You're the best! – vprado Apr 03 '18 at 00:17