How to generate a random array of integers subject to a certain constraint


Posted on 16th Feb 2014 07:03 pm by admin

I am writing a program that asks the user to enter an integer N and then generates a random array of integers of size 2^N, each of whose entries range from 1 to N+1. The final random array, however, must satisfy the following properties: the integer 1 can be stored in 1 slot, the integer 2 can only be stored in 1 slot, the integer 3 can only be stored in 2 slots, the integer 4 can only be stored in 4 slots... until all 2^N slots are filled with the integers 1, 2, ...,N+1.

I have written some code that asks the user to enter an integer N and then
generates a random array of integers of size 2^N but I cannot generate a random array subject to the constraints that I mentioned above. I have written a function, CheckArray, that returns the value of how many times a certain integer
appears in the array but I don't know how to use it to help me get my desired random array.

Below is the code I have written so far. Note that the function CheckArray is not called in the program since I don't know how to use it yet.

#include
#include
#include
#include
#include
using namespace std;

int RandomNumber(int m);
int exponentiation(int n);
int CheckArray(int IntegerArray[], int sizeOfArray, int CType);

int main()
{ int N;
int NUMOFCELLS;

cout<< "Enter the maximum number of cell divisions you want: ";
cin>>N;
NUMOFCELLS = exponentiation(N);
cout<<"The maximum number of cells will be: ";

cout< cout<<"n";
cout<<"n";

int sample[128]; // this reserves 128 integer elements
int t;

srand(time(0));
getchar();

for(t = 0; t <= NUMOFCELLS; t++)
{ sample[t] = RandomNumber(N); }

// display the array

for(t = 0; t < NUMOFCELLS; ++t)
{ cout << sample[t] << ' ' ; }

cout<<"n";

getchar();
return 0;
}

int RandomNumber(int m)
{ // generates a random number in the specified range from 0 to (m-1)
int iSecret;
iSecret = rand() % m + 1;
return(iSecret);
}

int exponentiation(int n)
{ int t;
int numberofcells;
numberofcells = 2;
for(t=1; t < n; ++t)
{numberofcells = 2*numberofcells;}

return(numberofcells);
}

int CheckArray(int IntegerArray[], int sizeOfArray, int CType)
{
int accumulator = 0;
int temp;

for(int i = 0; i < sizeOfArray; i++)
{ temp = IntegerArray[i];
if(temp == CType)
{accumulator++;}

}

return accumulator;
}

No comments posted yet

Your Answer:

Login to answer
349 Like 46 Dislike
Previous forums Next forums
Other forums

Memory Allocation
Hi folks,
see this ex:

int v=5;
v=v+10;

then what will do the compiler before th

my login script page is not working on remote computers
hi my login is working on my computer, but when i tried 2 computers from 2 different locations, they

Sort a two dimensional array.
Hi. I've set up a two dimensional array that reads as follows:

Code: $modifiedData = array(

how do i make new line after *
First check this page here. and you see my report. Im pulling form a mysql db. I want to beable to m

gettext translates ALWAYS?
I'm using gettext with the new version of XAMPP (5.3.0) on Windows Seven RTM (build 7600)
gettext

Preventing blank or already entered values
Im trying to learn how to place a read file line by line and prevent duplicate entries and also BLAN

Multiple submit forms inside a mysql array page
Hi all,

Im having trouble to get the beneath script to work the thing i can't seem to get wor

background color imagefill
Hello

I would like to ask you why I see this square in red color just in my local xampp insta

limit string by words??
hello all,

I was wonder if there is a function to limit a string by the number of words inste

RTF fomatting to email content
Im trying to sen an email with content is picked up from a rtf-file (file_get_contents('*.rtf'). Mai

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash