poniedziałek, 14 listopada 2016

Sum and average in C++

bool __fastcall TForm4::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 __fastcall TForm4::write_First(int x)
{
int i,sum,k;
float average;
k=0;
average=0.;
sum=0;
for(i=1;i<x+1;i++)
{
if(is_First(i))
{
ListBox2->Items->Add(IntToStr(i));
++k;
sum+=i;
}
}
average=sum/(k*1.);
Edit3->Text=IntToStr(sum);
Edit7->Text=FloatToStr(average);

}
 void __fastcall TForm4::even_N(int x)
 {
int i,j,sum,k;
int sum1,sum2;
float average1,average2;

k=0;sum1=0.;sum2=0.;
for(i=1,j=2;i<x+2;i+=2,j+=2)
{
ListBox3->Items->Add(IntToStr(j));
ListBox4->Items->Add(IntToStr(i));
++k;
sum1+=j;
sum2+=i;

}
average1=sum1/(k*1.);
average2=sum2/(k*1.);
Edit4->Text=IntToStr(sum1);
Edit8->Text=FloatToStr(average1);
Edit5->Text=IntToStr(sum2);
Edit9->Text=FloatToStr(average2);
 }
  void __fastcall TForm4::clear_L()
  {
 ListBox1->Items->Clear();
 ListBox2->Items->Clear();
 ListBox3->Items->Clear();
 ListBox4->Items->Clear();
  }
void __fastcall TForm4::Button2Click(TObject *Sender)
{
 int i,j,k,sum;
 float average;
 clear_L();
 i=StrToInt(Edit1->Text);
 Label9->Caption=FloatToStr(1./(1*i));
 Label11->Caption=FloatToStr(sqrt(1.*i));


 sum=0;
 k=0;
 average=0.0;
 for(j=1;j<i+i;j++)
 {
  if(i%j==0)
  {
   ListBox1->Items->Add(IntToStr(j));
   ++k;
   sum+=j;
  }
 }
 average=sum/(k*1.);
 Edit2->Text=IntToStr(sum);
 Edit6->Text=FloatToStr(average);
 write_First(i);
 even_N(i);
}
//---------------------------------------------------------------------------
void __fastcall TForm4::Button1Click(TObject *Sender)
{
 Close();
}

Brak komentarzy:

Prześlij komentarz