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";
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;
}
Need help making a blockquote and line items conditional
I have some code I bought a few years ago that allows my clients to update content on their site using an Excel spreadsheet. Well this particular application sometimes has line items so I need to come
PHP MySQL and DATE
Hi everyoneI have a databse and in one of the columns I have date values such as 2009-March-27. If i have a php interface and want to use a query to select all the rows that is in say october. how
Auto install
Hi I have a directory lets say "apps" that I then have more folders ie "email", "projectmanagment" now each of these "apps" need a mysql table, each of these
Saving data from a form into a file
Hey everybody,Sorry, I am really new to PHP coding and such but a project kind of got thrust on me. I need to create a a form for people to input some information and then be able to collect the
need help to creat database
Hello Team, please guys i am stuck from three days with paypal issue for IPN but no luck yet now i wants to do other method. i have used this form to send info to paypal and everything is working ok
Convert array to string and write to csv file
I am looking to write a MySQL query to a csv file.At the minute I:check for the original file, and delete it;create a new (blank) file;run the MySQL query and pass it into an array;write it to the new
PHP Mysql Staff Induction System
Hi there, I'm pretty new to PHP and Mysql so could really do with being pointed in the right direction with this problem I have.I am trying to set up a system induct new members of staff onto their
Batch update record with Pagination
Hoping someone can help me with this issue I'm having, im trying to batch update records from a result set with pagination. The first page results (first 10 records) will update with the batch, but
Posting Serialized Data Not Working
I am trying to send an object from one PHP file to another using POST. I serialize the object and then add slashes. For some reason, only part of the data is getting through. It appears to be a
modifying a property of an object from a different class
I have 2 classes:- Math- Distancein my math class, I have this line:$distance = new Distance($this->query, $this->distances);and this property:public $special = array();in my Distance