[RESOLVED] What's wrong?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: [RESOLVED] What's wrong?

  1. #1
    Join Date
    Jul 2011
    Posts
    35

    [RESOLVED] What's wrong?

    Hi,
    I'm coding a program which indicates how many times a number ranging from 0 to max is casted (max is an integer chosen by the user). I'm using the same random number generator as in "C++ without fear". However, when compiled, the program outputs right and wrong results . What's wrong?

    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <stdlib.h>
    using namespace std;
    
    int rand_0toN1(int n);
    
    int main() {
        for (;;) {
            int n, i, r, max;
    
            srand(time(NULL));
    
            cout << "The highest number to be generated: "; cin >> max;
            int hits[max];
            cout << "Enter how many trials: "; cin >> n;
            for (i = 0; i < n; i++) {
                r = rand_0toN1(max);
                hits[r]++;
            }
            for (i = 0; i < max; i++) {
                cout << i << ": " << hits[i] << " Accuracy: ";
                double results = hits[i];
                cout << results / (n / max) << endl;
            }
        }
        return 0;
    }
    
    int rand_0toN1(int n) {
        return rand() % n;
    }

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,625

    Re: What's wrong?

    You might want to try initializing all elements of the "hits" array to 0.
    I don't know if your declaration does that, since it is non-standard.

  3. #3
    Join Date
    Jul 2011
    Posts
    35

    Thank

    Is that what you meant?

    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <stdlib.h>
    using namespace std;
    
    int rand_0toN1(int n);
    int hits[0];
    
    int main() {
        for (;;) {
            int n, i, r, max;
    
            srand(time(NULL));
    
            cout << "The highest number to be generated: "; cin >> max;
            cout << "Enter how many trials: "; cin >> n;
            for (i = 0; i < n; i++) {
                r = rand_0toN1(max);
                hits[r]++;
            }
            for (i = 0; i < max; i++) {
                cout << i << ": " << hits[i] << " Accuracy: ";
                double results = hits[i];
                cout << results / (n / max) << endl;
            }
        }
        return 0;
    }
    
    int rand_0toN1(int n) {
        return rand() % n;
    }
    You're right. I needed to initialize the array from outside the main function. Thanks

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,127

    Re: [RESOLVED] What's wrong?

    No, the problem is you're calling srand inside your loop. You should only call it once.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,625

    Re: [RESOLVED] What's wrong?

    this is not correct ...
    Code:
    int hits[0];
    that is declaring the array with size 0 (I'm not sure if that is legal or not).

    In your "for(;;)" loop, you need to initialize the array to 0 ...

    Code:
    int hits[max];
    
    for (i=0; i<max; ++i)
       hits[i] = 0;

    As mentioned, you only need to call srand() once at the start of the program.
    But your usage is OK.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,014

    Re: [RESOLVED] What's wrong?

    Quote Originally Posted by Philip Nicoletti View Post
    that is declaring the array with size 0 (I'm not sure if that is legal or not).
    It's not legal to specify a static array (or dynamic array) of size 0. This is actually used as a way to implement compile-time assertions.

    Note however, that the OP's original program uses neither; it uses a variable length array, which is a language extension. IMO, it's better to just use a std::vector instead. In that case the initialization is also very simple.
    Code:
    std::vector<int> hits(max, 0);
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Thank

    Quote Originally Posted by CodingFreak View Post
    Is that what you meant?
    No, this:
    Code:
    cout << "The highest number to be generated: "; cin >> max;
            int hits[max];
    It is illegal in C++ to declare an array using a variable as the number of items in the array.
    Code:
    int main()
    {
        int x = 10;
        int hits[x];  // error
    }
    This code should not compile if compiled using ANSI C++. More than likely, you're using gcc or an IDE that uses the gcc compiler underneath. If so, you must change the compiler settings:
    -Wall -pedantic
    Once you do that, then you will see that your code will not compile. If this is a C++ course or homework, then you must change it to compile ANSI C++ and not use language extensions, otherwise you may get points taken off for using non-C++ code.

    Once you see that your code doesn't compile, the real alternative is to use std::vector<> as a dynamic array of hits.
    Code:
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <stdlib.h>
    #include <vector>
    
    using namespace std;
    
    int rand_0toN1(int n);
    
    int main() {
        for (;;) {
            int n, i, r, max;
    
            srand(time(NULL));
    
            cout << "The highest number to be generated: "; cin >> max;
            std::vector<int> hits(max, 0);
            cout << "Enter how many trials: "; cin >> n;
    //...
    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center