Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to implement heap sort into my code, it should take numbers from the .txt file and sort them using heap sort however, I am getting the error,
"Cannot implicitly convert type 'int' to 'int[]'"
this is showing up on the line
C#
int[] arr = value;

I don't know how to fix this issue, any help would be appreciated, thanks.

What I have tried:

Here is the code:
C#
static void HeapSort(int[] arr, int n)
        {
            for (int i = n / 2 - 1; i >= 0; i--)
                Heapify(arr, n, i);
            for (int i = n - 1; i >= 0; i--)
            {
                int temp = arr[0];
                arr[0] = arr[i];
                arr[i] = temp;
                Heapify(arr, i, 0);
            }
        }
        static void Heapify(int[] arr, int n, int i)
        {
            int largest = i;
            int left = 2 * i + 1;
            int right = 2 * i + 2;
            if (left < n && arr[left] > arr[largest])
                largest = left;
            if (right < n && arr[right] > arr[largest])
                largest = right;
            if (largest != i)
            {
                int swap = arr[i];
                arr[i] = arr[largest];
                arr[largest] = swap;
                Heapify(arr, n, largest);
            }
        }
        public static void Main(string[] args)
        {
            string userInput;
            Console.WriteLine("Which Array do you want to sort?");
            Console.WriteLine("1. Net_1\n2. Net_2\n3. Net_3");
            userInput = Console.ReadLine();
            if (userInput == "1")
            {
                string[] text = File.ReadAllLines("Net_1\\Net_1.txt");
                List<int> values = new List<int>(text.Length);
                int n = 10, i;
                foreach (string line in text)
                    {
                    int value;
                    if (int.TryParse(line,out value))
                        {
                            values.Add(value);
                        }

                    int[] arr = value;
                    for (i = 0; i < n; i++)
                    {
                        Console.Write(arr[i] + " ");
                    }
                    HeapSort(arr, 10);
                    Console.Write("\nSorted Array is: ");
                    for (i = 0; i < n; i++)
                    {
                        Console.Write(arr[i] + " ");
                    }
                }
            }
Posted
Updated 10-Mar-20 2:18am
Comments
RamiroX 10-Mar-20 8:22am    
Your "value" is an int not an int array. If you want to have an array from your list "values" use values.ToArray().

You're trying to assign a single int variable (value) to an array (arr), which obviously won't work.

You're also trying to sort and output the array on each iteration of the first foreach loop, which is almost certainly not what you want.

Move the sorting and output outside of the foreach loop, and use values.ToArray() to get an array of values.

You'll also want to use the array's Length property instead of the variable n, in case the text doesn't contain exactly n valid integers.
C#
foreach (string line in text)
{
    int value;
    if (int.TryParse(line,out value))
    {
        values.Add(value);
    }
}

int[] arr = values.ToArray();
for (i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + " ");
}

HeapSort(arr, arr.Length);

Console.Write("\nSorted Array is: ");
for (i = 0; i < arr.Length; i++)
{
    Console.Write(arr[i] + " ");
}
 
Share this answer
 
Comments
CPallini 10-Mar-20 8:18am    
5.
C#
int[] arr = value;

The variable value is a single integer, so you cannot store it into an array. You can store it into a single element of an array thus:
C#
arr[0] = value;

But first you need to create your array given its size:
C#
int[] arr = new int[the_number_of_elements_needed];

In your code, you use the fixed value 10 to count through the array, but it would be better if you used the actual number of items in your list:
C#
int[] arr = new int[values.Count];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900