dusktreader 137 Posting Whiz in Training

I know the solution to the random reordering has already been handled, but here is a solution that I find more mathematically satisfying.

1. Assign each die a random x, y coordinate
a. The coordinates should be real numbers (i.e. doubles )
b. If you prefer integers, then the range of possible values for each dimension should be significantly larger than the corresponding grid dimension and a multiple of the grid dimension.
For a 6x16 grid, use x=rand()%(6*6), y=rand()%(16*16)
2. Sort the dice by their y coordinate *in place*
3. Partition the list into h equally sized groups where h = height of dice grid
For a 6x16 grid, obviously partition into 16 groups of 6 coordinate pairs
4. Sort the dice in each partition *in place*
5. The list should represent a random distribution representing random re-ordering over a fixed size grid
So, for the die in group 4 at slot 2, it should go to Grid location row 4, column2

It may not be more efficient (haven't done the analysis), and it might be more troublesome to implement, but it is an interesting solution, no?

dusktreader 137 Posting Whiz in Training

You have some value index confusion happening here I think

Try

int temp;
int idx0 = rand() % 16;
int idx1 = idx0;
while( idx1 == idx0 )
    idx1 = rand() %16;
temp = Dice[idx0];
Dice[idx0] = Dice[idx1];
Dice[idx1] = temp;
dusktreader 137 Posting Whiz in Training

You can also deduce the indices of a 2D array from a 1D iterator using modulus math:

/* Performs both integer division and modulo with improved efficiency */
void divmod( int dividend, int divisor, int &quotient, int &remainder ){
    quotient = dividend / divisor;
    remainder = dividend - divisor * quotient;
}

int find2D(int A[][], int x, int w, int h, int &i, int&j ){
    for( int idx=0; idx<n; idx++ ){
        divmod( idx, w, i, j );
        if( A[i][j] == x )
            return 1;
    }
    return -1;
}

If the function returns 1, a match was found at A[j]

dusktreader 137 Posting Whiz in Training

I am having some trouble getting some overloaded operators working correctly with a simple inherited Vector class (numerical vector, not container) The scalar multiply from the base class doesn't appear to be visible to the derived class. Code follows:

Base Class Header

#pragma once

#include <vector>

class Base
{
protected:
    std::vector<double> val;
public:
    Base();
    Base( double v0, double v1, double v2 );
    virtual ~Base(){}
    virtual Base operator*( const double scalar ) const;
};

Base Class Implementation

#include "base.h"

using namespace std;

Base::Base(){
    val = vector<double>( 3, 0.0 );
}

Base::Base( double v0, double v1, double v2 ){
    val = vector<double>( 3, 0.0 );
    val[0] = v0;
    val[1] = v1;
    val[2] = v2;
}


Base Base::operator*( const double scalar ) const{
    return Base( val[0] * scalar, val[1] * scalar, val[2] * scalar );
}

Derived Class Header

#pragma once

#include "base.h"

class Derived : public Base{
public:
    Derived();
    Derived( double a, double b, double c );
    Derived( const Base &other );
    virtual ~Derived(){}
    double a() const;
    double b() const;
    double c() const;

    double operator*( const Derived& other ) const;
};

Derived Class Implementation

#include "derived.h"

Derived::Derived() : Base(){}

Derived::Derived( double a, double b, double c ) : Base(a,b,c){}

Derived::Derived( const Base &other ) : Base(other){}

double Derived::a() const{
    return val[0];
}

double Derived::b() const{
    return val[1];
}

double Derived::c() const{
    return val[2];
}

double Derived::operator*( const Derived& other ) const{
    return val[0] * other.val[0] + val[1] * other.val[1] + val[2] * other.val[2];
}

Main Program


       
dusktreader 137 Posting Whiz in Training

Have a look at Qt. It is an excellent GUI development kit for c++, and the basics are really easy to pick up. Qt's developers also offer a fully integrated IDE for both developing the code and designing the gui. It is called Qt Creator. There is extensive documentation available as well as tons of helpful tutorials. Additionally, Qt is completely portable across most platforms. For example, I develop applications on a Linux workstation, but I can build and deploy them on Windows as well. It is definitely worth a look: http://qt.nokia.com/products/developer-tools