7 Replies - 710 Views - Last Post: 15 January 2011 - 10:13 AM Rate Topic: -----

#1 Azardokht  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 29-December 10

join 2 int arrays

Posted 14 January 2011 - 08:20 AM

hi there, I have 2 sorted int arrays and I wanna joint them in a way they remain sorted. here is a method in a class of my project, but it doesn't work:

public void Array_Set_Value()
        {
            int N=0;
            int [] finalarray = new int [Firstarray.Length + Secondarray.Length];
            for (int i = 0; i < Firstarray.Length; i++)
            { 
                for (int j=0; j<Secondarray.Length; j++)
                {
                    if (Firstarray[i]< Secondarray[j])
                    {
                        N=i+1;
                        Firstarray.CopyTo(finalarray, 0);
                        finalarray.SetValue(Secondarray[i], N);
                    }
                }
            }
        }


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 14 January 2011 - 08:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: join 2 int arrays

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon




Reputation: 1325
  • View blog
  • Posts: 3,504
  • Joined: 03-May 10

Re: join 2 int arrays

Posted 14 January 2011 - 08:25 AM

How exactly is to "not working". Are you getting any errors, or are the results just not what you'd expect?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4880
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

Re: join 2 int arrays

Posted 14 January 2011 - 08:30 AM

// fine, this makes sense
int [] finalarray = new int [Firstarray.Length + Secondarray.Length];
for (int i = 0; i < Firstarray.Length; i++) { 
	for (int j=0; j<Secondarray.Length; j++) {
	// you do understand that the code in here will get executed
	// Firstarray.Length * Secondarray.Length times?



You want something more like:
int pos=0, i=0, j=0;
while (i < Firstarray.Length && j<Secondarray.Length) {
	// find the smaller
	// add it to the list and inc i OR j
	pos++;
}
// add the left over from the array you didn't reach the end of


Was This Post Helpful? 1
  • +
  • -

#4 tinase  Icon User is offline

  • New D.I.C Head

Reputation: -16
  • View blog
  • Posts: 33
  • Joined: 19-June 08

Re: join 2 int arrays

Posted 14 January 2011 - 08:42 AM

I am not experienced in C++, If you post the whole code with all the declaration and sorting codes then I will be able to help you right now
Was This Post Helpful? -1
  • +
  • -

#5 Mitja Bonca  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 82
  • Joined: 07-May 09

Re: join 2 int arrays

Posted 14 January 2011 - 08:49 AM

Check out this code how to join arrays into one and sorting it:
        private void method()
        {
            int[] array1 = new int[] { 4, 3, 8, 12, 15, 6 };
            int[] array2 = new int[] { 5, 7, 2, 13, 9, 1 };
            //joining them:
            int[] array3 = new int[array1.Length + array2.Length];
            array1.CopyTo(array3, 0);
            array2.CopyTo(array3, array1.Length);

            //sortinng array3:
            Array.Sort(array3);
        }


Was This Post Helpful? 1
  • +
  • -

#6 suckedforu  Icon User is offline

  • New D.I.C Head

Reputation: -13
  • View blog
  • Posts: 8
  • Joined: 15-January 11

Re: join 2 int arrays

Posted 15 January 2011 - 09:38 AM

View Posttinase, on 14 January 2011 - 08:42 AM, said:

I am not experienced in C++, If you post the whole code with all the declaration and sorting codes then I will be able to help you right now

you are very helpfull you deserve a +
Was This Post Helpful? -1
  • +
  • -

#7 skyhawk133  Icon User is offline

  • Head DIC Head
  • member icon

Reputation: 1812
  • View blog
  • Posts: 20,232
  • Joined: 17-March 01

Re: join 2 int arrays

Posted 15 January 2011 - 09:41 AM

View Postsuckedforu, on 15 January 2011 - 09:38 AM, said:

View Posttinase, on 14 January 2011 - 08:42 AM, said:

I am not experienced in C++, If you post the whole code with all the declaration and sorting codes then I will be able to help you right now

you are very helpfull you deserve a +


Hey dumb ass... you're all posting from the same IP, don't be fucking stupid up-voting each other. I'll move you to the rep abuse group if you continue to do it. NO REP FOR YOU!!
Was This Post Helpful? 2
  • +
  • -

#8 Mitja Bonca  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 82
  • Joined: 07-May 09

Re: join 2 int arrays

Posted 15 January 2011 - 10:13 AM

LOL... what ppl dont invent...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1