sobota, 9 lipca 2016

Random and array with new/delete

#include <iostream>

#include <stdlib.h>
#include <conio.h>
using namespace std;


void init_random()
{
srand(time(NULL));
}

void bubble_sort(int *array, int n)
{
int i,j,k;
for(i=1;i<n;i++)
for(j=n-1;j>=i;j--)
 if(array[j]<array[j-1])
 {
 k=array[j-1];
 array[j-1]=array[j];
 array[j]=k;
 }
}


int main(int argc, char** argv)
{
int whole,how_many_numbers,how_many_reps;
int *my_numbers_drawn,*temp_whole;
int **all_numbers,**temp_all_numbers;
init_random();
cout<<"Whole numbers lottery? - ";cin>>whole;
cout<<"How many numbers will random? ";cin>>how_many_numbers;
cout<<"How many reps? ";cin>>how_many_reps;
int i,j,k;
my_numbers_drawn=new int[how_many_numbers];

temp_whole=new int[whole];


all_numbers=new int *[how_many_numbers];
for(i=0;i<how_many_numbers;i++)
all_numbers[i]=new int[how_many_reps];
temp_all_numbers=new int *[how_many_reps];
for(i=0;i<how_many_reps;i++)
 temp_all_numbers[i]=new int[whole];
for(i=0;i<whole;i++)
temp_whole[i]=0;
for(i=0;i<how_many_reps;i++)
for(j=0;j<whole;j++)
 temp_all_numbers[i][j]=0;
i=0;
do{
 do
 {
      j=rand()%whole;
 }while(temp_whole[j]!=0);
 temp_whole[j]=1;
 my_numbers_drawn[i]=j+1;
 ++i;
}while(i<how_many_numbers);
i=0;
do
{
j=0;
do
{
 do
 {
  k=rand()%whole;
  }while(temp_all_numbers[i][k]!=0);
 temp_all_numbers[i][k]=1;
 all_numbers[i][j]=k+1;
 ++j;
      }while(j<how_many_numbers);
     ++i;
    }while(i<how_many_reps);

bubble_sort(my_numbers_drawn,how_many_numbers);
cout<<"My bet: ";
for(i=0;i<how_many_numbers;i++)
{
cout<<my_numbers_drawn[i]<<"  ";
}
        for(i=0;i<how_many_reps;i++)
        {
         bubble_sort(all_numbers[i],how_many_numbers);
        }
        cout<<"\nDraw bet:";
        cout<<"\n\n";
        
        for(i=0;i<how_many_reps;i++)
        {
        
         for(j=0;j<how_many_numbers;j++)
         {
          cout<<all_numbers[i][j]<<"   ";
         }
         cout<<"\n";
         
        }
for(i=0;i<how_many_numbers;i++)
delete [] temp_all_numbers[i];
delete [] *temp_all_numbers;
for(i=0;i<how_many_reps;i++)
delete [] all_numbers[i];
delete [] *all_numbers;
delete temp_whole;
delete my_numbers_drawn;
        getch();
return 0;
}

Brak komentarzy:

Prześlij komentarz