+ 1
What's wrong?
I want to move array's A and B into empty array S, whit merge sort in algorithms. https://code.sololearn.com/c8PTTW8iT2O2/?ref=app
13 ответов
+ 3
Are you actually reading your error messages? You still have index error, meaning that you are trying to access an element in one of the arrays that doesn't exist.
Learn to debug your code. It is easier if you can do it on a computer, in an IDE (integrated development environment, a specialized software for code development).
But on Sololearn you can still insert some Console.WriteLine statements at some critical points, to examine the value of your variables during runtime.
+ 2
I am not sure where the error exactly lies within your code, but as Tibor Santa said, it is to do with the index, where it is out of bounds (so trying to access an element that does not exist).
You do have the right idea, but maybe change your code a little.
So instead of the while loop, use 2 for loops (and remove the if/else statement you added at the end, I feel that causes the error):
for(i=0;i<4; i++)
{
s[i] = a[i];
}
for(j=0;j<4; j++)
{
s[i] = b[j];
i++;
}
These two loops iterate from 0 to 3 (as you have 4 items in each list) and adds them to your third list 's'. Now you just need to output the list, which is simple, you can either do a for loop or a foreach loop for that.
I'm not sure if you want this third array sorted, because in its current form, it simply adds each item from the two lists, so is not in numerical order.
Hope that helps.
+ 2
var res =a.Concat(b)
.OrderByDescending(v=>v)
.ToArray();
For merge sort to work on array you need to take care of the case when length of one array not equal to another array also
+ 2
Zeinab
I also did a shorter version using Zip.
https://code.sololearn.com/c3ffRYncYO71/?ref=app
+ 1
If your array has 4 elements, the index goes from 0 to 3, not from 1 to 4.
+ 1
Hassanah thanks for the explanation
I actually want to move array a and b to s but in order. I think the problem is with the array s which is empty at the beginning
Could we ever declare an empty array in c# and then fill it during the program?
+ 1
Yes you can do that, whether you fill the empty array manually, or through user input, but you can definitely fill the array in the program.
+ 1
Perhaps try moving array a's contents into array s and then b's contents into array s, and then at the end sort array s in order? That seems a bit easier to do, and I do know this is a bit complicated to program.
+ 1
Coding problem
+ 1
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Sololearn { class Program { static void Main(string[] args) { int[] a={8,22,45,89}; int[] b={6,9,34,43}; int[] s=new int[a.Length + b.Length]; int i=0; int j=0; int k=0; while(i<a.Length && j<b.Length){ if(a[i]>b[j]){ s[k]=a[i]; i++; }else{ s[k]=b[j]; j++; } k++; } if(i == a.Length && j < b.Length){ for (int l = j; l < b.Length; l++) { s[k] = b[l];
+ 1
I need to lean on priming
+ 1
Thanks guys🙏🏽
0
Tibor Santa i fixed it but not working yet