piątek, 26 lipca 2019

Structure data and file (C++)

#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;

#define max_size 10


bool is_First(int value)
{
int i,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 array_size)
{
int i,sum=0;
for(i=0;i<array_size;i++)
sum+=tab[i];
return sum; 
 
}
double aver_Sum(int *tab, int array_size)
{
double aver;
aver=(1.0*sum_Table(tab,array_size))/(1.*array_size);
return aver;
}
struct dates
{
int f1;
int s1;
double a1;
};

int main(int argc, char **argv)
{
int i,j,k;
fstream file1("date1.txt",ios::out);

struct dates *m_dates=new struct dates[max_size];


i=0;
j=2;
do
{
if(is_First(j))
{
m_dates[i].f1=j;
++i;
}
++j;
}while(i<max_size);
for(i=0;i<max_size;i++)
{
j=0;
for(k=1;k<m_dates[i].f1+1;k++)
{
j+=k;
 
}
   m_dates[i].s1=j;
}
i=0;
do
{
  k=0;
 int *temp=new int[m_dates[i].f1];
 for(j=1;j<m_dates[i].f1+1;j++)
 {
  k=j;
  temp[j-1]=k;
 }
 m_dates[i].a1=aver_Sum(temp,m_dates[i].f1);
 delete temp;
 ++i;
}while(i<max_size);


for(i=0;i<max_size;i++)
{
printf("%d)     %d   -    %d    -   %.2f\n",i+1,m_dates[i].f1,m_dates[i].s1,m_dates[i].a1);
}
for(i=0;i<max_size;i++)
{
file1<<m_dates[i].f1<<"    "<<m_dates[i].s1<<"   "<<m_dates[i].a1<<"\n";
file1.flush();
}
file1.close();

delete m_dates; 
return 0;
}

poniedziałek, 15 lipca 2019

Data from the file (C++ Builder)



My file:
point1.txt


10 10 35 40
35 40 100 40
100 40 150 50
150 50 220 50
220 50 220 180
220 180 270 270
270 270 330 330
330 330 350 400
350 400 400 420
400 420 500 500


The program reads the data and writes it out. 


private: // User declarations
        int date_Point1[2][10];
        int date_Point2[2][10];

void __fastcall TForm1::FormCreate(TObject *Sender)
{
 int i;
 AnsiString text[4]={"X1","Y1","X2","Y2"};
 for(i=0;i<4;i++)
  StringGrid1->Cells[i][0]=text[i];
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 int temp[4][10];
 int temp2[4*10];
 int temp3[4][10];
 int i,j,k;
 FILE *date;
 date=fopen("point1.txt","r");
 if(date==NULL)
 {
  ShowMessage("I can't open file");
  Close();
 }
 else
 {
  k=0;
  for(i=0;i<4;i++)
  {
   for(j=0;j<10;j++)
   {
    fscanf(date,"%d",&temp[i][j]);
    temp2[k]=temp[i][j];
    ++k;
   }
  }
  fclose(date);
 }
 k=0;
 i=0;
 do
 {

  for(j=0;j<4;j++)
  {
   temp3[i][j]=temp2[k];
   ++k;
   if(j==3)
    ++i;
  }
 }while(i<10);
 for(i=0;i<4;i++)
 {
  for(j=0;j<10;j++)
  {
   StringGrid1->Cells[i][j+1]=IntToStr(temp3[j][i]);

  }
 }
 for(i=0;i<10;i++)
 {
  for(j=0;j<2;j++)
  {
   date_Point1[j][i]=temp3[j][i];
   date_Point2[j][i]=temp3[j+2][i];
  }
 }
 PaintBox1->Canvas->MoveTo(date_Point1[0][0],date_Point2[0][0]);
 for(i=0;i<10;i++)
  for(j=0;j<2;j++)
   PaintBox1->Canvas->LineTo(date_Point1[j][i],date_Point2[j][i]);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
 Close();
}


sobota, 6 lipca 2019

Discrete knapsack problem (C - gcc)

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size_B 10
#define max_T 100
#define min_V 500
#define max_V 1000

int double_Arrays[size_B][size_B];
int tab_First[max_T];
int main_Values[size_B];

int sum_Array(int *tab, int size)
{
int i,sum;
sum=0;
for(i=0;i<size;i++)
sum+=tab[i];
return sum;
}
void ins_Sort(int *tab,  int size)
{
int i,j,k;
for(i=1;i<size;i++)
{
j=i;
k=tab[j];
while((j>0) && (tab[j-1]>k))
{
tab[j]=tab[j-1];
j--;
}
tab[j]=k;
}
}
int 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 1;
    else
     return 0;
     
}

void init_Tab()
{
int i,j;
i=0;
j=2;
do
{
if(is_First(j))
{
tab_First[i]=j;
++i;
}
++j;

}while(i<max_T);
for(i=0;i<size_B;i++)
{
main_Values[i]=rand()%(max_V-min_V)+min_V;
}
ins_Sort(main_Values,size_B);
}
void run_Arrays()
{
int i,j,k,l;
i=0;
do
{
 do
 {
for(j=0;j<size_B;j++)
{
k=rand()%max_T;
l=tab_First[k];
double_Arrays[i][j]=l;
    }
} while(sum_Array(double_Arrays[i],size_B)!=main_Values[i]);
++i;
}while(i<size_B);
for(i=0;i<size_B;i++)
{
ins_Sort(double_Arrays[i],size_B);
}
}

int main(int argc, char **argv)
{
int i,j;
srand(time(NULL));
init_Tab();
run_Arrays();
printf("Drawn 10 numbers: (500-1000)\n");
for(i=0;i<size_B;i++)
{
printf("%d  ",main_Values[i]);
}
printf("\n\n");

printf("The sum of the ten prime numbers: ");
printf("\n");
i=0;
do
{
for(j=0;j<size_B;j++)
{
printf("%d  ",double_Arrays[i][j]);
}
printf("\n");
++i;
}while(i<size_B);

return 0;
}

czwartek, 4 lipca 2019

Days and months 2019 (C-gcc)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv)
{
char months[12][20];
int months_values[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int day_months;
int sum[12];
char days[7][20];
int day;
char which_Day[20];
char which_Monat[20];
int i,j,s;
srand(time(NULL));
s=0;
for(i=0;i<12;i++)
{
s+=months_values[i];
sum[i]=s;

}
strcpy(months[0],"January");
strcpy(months[1],"February");
strcpy(months[2],"March");
strcpy(months[3],"April");
strcpy(months[4],"May");
strcpy(months[5],"June");
strcpy(months[6],"July");
strcpy(months[7],"August");
strcpy(months[8],"September");
strcpy(months[9],"October");
strcpy(months[10],"November");
strcpy(months[11],"December");
strcpy(days[6],"Monday");
strcpy(days[0],"Tuesday");
strcpy(days[1],"Wednesday");
strcpy(days[2],"Thursday");
strcpy(days[3],"Friday");
strcpy(days[4],"Saturday");
strcpy(days[5],"Sunday"); 
day=rand()%365+1;
i=1;
j=0;
do
{


if(j==7)
j=0;
++i;
++j;

}while(i<day);
strcpy(which_Day,days[j]);
if(day<sum[0]+1)
{
strcpy(which_Monat,months[0]);
day_months=day;
}
if(day>sum[0] && day<sum[1]+1)
{
strcpy(which_Monat,months[1]);
day_months=day-sum[0];
}
if(day>sum[1] && day<sum[2]+1)
{
strcpy(which_Monat,months[2]);
day_months=day-sum[1];
}
if(day>sum[2] && day<sum[3]+1)
{
strcpy(which_Monat,months[3]);
day_months=day-sum[2];
}
if(day>sum[3] && day<sum[4]+1)
    {
strcpy(which_Monat,months[4]);
day_months=day-sum[3];
}
if(day>sum[4] && day<sum[5]+1)
{
strcpy(which_Monat,months[5]);
day_months=day-sum[4];
}
if(day>sum[5] && day<sum[6]+1)
{
strcpy(which_Monat,months[6]);
day_months=day-sum[5];
}
if(day>sum[6] && day<sum[7]+1)
{
strcpy(which_Monat,months[7]);
day_months=day-sum[6];
}
if(day>sum[7] && day<sum[8]+1)
{
strcpy(which_Monat,months[8]);
day_months=day-sum[7];
}
if(day>sum[8] && day<sum[9]+1)
{
strcpy(which_Monat,months[9]);
day_months=day-sum[8];
}
if(day>sum[9] && day<sum[10]+1)
{
strcpy(which_Monat,months[10]);
day_months=day-sum[9];
}
if(day>sum[10] && day<sum[11]+1)
{
strcpy(which_Monat,months[11]);
day_months=day-sum[10];
}  
 
printf("Value =  %d\n",day);
printf("Month = %s\n",which_Monat);
printf("Day = %d\n",day_months);
printf("Day of the week = %s\n",which_Day);



return 0;
}