the program should randomly divide the numbers [110001-111999] into several groups and the numbers should be arranged from an ascending order [I finished this part already]
so this is what I did:
#include <cstdlib> //needed for srand and rand functions
#include <iostream> //needed for cout and cin
#include <algorithm> //needed for the shuffle function
#include <ctime> // needed since we used time function
using namespace std;
//function prototypes
void OnlyOne(int B);
void MoreThanOne(int Array[], int numElements);
void bubble_sort(int Array[],int N);
int main(int argc, char *argv[]) //int/main function
{
//declaration of variables
char repeat;
int batch;
int RandNum[1999] = {0}; //initializing values in the array to 0
//start of int function
do
{
int batch;
cout << "Hello! Do you wish to divide the ID #s into 1";
cout << " (press 1) or many (press 2) batches?: "; //I did this just to have the
//batch input/cin in the void function (MoreThanOne(RandNum, batch))
cin >> batch;
if(!cin.fail())//if the cin value is appropriate as what is declared, which is int.
if(batch == 1)
{
cout << "Batch 1 students are: " << endl;
OnlyOne(batch); //void
}
else if(batch == 2)
{
MoreThanOne(RandNum, batch); //void
}
else
{
cout << "Invalid!" << endl; //reaction
cin.clear(); //clears the cin
cin.sync(); //ignores the stream
}
cout << "Try Again? (Y): ";
cin >> repeat;
} while (toupper(repeat) == 'Y'); //looping the program to start again if user wants to
system("PAUSE");
return EXIT_SUCCESS; //end of int/main function
}
void OnlyOne(int B) //simply displays ID numbers from 110001 to 111999
{
int x = 1;
cout << "The only batch which contains 1999 elements: " << endl;
for (x = 1; x < 2000; x += 1)
{
cout << x+110000 << endl;
}
}
void MoreThanOne(int Array[], int numElements) //function to be use when division of ID NUMBERS
//is necessary
{
//array setting {ordered}
for(int x = 0; x < 1999; x += 1)
{
Array[x] = x+110001;
}
//Shuffling Array[x]
srand(static_cast<int>(time(0))); //shuffle using time
for (int i=0; i<1999; i++)
{
int r = i + (rand() % (1999-i)); // Random remaining position.
int temp = Array[i]; Array[i] = Array[r]; Array[r] = temp;
}//end of shuffling the Array[x]
int batch;
cout << "Hello user! Please enter the number of batches you want to have (2 to 1999): ";
cout << endl;
cin >> batch;
//start of batching , initial step
int N = 1999 % batch; //number of repetitions the...
int L = 1999/batch + 1; //this
int K = (1999-L*N)/(batch-N); //number of id left
int Z = batch - N; //number of batches holding the number of id left
for (int n = 0; n < N; n++) //repetition of batches
{
//pointers
cout << "Batch " << n + 1 << " with " << L << " elements:" << endl;
int size = L;
int start = n*L;
int end = size +n*L;
int *batchArray = new int[size];
for (int i = 0; i < size; i++)
{
batchArray[i] = Array[i + start];
}
bubble_sort(batchArray,L);
for (int i=0; i < size; i++)
{
cout << batchArray[i] << endl;
}
delete[] batchArray;
}
//additional batches
for (int j = 0; j < batch - N; j++) // repetition of batch
{
cout << "Batch " << j+ N +1 << " with " << K << " elements:" << endl;
int size = K;
int startb = j*K+N*L;
int endb = size + j*K +N*L;
int *batchArray = new int [size];
for ( int i = 0; i < size; i++)
{
batchArray [i] = Array[i + startb];
}
bubble_sort(batchArray, K);
for (int i=0; i < size; i++)
{
cout << batchArray[i] << endl;
}
delete[] batchArray;
}
}
void bubble_sort(int Array[],int N)
{
int sub=0;
int temp = 0;
int maxSub= N;
char swap='Y';
int lastSwap=0;
while(swap=='Y')
{
swap='N';
sub=0;
while (sub < N-1)
{
if(Array[sub]>Array[sub+1])
{
temp=Array[sub];
Array[sub]=Array[sub+1];
Array[sub+1]=temp;
swap='Y';
lastSwap=sub;
}
sub+=1;
}
}
}
so my problems are...
*I am having problems in indicating the number of students per group
*I am also having problems in two dimensional arrays [It's kinda new to me]
since in every group... the numbers or elements should be numbered randomly too -depending on the number of students in the group [as seen in my example above]. well... *I'm having problems in doing this too.