private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
g1 = panel1->CreateGraphics();
g2 = panel2->CreateGraphics();
blackPen = gcnew Pen(Color::Black);
blackBrush = gcnew SolidBrush(Color::Black);
arial8Font = gcnew System::Drawing::Font("Arial",8);
}
private: void DrawArray(int arr[], int n, int panel)
{
// Declare variables
int x = 40; // x coordinate (40 pixels from left edge
int y = 0; // y coordinate (at top of the panel)
int height = 20; // bar height set to 20 pix
int width; // the width of the bar
Graphics^ g;
// Decide which panel to draw on
if (panel == 1)
{
panel1->Refresh();
g = g1;
}
else
{
panel2->Refresh();
g = g2;
}
// loop to draw the bars
for (int i = 0; i < n; i++)
{
y += height;
width = arr[i];
g->DrawString(arr[i].ToString(), arial8Font, blackBrush,x-30,y);
Rectangle bar(x, y, width, height);
g->DrawRectangle(blackPen, bar);
}
}
private: System::Void btnSort_Click(System::Object^ sender, System::EventArgs^ e) {
int data[] = {34,67,21,48,15,92,56,37,71,11};
int n = 10; // the size of the array
int i, j; // loop control variables
int smallIndex; // the index of the smallest value
DrawArray(data, n, 1); // draw unsorted array in panel1
for (i = 0; i < n-1; i++)
{
smallIndex = i;
for (j = i+1; j < n; j++)
if (data[j] < data[smallIndex]) smallIndex = j;
Swap(data[i], data[smallIndex]);
}
DrawArray(data, n, 2); // draw sorted array in panel2
}
// See page 420 Example 9-1
void Swap(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
};
}
How do I going about changing this code so if will draw in my graph in desending order?? The only seems to show me how to do ascending order.
for (i = 0; i > j-1; i++)
{
largeIndex = i;
for (i = i+1; i > n; i++)
if (data[i] > data[largeIndex]) largeIndex = i;
Swap(data[j], data[largeIndex]);
}