Jump to content

Error while dumping state

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I'm currently working on this question:

Quote

Two dice are tossed 1000 times. Record the sum of the faces in an array. Print out the results and determine the probability of getting a sum of 7. 11?

and when I run my executable I get a message like in the attached picture.

I'm not really sure why. I can compile and run C++ programs in NetBeans with no problems!

My code:


/* 

 * File:   2a.cpp

 * Author: James

 *

 * Created on January 27, 2009, 3:33 PM

 */


#include <stdlib.h>

#include <iostream>

#include <cmath>

#include <time.h>


using namespace std;


/*

 * 

 */

int main(int argc, char** argv) {

    int arnSum[1000]; // sum of the faces

    int arnProb[36];

    int nDie1, nDie2;


    for (int i=0;i<1000;i++) {

        nDie1 = rand()*6+1;

        nDie2 = rand()*6+1;

        arnSum[i] = nDie1 + nDie2;

        arnProb[nDie1+nDie2]++;

    }


    return (EXIT_SUCCESS);

}



#2
Babbage

Babbage

    Newbie

  • Members
  • PipPip
  • 11 posts
It looks like you are multiplying rand() with 6, when you should be using a modulus operator:

        nDie1 = rand()%6+1;
        nDie2 = rand()%6+1;

Try that and see if it fixes your problem.

Reference: rand - C++ Reference

-Babbage

#3
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Yep that fixed my problem.

See what my assumption was that rand() gave me a value between 0 and 1 like in every other language I use.

Thanks a lot! :D