Help with number generation
Here is my code. I need to generate three random numbers and produce the binary equivilent for each. I have got so far with it but now it produces a load of garbage and im struggling to see why.
Code:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#define Population 10
#define Chrom_Length 4
#define Dimension 3
void main(){
int Numbers [Population][Dimension];
int Binary [Population][Chrom_Length*Dimension];
int Position;
int Random;
int Decimal;
int MAX = pow(2,Chrom_Length)-1;
int MostSigBit = pow(2,(Chrom_Length-1));
int CurrSigBit;
srand ( time(NULL)); // Seeds the random number generator
for(int i=0; i<Population; i++)
{
for(int j=0; j<Dimension;j++)
{
Random = (rand()%MAX); // Generate the random number
Numbers[i][j] = Random; // Store the number
}
}
for(int i=0; i<Population; i++) // Population for the loop
{
for(int i=0; i<Population; i++)
{
Position =0;
for (int d=0; d< Dimension; d++) // Populates the dimension
Decimal = Numbers[i][d];
for (int j=0; j<Chrom_Length; j++) // Looks at each part of the chromosome in the loop
{
CurrSigBit = pow(2,(Chrom_Length-j-1));
if (CurrSigBit <= Decimal)
{
Binary[i][j]=1;
Decimal = Decimal - CurrSigBit; // Takes the CurrSigBit value away from the current decimal value
}
else
{
Binary[i][j]=0; // If CurrSigBit is greater than the current decimal number store a zero in the binary array
}
Position++;
}
}
}
for (int i=0; i<Population; i++)
{
for (int d=0; d< Dimension; d++)
{
cout<< Numbers[i][d];
cout<< " " ;
for(int j=Position; j< (Chrom_Length+Position); j++)
{
cout<<Binary[i][j];
}
Position = Position + Chrom_Length;
}
cout<<endl;
}
}