Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This code gives output but it has one problem that is when user write 5,6 in textbox1 and 7,8 in textbox3 it gives output 5,6.
I know the problem is that when the elements of an array ends,it doesnt print the rest elements of other array, I commented on line of problem.

*I used textbox1 and textbox3 for getting the elements of the arrays that user wants to merge

C#
private void button3_Click(object sender, EventArgs e)
{

    string[] source = textBox1.Text.Split(',');
    string[] source1 = textBox3.Text.Split(',');
    int[] nums2 = new int[8];
    int[] nums = new int[source.Length];
    for (int i = 0; i < source.Length; i++)
    {
        nums[i] = Convert.ToInt32(source[i]);
    }
    int[] nums1 = new int[source1.Length];
    for (int j = 0; j < source1.Length; j++)
    {
        nums1[j] = Convert.ToInt32(source1[j]);
    }
    int x = 0;
    int y = 0;
    int z = 0;
    while (x < nums.Length && y < nums1.Length)
    {
        if (nums[x] < nums1[y])
        {
            nums2[z] = nums[x];
            x++;
        }
        else
        {
            nums2[z] = nums1[y];
            y++;
        }
        z++;
    }////----->>it works untill here
    while (x > nums.Length)///this mean when the elements of nums end,out the rest of the elements in other textbox but it doesnt do anything,whats the problem ?
    {
        if (y <= nums1.Length)
        {
            nums2[z] = nums1[y];
            z++;
            y++;
        }
    }
    while (y > nums1.Length)
    {
        if (x <= nums.Length)
        {
            nums2[z] = nums[x];
            z++;
            x++;
        }
    }
        string merge = "";
        foreach (var n in nums2)
            merge += n.ToString() + ",";
        textBox4.Text = merge;

    }
Posted
Updated 18-Dec-10 5:27am
v2
Comments
Henry Minute 18-Dec-10 11:51am    
Yes replace both your while loops with the ones I posted. Don't just change the while statement, there are other changes there too.

FYI: I pasted the code snippet directly from a WORKING example based on your code. The only changes I made are the code I've given you.

It most definitely does not give an error.
arashmobileboy 18-Dec-10 11:54am    
sorry,but can you paste all of your code?thanks
arashmobileboy 18-Dec-10 12:13pm    
wow it worked thanks so much

1 solution

You have your logic wrong in the final two while loops.

try:
C#
while (x < nums.Length)
{
    nums2[z] = nums[x];
    z++;
    x++;
}
while (y < nums1.Length)
{
    nums2[z] = nums1[y];
    z++;
    y++;
}


BTW: Don't forget to make sure that your two input arrays are sorted before starting the merge.
 
Share this answer
 
Comments
arashmobileboy 18-Dec-10 11:47am    
you mean i put these codes instead of while (x > nums.Length) and while (y > nums1.Length)?it gives index out of range error

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