C++ Help: Sorting with Arrays & Merging Strings

  • C/C++
  • Thread starter scumbum22
  • Start date
  • Tags
    Arrays C++
In summary, the code does not properly handle cases where elements of the array are not strictly in decreasing order.
  • #1
scumbum22
6
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
  • #2
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
 
  • #3
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/
 
  • #4
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 { ... ? }
}
 
  • #5


I am not an expert in programming, but I can offer some guidance and suggestions based on my knowledge of algorithms and data structures.

Firstly, it looks like you are on the right track with using the divide and conquer approach for sorting. This is a commonly used algorithm for sorting and can be very efficient.

To address your first question, you can create two new arrays, let's call them subarray1 and subarray2, and use a for loop to iterate through the original array and add elements to the appropriate subarray based on the condition you have set. For example:

int subarray1[4], subarray2[4];
int index1 = 0, index2 = 0;

for (int i = 0; i < 4; i++) {
if (dis <= dis[0]) {
subarray1[index1] = dis;
index1++;
} else {
subarray2[index2] = dis;
index2++;
}
}

This will create two subarrays - subarray1 with elements less than or equal to dis[0] and subarray2 with the remaining elements.

As for merging strings, you can use the string class in C++ to store and manipulate strings. To merge multiple strings into one, you can use the "+" operator or the append() function. For example:

string str1 = "Hello ";
string str2 = "world!";
string str3 = str1 + str2; // str3 will now contain "Hello world!"
// or
str1.append(str2); // str1 will now contain "Hello world!"

I hope this helps you with your sorting code. Keep practicing and experimenting, and don't be afraid to ask for help when you get stuck. Good luck!
 

Related to C++ Help: Sorting with Arrays & Merging Strings

What is C++?

C++ is a programming language that was developed in the 1980s as an extension of the C programming language. It is a high-level language that is used to create a variety of programs, including operating systems, video games, and applications.

How do I sort arrays in C++?

To sort arrays in C++, you can use the sort function from the library. This function takes in two parameters: the beginning and ending index of the array. You can also provide a third parameter which is a comparison function to specify the order in which you want the elements to be sorted.

Can I merge strings in C++?

Yes, you can merge strings in C++ using the append function from the string library. This function takes in a string as a parameter and appends it to the end of the original string. You can also use the += operator to merge strings.

What is the difference between sorting and merging in C++?

Sorting in C++ refers to arranging elements in a specific order, usually from lowest to highest or vice versa. This is done using a comparison function to determine the order. Merging, on the other hand, refers to combining two or more strings or arrays into a single string or array. It does not involve any comparison or reordering of elements.

Can I use both sorting and merging in the same program?

Yes, you can use both sorting and merging in the same program. For example, you can sort an array of strings using the sort function and then merge them into a single string using the append function. However, it is important to note that sorting and merging are two separate operations and should be used accordingly.

Similar threads

  • Programming and Computer Science
Replies
6
Views
830
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
Replies
13
Views
3K
Back
Top