poniedziałek, 11 stycznia 2021

Combine duplicate arrays into one (C++)

 #include <iostream>

#define m_min 5

#define m_max 10

using namespace std;


int **connect_Tab(int **tab1,int **tab2, int width, int height)

{

int **base,i,j,k,l;

k=2*width;

l=height;

base=new int *[k];

for(i=0;i<k;i++)

base[i]=new int[l];

int i1,j1;

for(i=0,i1=1,j1=0;i<width;i++,i1+=2,j1+=2)

{

for(j=0;j<height;j++)

{

base[j1][j]=tab1[j][i];

base[i1][j]=tab2[j][i];

}

}

return base;

}


int main(int argc, char **argv)

{

int i,j,k,l;

int **tab1,**tab2;

tab1=new int *[m_min];

tab2=new int *[m_min];

for(i=0;i<m_min;i++)

{

tab1[i]=new int[m_min];

tab2[i]=new int[m_min];

}

k=0,l=1;

for(i=0;i<m_min;i++)

{

for(j=0;j<m_min;j++)

{

tab1[i][j]=k;

tab2[i][j]=l;

k+=2;

l+=2;

}

}

for(i=0;i<m_min;i++)

{

for(j=0;j<m_min;j++)

{

cout<<tab1[i][j]<<" ";

}

cout<<"\n";

}

cout<<"\n\n";

for(i=0;i<m_min;i++)

{

for(j=0;j<m_min;j++)

{

cout<<tab2[i][j]<<" ";

}

cout<<"\n";

}

cout<<"\n\n";

int **main_Tab=connect_Tab(tab1,tab2,m_min,m_min);

for(i=0;i<m_min;i++)

{

for(j=0;j<m_max;j++)

{

cout<<main_Tab[j][i]<<"  ";

}

cout<<"\n";

}

for(i=0;i<m_min;i++)

{

delete [] tab1[i];

delete [] tab2[i];

}

delete [] tab1;

delete [] tab2;

return 0;

}

YOU CAN PAY ME - https://paypal.me/LukaszW77?locale.x=pl_PL

niedziela, 10 stycznia 2021

Writing data to a file (C/C++)

 


#include <iostream>

#include <stdio.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdlib.h>

#include <fstream>

#include <time.h>


#define b_size 10

#define max_value 1000


using namespace std;


int sum_T(int *tab, int n)

{

int s=0;

for(int i=0;i<n;i++)

s+=tab[i];

return s;

}

double aver_T(int *tab, int n)

{

double av;

av=(1.00*sum_T(tab,n))/(1.00*n);

return av;

}


bool is_True(int value)

{

if(value==2)

return true;

else

return false;

}

bool is_First(int value)

{

int i,sum=0;

for(i=1;i<value+1;i++)

if(value%i==0)

  ++sum;

return is_True(sum);

}

void sort_Tab(int* tab)

{

int i,j,k;

for(i=1;i<b_size;i++)

for(j=b_size-1;j>=i;j--)

  if(tab[j]<tab[j-1])

  {

  k=tab[j-1];

  tab[j-1]=tab[j];

  tab[j]=k;

  }

}


int *base_Tab;

int **main_Tab;

int *sum_Tab;

double *aver_Tab;

int main(int argc, char **argv)

{

int i,j,k;

char *files[3]={"base.txt","sum.txt","aver.txt"};

srand(time(NULL));

base_Tab=new int[max_value];

main_Tab=new int *[b_size];

sum_Tab=new int[b_size];

aver_Tab=new double[b_size];

for(i=0;i<b_size;i++)

main_Tab[i]=new int[b_size];

i=0;

j=2;

do

{

if(is_First(j))

{

base_Tab[i]=j;

++i;

}

++j;

}while(i<max_value);

for(i=0;i<b_size;i++)

{

for(j=0;j<b_size;j++)

{

k=rand()%max_value;

main_Tab[i][j]=base_Tab[k];

}

    }

    for(i=0;i<b_size;i++)

     sort_Tab(main_Tab[i]);

for(i=0;i<b_size;i++)

sum_Tab[i]=sum_T(main_Tab[i],b_size);

for(i=0;i<b_size;i++)

aver_Tab[i]=aver_T(main_Tab[i],b_size);

 

fstream stream_File;

stream_File.open(files[0],ios::out);

for(i=0;i<b_size;i++)

{

for(j=0;j<b_size;j++)

{

stream_File<<main_Tab[i][j]<<" ";

}

stream_File<<"\n";

}

stream_File.close();

FILE* fp;

fp=fopen(files[1],"w");

for(i=0;i<b_size;i++)

fprintf(fp,"%d\n",sum_Tab[i]);

fclose(fp);

int fd=open(files[2],O_WRONLY | O_CREAT);

for(i=0;i<b_size;i++)

write(fd,"%.2f\n",aver_Tab[i]);

close(fd);

   

delete [] base_Tab;

for(i=0;i<b_size;i++)

delete [] main_Tab[i];

delete [] main_Tab;

delete [] sum_Tab;

delete [] aver_Tab;

return 0;

}

YOU CAN PAY ME - https://paypal.me/LukaszW77?locale.x=pl_PL

piątek, 1 stycznia 2021

Class with fstream (C++)

 #include <iostream>

#include <fstream>


using namespace std;



class change_TAB

{

private:

fstream fp1;

fstream fp2;

int **tab1;

int **tab2;

int width;

int height;

int ss;

char *f1,*f2;

public:

change_TAB(char* file_Start, char* file_End, int x, int y, int s)

{

int i;

width=x;

height=y;

ss=s;

f1=file_Start;

f2=file_End;

tab1=new int *[width];

tab2=new int *[width];

for(i=0;i<width;i++)

{

tab1[i]=new int[height];

tab2[i]=new int[height];

}

}

~change_TAB()

{

int i;

for(i=0;i<width;i++)

{

delete [] tab1[i];

delete [] tab2[i];

}

delete [] tab1;

delete [] tab2;

}

void Read()

{

int i,j;

fp1.open(f1,ios::in);

for(i=0;i<width;i++)

for(j=0;j<height;j++)

  fp1>>tab1[i][j];

fp1.close();

}

void Write()

{

int i,j;

for(i=0;i<width;i++)

for(j=0;j<height;j++)

  tab2[i][j]=tab1[i][j]*ss;

fp2.open(f2,ios::out);

for(i=0;i<width;i++)

{

for(j=0;j<height;j++)

{

fp2<<tab2[i][j]<<" ";

}

fp2<<'\n';

}

fp2.close();

}

};



int main()

{

change_TAB TABLES("old_Tab","new_Tab",100,200,5);

TABLES.Read();

TABLES.Write();

return 0;

}


YOU CAN PAY ME - https://paypal.me/LukaszW77?locale.x=pl_PL