I want to draw the numbers in the table, the sum of which is equal to the sum previously proposed. The algorithm draws a different number when the sum is equal to the sum of the set.
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Close();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
srand(time(NULL));
int size_table, range,sum_table;
int sum_temporary;
int i,k;
size_table=Int32::Parse(textBox1->Text);
range=Int32::Parse(textBox2->Text);
sum_table=Int32::Parse(textBox3->Text);
dataGridView1->ColumnCount=size_table;
dataGridView1->RowCount=1;
dataGridView1->Rows[0]->Height=70;
array<System::Int32>^ temporary_value=gcnew array<System::Int32>(range);
array<System::Int32>^ result_value=gcnew array<System::Int32>(size_table);
for(;;)
{
for(i=0;i<range;i++)
temporary_value[i]=0;
sum_temporary=0;
i=0;
do
{
do
{
k=rand()%range;
}while(temporary_value[k]!=0);
temporary_value[k]=1;
result_value[i]=k;
sum_temporary+=k;
i++;
}while(i<size_table);
if(sum_temporary==sum_table)
break;
}
for(i=0;i<size_table;i++)
dataGridView1->Rows[0]->Cells[i]->Value=result_value[i].ToString();
textBox4->Text=sum_table.ToString();
}
};
}
Brak komentarzy:
Prześlij komentarz