Reputation: 13
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 _ '.
Upvotes: 1
Views: 79
Reputation: 39092
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.
Upvotes: 1
Reputation: 74660
It's probably easiest to go through your code and add some comments:
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)
{
//you should consider having a bool here to track whether a change was made to the array
//if the for loop runs without making a change, the array is sorted
for (int b = 0; b < 4; b++) //don't use 4, use changing_array.Length - 1
{
int c = b++; //i'd avoid this, because it's potentially confusing as to what b and c values are after it runs. remove use of C and just use b+1 instead
int temp; //move this into the if, it doesn't need to be out here
if (changing_array[b] > changing_array[c])
{
temp = changing_array[b];
changing_array[b] = changing_array[c];
changing_array[c] = temp;
//you made a change, so set your changeMade bool to true here
}
else //this else is totally redundant - the only thing it instructs is to continue looping, which will happen anyway
{
continue;
}
}
//test your changeMade Boolean here - if it is false, then set sorted = true
pass_count++;
//this isn't how you compare if all the elements of array 1 are in the same order as array 2
//this is how you compare whether two array variables refer to the same object in memory
//they don't, so this is always false
if (changing_array == final_array)
{
//this prints Int32[] because when you pass an object into something that
//needs a string, the runtime calls ToString() on it to make it into a string
//ToString() isn't anything special for an array, it doesn't print the elements
//so the default object.ToString() is used, which just prints the type of the object
//one way to turn an array into a string representation is to say
//string.Join(",", starting_array)
//The join method will visit each element, adding them to a comma separated string
Console.WriteLine("It took {0} passes to sort \n{1} \ninto \n{2} ",pass_count,starting_array,final_array);
sorted = true;
}
else //the reason your loop runs forever is because this else always runs, and sorted is never made true
{
Console.WriteLine("End of pass {0}. \n{1} \nis now \n{2} ",pass_count,starting_array,changing_array);
}
}
Upvotes: 0
Reputation: 21406
The reason why you are getting into an infinite loop is because control in your app is going to else part of if within sorted == false loop; in this else part sorted remains false and so loop continues.
So, you must break
out the loop in the else part of the loop as shown below to prevent infinite loop by using the break statement as shown in code below. Also, instead of using the break statement you could just set sorted = true;
and that will also prevent infinite loop.
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);
break;//this is what will prevent infinite loop
}
Upvotes: 0
Reputation: 2619
Use the string.Join method:
string.Join(",", yourArray)
...instead of trying to output the array object itself.
Upvotes: 0