Number doesn't get printed

using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Solution s = new Solution();
int[] firstArr = {1,2,3,4,5,7,9,10};
int[] secondArr = {13,15,17,21,22,23};
s.FindMedianSortedArrays(firstArr, secondArr);
}
}
public class Solution {
public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
int [] _nums1 = nums1;
int [] _nums2 = nums2;
int index1 = 0;
int index2 = 0;
int _nums1LastValue = _nums1[_nums1.Length-1];
int _nums2LastValue = _nums2[_nums2.Length-1];
int[] mergedArray = new int[_nums1.Length+_nums2.Length];
while(true){
if(_nums1[index1] <= _nums2[index2]){
mergedArray[index1+index2] = _nums1[index1];
index1++;
if(index1 > _nums1.Length - 1){
index1 = _nums1.Length-1;
_nums1[index1] = 999999;
}
}
else{
mergedArray[index1+index2] = _nums2[index2];
index2++;
if(index2 > _nums2.Length - 1){
index2 = _nums2.Length-1;
_nums2[index2] = 999999;
}
}
if(_nums1[index1] == 999999 && _nums2[index2] == 999999){
_nums1[index1] = _nums1LastValue;
_nums2[index2] = _nums2LastValue;
foreach(int i in mergedArray){
System.Console.WriteLine(i);
}

if(mergedArray.Length%2==0){
return Convert.ToDouble(mergedArray[(mergedArray.Length - 1)/2] + mergedArray[(mergedArray.Length)/2])/2;
}
else{
return mergedArray[(mergedArray.Length-1)/2];
}
}
}

}
}
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Solution s = new Solution();
int[] firstArr = {1,2,3,4,5,7,9,10};
int[] secondArr = {13,15,17,21,22,23};
s.FindMedianSortedArrays(firstArr, secondArr);
}
}
public class Solution {
public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
int [] _nums1 = nums1;
int [] _nums2 = nums2;
int index1 = 0;
int index2 = 0;
int _nums1LastValue = _nums1[_nums1.Length-1];
int _nums2LastValue = _nums2[_nums2.Length-1];
int[] mergedArray = new int[_nums1.Length+_nums2.Length];
while(true){
if(_nums1[index1] <= _nums2[index2]){
mergedArray[index1+index2] = _nums1[index1];
index1++;
if(index1 > _nums1.Length - 1){
index1 = _nums1.Length-1;
_nums1[index1] = 999999;
}
}
else{
mergedArray[index1+index2] = _nums2[index2];
index2++;
if(index2 > _nums2.Length - 1){
index2 = _nums2.Length-1;
_nums2[index2] = 999999;
}
}
if(_nums1[index1] == 999999 && _nums2[index2] == 999999){
_nums1[index1] = _nums1LastValue;
_nums2[index2] = _nums2LastValue;
foreach(int i in mergedArray){
System.Console.WriteLine(i);
}

if(mergedArray.Length%2==0){
return Convert.ToDouble(mergedArray[(mergedArray.Length - 1)/2] + mergedArray[(mergedArray.Length)/2])/2;
}
else{
return mergedArray[(mergedArray.Length-1)/2];
}
}
}

}
}
3 Replies
dyjheuruzd929iud74jd
Hello, I have a code that is supposed to return the median of two sorted arrays. This is my code. It doesn't return the median. I tried logging my mergedArray to the console. The output in my console should be: 1 2 3 4 5 7 9 10 13 15 17 21 22 23 Instead the console outputs: 1 2 3 4 5 7 9 13 15 17 21 22 23 0 The 10 doesn't get Writen in my console and thus also isn't included in my mergedArray for some reason Btw I know that my code is very ugly and not optimal, but I am primarily trying to look for the reason why the number 10 isn't included in my merged array. So far I tried asking chatgpt, claude and grok, but none of these AIs seem to know why the 10 isn't included.
Keswiik
Keswiik5w ago
to start with, have you tried debugging this to see what is happening in your program? Much easier to find bugs when you can step through each statement and find where things don't behave correctly
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?