#include <conio.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
#define max 20
int **TABLE,*FIRST_VALUES,*SUM_VALUES;
int **MIN_TABLE,**MAX_TABLE;
float *AVERAGE_VALUES;
void sort_Table(int *tab)
{
int i,j,temp;
for(i=1;i<max;i++)
for(j=max-1;j>=i;j--)
if(tab[j]<tab[j-1])
{
temp=tab[j-1];
tab[j-1]=tab[j];
tab[j]=temp;
}
}
int comp_Values(int value1,int value2)
{
return (value1<value2);
}
void move_Values(int *value1,int *value2)
{
int temp_value;
temp_value=*value1;
*value1=*value2;
*value2=temp_value;
}
void max_Sort(int *table)
{
int i,j;
for(i=0;i<max;i++)
{
for(j=0;j<max-1;j++)
{
if(comp_Values(table[j],table[j+1]))
move_Values(&table[j],&table[j+1]);
}
}
}
bool is_First(int value)
{
int i,sum;
sum=0;
for(i=1;i<value+1;i++)
if(value%i==0)
++sum;
if(sum==2)
return true;
else
return false;
}
int sum_Table(int *tab, int size)
{
int i,result;
result=0;
for(i=0;i<size;i++)
result+=tab[i];
return result;
}
float average_Table(int *tab, int size)
{
float result;
result=(1.0*sum_Table(tab,size))/(1.0*size);
return result;
}
int main()
{
srand(time(NULL));
int i,j,k;
TABLE=new int *[max];
MIN_TABLE=new int *[max];
MAX_TABLE=new int *[max];
FIRST_VALUES=new int[max];
SUM_VALUES=new int[max];
AVERAGE_VALUES=new float [max];
for(i=0;i<max;i++)
{
MIN_TABLE[i]=new int[max];
MAX_TABLE[i]=new int[max];
TABLE[i]=new int [max];
}
i=0;
j=2;
do
{
if(is_First(j))
{
FIRST_VALUES[i]=j;
SUM_VALUES[i]=0;
++i;
}
++j;
}while(i<max);
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
k=FIRST_VALUES[rand()%20];
TABLE[i][j]=k;
MIN_TABLE[i][j]=k;
MAX_TABLE[i][j]=k;
SUM_VALUES[i]+=TABLE[i][j];
}
}
for(i=0;i<max;i++)
AVERAGE_VALUES[i]=(1.0*SUM_VALUES[i])/(1.0*max);
for(i=0;i<max;i++)
{
max_Sort(MAX_TABLE[i]);
sort_Table(MIN_TABLE[i]);
}
cout<<"Original table: \n";
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
cout<<TABLE[i][j]<<" ";
}
cout<<"Sum = "<<SUM_VALUES[i];
cout<<" Average ="<<AVERAGE_VALUES[i];
cout<<"\n";
}
cout<<"\n\n Sorted in descending order: \n";
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
cout<<MIN_TABLE[i][j]<<" ";
}
cout<<"Sum = "<<SUM_VALUES[i];
cout<<" Average ="<<AVERAGE_VALUES[i];
cout<<"\n";
}
cout<<"\n\n Sorted ascending: \n";
for(i=0;i<max;i++)
{
for(j=0;j<max;j++)
{
cout<<MAX_TABLE[i][j]<<" ";
}
cout<<"Sum = "<<SUM_VALUES[i];
cout<<" Average ="<<AVERAGE_VALUES[i];
cout<<"\n";
}
for(i=0;i<max;i++)
{
delete [] TABLE[i];
delete [] MIN_TABLE[i];
delete [] MAX_TABLE[i];
}
delete [] MAX_TABLE;
delete [] MIN_TABLE;
delete [] TABLE;
delete AVERAGE_VALUES;
delete SUM_VALUES;
delete FIRST_VALUES;
getch();
return 0;
}