开发者

C++ Adding from one array to another, look through the other array everytime I add an element to it

HI everyone

I would like to add an element one at a time from another array to a temporary array ive made. Every time I add an element to the temporary array, I 开发者_StackOverflowwould like to look through what I have already added to it.

I am doing this for sorting purposes and I am very new to this.

I know I have to use a for loop

int numbers[6]; //main array
int tempArr[6]; //temporary array I would like to add elements to

for(int i = 0; i < 6; i++)
{
//if temp element just added is >, < or = any element in temp array
//do stuff
}

the elements in the numbers array come from a file so by this point they are already in the numbers array. I just need to pass each value one at a time to the temp array so I can look inside the temp array for lower, higher or equal values

Hope you can help, ive looked everywhere and nothing seems to match what I need :(


It seems to me you should really think about Insertion Sort algorithm. Every time you need to add a new value just put it into the end your current temp array and push the value down the array until you'll find it's place and knowing the current index for this item you'll be able to find all elements < or > than this one.

This algorithms has O(n^2) complexity in the average case. You could achieve O(n log n) using binary trees.


I think you need an std::set:

std::set< int > temp;
for (int i(0); i < (sizeof(numbers) / sizeof(numbers[0])); ++i)
{
    temp.insert(numbers[i]);
}

The set will sort and eliminate duplicates efficiently


There're two things you're missing:

  • A way to know how many elements are in an array. (C++ arrays are fixed-size (6 for both in your case) but presumably tempArr starts out empty.)
  • A way to insert an element into an array (which you'll be doing to tempArr).

.

//if temp element just added is >, < or = any element in temp array

Any element will be either >, < or = to any other element, always. (Well, except when there are some funky operator overloads.)

Elaborate on your pseudo-code. And maybe add a function call or two.


You could do this very easily with another loop. If you are sure you will always have 6 items in your file (which is what you currently have based on the size of your arrays above) then you can do something like this:

for (i = 0; i < 6; i++)
{
   numbers[i] = temp[i];

   for (j = 0; j < count; j++)
   {
      if (temp[i] > temp[j])
         // Do something
      if (temp[i] < temp[j])
         // Do something else
      if (temp[i] == temp[j])
         // Do something else
   }
   count++;
}

Additional things will need to be adjusted if the number of input elements is variable, but otherwise this will, one by one, move each element in "numbers" to the "temp" array, and on each move, it will look through the entire list of other items and check if the new value is less than, greater than, or equal to the one just added.

EDIT: If you are trying to sort the items in the tempArray, then look up an insertion sort as others have suggested. If you just want to get the elements in the numberArray in ascending (or descending) order, you can look into bubble sort which is likely easier for a new programmer to handle, since it doesn't require a second array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜