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
Did you know?Explore Trending and Topic pages for more stories like this.
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
46
Other forums
Class not found error
I am getting Class 'index' not found in Eval function:
//write config
$pat
OOP help
okay so i have a class im making it has everything setup i just need to randomize the 2 variables an
cURL proxy
Okay here is the thing... i know how to add proxys
Code: curl_setopt($ch, CURLOPT_PROXYTYPE,
Allowing ' and "
Hello everyone,
I am creating a form where users submit information to go into a database. I
Why doesn't this work? (SSH2)
This is my script:
Code: <?php
$connection = ssh2_connect('213.251.167.109', 22);
1,000 select boxes with 100 options?!
I have 1 drop down select box with 1,000 options.
In some case, there will be 100+ of the
Regarding accessing SQL query issued by any user in Oracle 10g
Hi all,
i want to know the queries issued by various users accessing a database...
very easy question about SQL info
Hello,
I think that I have a very easy question.
I know how to create a form using php SQL and
Comma seperated implode & modifying returned string.
I have a PHP post form that posts up to 4 variables. I need whatever variables are passed to be comb
Multiple PHP errors when clicking a link..and..removing a ghost..
Hi - I have 2 problems with a new website - homeswapvacations.com, which I need help solving.