środa, 11 listopada 2020

Writing a StringGrid to a file (C++ Builder)

#include <Grids.hpp>


 void __fastcall TForm1::save_SG(TStringGrid *table,UnicodeString filename)

{

UnicodeString **values;


int i,j,size_x,size_y;

size_x=table->ColCount;

size_y=table->RowCount;

values=new UnicodeString *[size_x];

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

values[i]=new UnicodeString[size_y];

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

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

  values[i][j]=table->Cells[i][j];

std::auto_ptr<TStreamWriter> write_Data(new TStreamWriter(filename,false));

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

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

  write_Data->WriteLine(values[i][j]);

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


delete [] values[i];

    delete [] values;

}

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

wtorek, 10 listopada 2020

Swap double arrays (C++ - new/delete)

 #include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;



void change_Arrays(int** tab1, int** tab2, int m_size)

{

int **temp;

int i,j;

temp=new int *[m_size];

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

temp[i]=new int[m_size];

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

{

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

{

temp[i][j]=tab1[i][j];

}

}

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

{

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

{

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

}

}

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

{

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

{

tab2[i][j]=temp[i][j];

}

}

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

delete [] temp[i];

delete [] temp;

}


int sum_double_Array(int **tab, int m_size)

{

int i,j,sum;

sum=0;

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

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

  sum+=tab[i][j];

return sum;

}

double aver_double_Array(int **tab, int m_size)

{

double aver;

aver=(1.00*sum_double_Array(tab,m_size))/(1.00*m_size);

return aver;

}

int main()

{

srand(time(NULL));

int i,j,k;

int **tab1,**tab2;

int sum1, sum2;

double aver1,aver2;

k=20;

tab1=new int *[k];

tab2=new int *[k];

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

{

tab1[i]=new int[k];

tab2[i]=new int[k];

}

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

{

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

{

tab1[i][j]=rand()%k+1;

tab2[i][j]=rand()%k+1;

}

}

sum1=sum_double_Array(tab1,k);

sum2=sum_double_Array(tab2,k);

aver1=aver_double_Array(tab1,k);

aver2=aver_double_Array(tab2,k);

cout<<"Sum table 1: "<<sum1<<endl;

cout<<"Sum table 2: "<<sum2<<endl;

cout<<"Aver table 1: "<<aver1<<endl;

cout<<"Aver table 2: "<<aver2<<endl;

change_Arrays(tab1,tab2,k);

sum1=sum_double_Array(tab1,k);

sum2=sum_double_Array(tab2,k);

aver1=aver_double_Array(tab1,k);

aver2=aver_double_Array(tab2,k);

cout<<"AFTER CHANGE: \n";

sum1=sum_double_Array(tab1,k);

sum2=sum_double_Array(tab2,k);

aver1=aver_double_Array(tab1,k);

aver2=aver_double_Array(tab2,k);

cout<<"Sum table 1: "<<sum1<<endl;

cout<<"Sum table 2: "<<sum2<<endl;

cout<<"Aver table 1: "<<aver1<<endl;

cout<<"Aver table 2: "<<aver2<<endl;

change_Arrays(tab1,tab2,k);

sum1=sum_double_Array(tab1,k);

sum2=sum_double_Array(tab2,k);

aver1=aver_double_Array(tab1,k);

aver2=aver_double_Array(tab2,k);

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

{

delete [] tab2[i];

delete [] tab1[i];

}

delete [] tab2;

delete [] tab1;

return 0;

}

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