I am currently trying to create an array and display it reverse or descending order. It currently displays a list of numbers but sometimes it does not follow the correct descending order. I believe the issues is in the if statement in between the two for loops, each time I am comparing a random number between 1-101 with the first number in your array. Instead of doing it that way, How can I compare the numbers in the array with each other? Or any suggestion in proving my reverse order array generator?
CODE
namespace reverseArray
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
long operations = 0;
int size;
int max;
int[] createArray;
int[] sortArray;
int[] copyArray;
public void ReverseOrder()
{
size = Convert.ToInt32(textBoxSize.Text);
max = Convert.ToInt32(textBoxMax.Text);
createArray = new int[size];
copyArray = new int[size];
sortArray = new int[size];
createArray[size - 1] = 1;
for (int i = size - 1; i > 0; i--)
{
createArray[i - 1] = createArray[i] + r.Next(1, max);
}
for (int i = size - 1; i > 0; i--)
{
if (r.Next(1, 101) < createArray[0])
{
for (int x = size - 1; x > 0; x--)
{
createArray[x] = r.Next(1, createArray[0]);
}
}
}
}
private void buttonCreateArray_Click(object sender, EventArgs e)
{
ReverseOrder();
}
}
}
r.Nextlooks like aRandominstance. What on earth are you doing in the loops?createArray,copyArrayandsortArray), which is supposed to be the array ofints in descending order?createArraycreateArrayis the array ofints in descending order, what is the original array that you created initially?createArrayis the original array created initially. Then the for loops arranges(or tries to) the array in descending order.