niedziela, 31 lipca 2016

double array with sorting and write to file

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#define size 10


using namespace std;


int **tab;
int **which_position;
int *table_with_first;
bool is_first(int x)
{
int i,sum;
sum=0;
for(i=1;i<x+1;i++)
if(x%i==0)
 ++sum;
if(sum==2)
return true;
else
return false;
}
void init_random()
{
srand(time(NULL));
}


int main(int argc, char** argv)
{
int i,j,k,sum;


init_random();
table_with_first=new int[size];
i=0;
j=1;
do
{

if(is_first(j))
{
table_with_first[i]=j;
++i;
}
++j;
 

}while(i<size);
which_position=new int *[size];
tab=new int *[size];
for(i=0;i<size;i++)
{
tab[i]=new int[size];
which_position[i]=new int[size];
}
for(i=0;i<size;i++)
{
for(j=0;j<size;j++)
{
k=rand()%size;
tab[i][j]=table_with_first[k];
which_position[i][j]=k;
 
     }
    }

for(k=0;k<size;k++)
{
for(i=1;i<size;i++)
for(j=size-1;j>=i;j--)
 if(tab[k][j]<tab[k][j-1])
 {
 int tmp1=tab[k][j-1];
 tab[k][j-1]=tab[k][j];
 tab[k][j]=tmp1;
 }
}
for(k=0;k<size;k++)
{
for(i=1;i<size;i++)
for(j=size-1;j>=i;j--)
 if(which_position[k][j]<which_position[k][j-1])
 {
 int tmp1=which_position[k][j-1];
 which_position[k][j-1]=which_position[k][j];
 which_position[k][j]=tmp1;
 }
}

    FILE *file1,*file2;
    file1=fopen("my_first_in_array","wt");
    file2=fopen("my_position_in_array","wt");
    for(i=0;i<size;i++)
    {
for(j=0;j<size;j++)
{
fprintf(file1,"%d  ",tab[i][j]);
fprintf(file2,"%d  ",which_position[i][j]);
}
fprintf(file1,"\n");
fprintf(file2,"\n");
}
fclose(file2);
fclose(file1);


 
delete table_with_first;
for(i=0;i<size;i++)
{
delete [] which_position[i];
delete [] tab[i];
}
delete []*which_position;
delete []*tab;
return 0;
}

Brak komentarzy:

Prześlij komentarz