Using methods to make a program that allows you to choose different search/sort methods. For the bubble sort, when i try and output the arrays in the bottom of the code, It prints 'System.Int32[]'. Also, The code never actually ends, it just prints out 'System.Int32[]' and 'End of pass _ '. How do i stop this from happening? Thanks
i have tried swapping {0} for the actual variable names, and changing the values of 'b < _' in the for loop.
int pass_count = 0;
bool sorted = false;
int[] changing_array = new int[5];
int[] final_array = new int[5];
int[] starting_array = new int[5];
for (int a = 0; a < 5; a++)
{
Console.WriteLine("Input number {0}", (a + 1));
changing_array[a] = Convert.ToInt32(Console.ReadLine());
}
Array.Copy(changing_array, final_array, 5);
Array.Copy(changing_array, starting_array, 5);
Array.Sort(final_array);
while (sorted == false)
{
for (int b = 0; b < 4; b++)
{
int c = b++;
int temp;
if (changing_array[b] > changing_array[c])
{
temp = changing_array[b];
changing_array[b] = changing_array[c];
changing_array[c] = temp;
}
else
{
continue;
}
}
pass_count++;
if (changing_array == final_array)
{
Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ",pass_count,starting_array,final_array);
sorted = true;
}
else
{
Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ",pass_count,starting_array,changing_array);
}
}
Assuming the numbers are "65,34,23,87,30" at the end i would like it to print "It took {0} passes to sort \n'65,34,23,87,30' \ninto \n'23,30,34,65,87' ", however it just prints 'System.Int32[]' and 'End of pass _ '.
There are a couple of problems in your code.
In the for
loop you are doing:
int c = b++;
However this does not do what you intend. It first assigns the value of b
to c
and then increases b
by one. This means this increments the for
-loop variable and you are essentially skipping iterations thanks to this, in addition to the fact that the values of c
and b
are now "swapped".
Instead you need to do this:
int c = b+1;
This assigns c
to be one greater than b
and leaves b
unchanged.
In the end you are comparing two array instances:
if (changing_array == final_array)
This will not work. Arrays are reference types in C# and this just checks if both variables point to the same place in memory (which they don't). Instead, you probably want SequenceEqual
:
if (changing_array.SequenceEqual(final_array))
This method compares items in two arrays one by one and returns true only if both have the same contents.
After these changes the while
loop finishes.
For the output, you cannot pass an array to Console.WriteLine
directly. This method can handle only simple data types and does not know how to output an array, so instead it just writes out the type name. Instead, you should turn the array into an integer. The easiest solution is the string.Join
method:
string.Join(",",starting_array)
This will create a string which contains the array items separated by comma.
Complete version of your code after these changes:
int pass_count = 0;
bool sorted = false;
int[] changing_array = new int[5];
int[] final_array = new int[5];
int[] starting_array = new int[5];
for (int a = 0; a < 5; a++)
{
Console.WriteLine("Input number {0}", (a + 1));
changing_array[a] = Convert.ToInt32(Console.ReadLine());
}
Array.Copy(changing_array, final_array, 5);
Array.Copy(changing_array, starting_array, 5);
Array.Sort(final_array);
while (!sorted)
{
for (int b = 0; b < 4; b++)
{
int c = b+1;
int temp;
if (changing_array[b] > changing_array[c])
{
temp = changing_array[b];
changing_array[b] = changing_array[c];
changing_array[c] = temp;
}
else
{
continue;
}
}
pass_count++;
if (changing_array.SequenceEqual(final_array))
{
Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ", pass_count, string.Join(",",starting_array), string.Join(",", final_array));
sorted = true;
}
else
{
Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ", pass_count, string.Join(",", starting_array), string.Join(",", final_array));
}
}
Also note I have used !solved
instead of solved == false
. Both are equivalent, but the first version is more commonly used and more succinct.