What's wrong? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

21st Dec 2022, 4:47 AM
Javascript Developer
Javascript Developer - avatar
13 Answers
+ 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.
21st Dec 2022, 5:37 AM
Tibor Santa
Tibor Santa - avatar
+ 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.
21st Dec 2022, 11:38 AM
Hassanah
Hassanah - avatar
+ 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
23rd Dec 2022, 2:30 AM
Prashant Priyadarshi
Prashant Priyadarshi - avatar
+ 2
Zeinab I also did a shorter version using Zip. https://code.sololearn.com/c3ffRYncYO71/?ref=app
23rd Dec 2022, 5:29 AM
Bob_Li
Bob_Li - avatar
+ 1
If your array has 4 elements, the index goes from 0 to 3, not from 1 to 4.
21st Dec 2022, 5:21 AM
Tibor Santa
Tibor Santa - avatar
+ 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?
21st Dec 2022, 1:08 PM
Javascript Developer
Javascript Developer - avatar
+ 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.
21st Dec 2022, 1:46 PM
Hassanah
Hassanah - avatar
+ 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.
21st Dec 2022, 1:49 PM
Hassanah
Hassanah - avatar
+ 1
Coding problem
22nd Dec 2022, 11:11 AM
Anord Muganyizi
Anord Muganyizi - avatar
+ 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];
22nd Dec 2022, 1:07 PM
Jhon Madrin
Jhon Madrin - avatar
+ 1
I need to lean on priming
23rd Dec 2022, 5:47 AM
Anord Muganyizi
Anord Muganyizi - avatar
+ 1
Thanks guys🙏🏽
24th Dec 2022, 6:32 AM
Javascript Developer
Javascript Developer - avatar
0
Tibor Santa i fixed it but not working yet
21st Dec 2022, 5:28 AM
Javascript Developer
Javascript Developer - avatar