C++ Help: Sorting with Arrays & Merging Strings

  • Context: C/C++ 
  • Thread starter Thread starter scumbum22
  • Start date Start date
  • Tags Tags
    Arrays C++
Click For Summary

Discussion Overview

The discussion revolves around programming in C++, specifically focusing on sorting algorithms using arrays and the merging of strings into arrays. Participants explore the implementation of a divide and conquer sorting approach and address issues related to array manipulation and memory management.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their attempt to sort an array of distances and seeks guidance on separating the array into subarrays based on a condition.
  • Another participant suggests creating new arrays dynamically using the new keyword but cautions against it due to increased complexity in development and memory management.
  • A different viewpoint proposes using STL's vector instead of arrays, highlighting the benefits of automatic memory management and focusing on algorithmic aspects.
  • A participant identifies several issues in the original code sample, including syntax errors in the for loop and suggests corrections to improve code clarity and prevent bugs.

Areas of Agreement / Disagreement

Participants present differing opinions on the best approach to handle arrays and memory management in C++. While some advocate for dynamic arrays, others recommend using STL vectors. There is no consensus on the preferred method.

Contextual Notes

Limitations include potential misunderstandings of memory management in C++, the need for clarity in code structure, and the varying levels of familiarity with STL among participants.

scumbum22
Messages
6
Reaction score
0
I'm working on programming my own sorting code in C++ using the divide and conquer algorithm. I'm a novice programmer and I've gotten a little stuck.

Let's say you begin your code with an array of distances,

dis[4] = {dis1, dis2, dis3, dis4}

Then I want to separate this array into subarrays. I have the following,

for (i = 0, i = 4, ++i);
if (dis <= dis[0]) { ...?
}
else { ... ? }

I want to somehow make two arrays: One made up of the elements less than or equal to dis[0] and another made up of the elements not following the condition. How should I do this?

Also, is there a way to merge a string of arrays into one array?
 
Technology news on Phys.org
You could create new arrays dynamically as you go, using the new keyword. I don't recommend doing this, though, because it will be harder to develop and debug, and will involve more bookkeeping and memory management code.

You should be able to implement your sorting algorithm using only swaps -- exchanging two elements of the array. This is called an "in-place" sort, because it does not require the allocation of any additional memory.

- Warren
 
If you have access to STL [1] another option is to use instances of stl::vector [2] instead of arrays. That will allow you to concentrate on the algorithmic aspect of your code and let (most of) the memory allocation be taken care of automatically. Of course, STL comes with its own set of idioms that may take a while to learn, but overall it is an improvement compared to coding dynamic arrays "manually".

[1] http://www.sgi.com/tech/stl/
[2] http://www.cplusplus.com/reference/stl/vector/
 
Note: I put [ code ] and [ /code ] tags (without the extra spaces) around your code.
scumbum22 said:
Let's say you begin your code with an array of distances,

dis[4] = {dis1, dis2, dis3, dis4}

Then I want to separate this array into subarrays. I have the following,
Code:
for (i = 0, i = 4, ++i);
    if (dis[i] <= dis[0]) { ...?
    }
        else { ... ? }
I see at least four problems with your code sample here.
1) The three expressions in parentheses for the for loop need to be separated by semicolons, not commas.
2) The middle expression should be i < 4.
3) There should not be a semicolon on the line with the for statement.
4) To avert hard to find bugs, you should put braces ({}) to mark the body of your for loop. IOW, it should look something like this:
Code:
for (i = 0; i < 4, ++i)
{
    if (dis[i] <= dis[0]) { ...? }
    else { ... ? }
}
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 17 ·
Replies
17
Views
4K
Replies
20
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
12
Views
2K