10

I'm learning to program, and C++ is my first language. Don't bother using pointers to show me - I don't understand them yet, and won't bother until I have more free time to dedicate to this.

    int mergeSort()
{
    const int n = 9;
    int originalarray[n] = {1, 3, 5, 7, 9, 2, 4, 6, 8};


    const int halfelements = (sizeof(originalarray) / sizeof(int)) / 2;
    int farray[halfelements];
    int sarray[halfelements];

    for (int i = 0; i < halfelements; i++) {
        farray[i] = originalarray[i];
    }

    for (int i = halfelements, x = 0; i < (halfelements * 2); i++, x++) {
        sarray[x] = originalarray[i];
    }

I was assigned (I'm not taking classes - just learning with a few friends helping me out) a merge sort algorithm, with the algorithm explained but not the implementation. I want to rewrite this so it will work for both odd and even integers. I tried adding this code:

if ((n % 2) != 0) int farray[halfelements + 1];

So that I could use the same integer to iterate over both subsequent arrays. A sizeof(farray) is showing to be 16 bytes, or 4 integers. So it isn't resizing. What I want to know - is it possible to resize arrays after they initialized?

Edit: How would I implement a vector? I don't understand how to use iterators in a loop to iterate over and copy the values.

0

6 Answers 6

21

C++ arrays are fixed in size.

If you need a "resizable array", you'll want to use std::vector instead of an array.

Sign up to request clarification or add additional context in comments.

2 Comments

OK, thanks. I've figured out how you would implement std::vector's into this algorithm. Although I wish I hadn't spent two hours debugging my code, just to figure out my function header "int mergeSort(std::vector, int)" was missing an "<int>" =/
Aren't vectors backed by dynamic arrays anyways? Dynamically re-sizing an array or a vector should incur the same performance penalties right>
4

My advice is even stronger: use std::vector<> (et. al.) unless you have a very good reason to use a C-style array. Since you're learning C++, I doubt you have such a reason: use std::vector<>.

1 Comment

Given a vector is guarenteed to use contiguous storage, even when passing to a method taking pointer you can use a vector. Only when passing a reference/pointer to a pointer for a method to size the data are you stuck with using raw memory.
1

If you want to resize an array, you probably want to use a vector, which can be resized automatically.

Comments

1

You can use the [] operator with a vector the same way you would in an array. You could implement this with a vector something like this (if you wanted to use more vector methods):

#include <vector>

const int halfelements = originalarray.size()/2; //use size to get size
vector <int> farray(halfelements);
vector <int> farray(halfelements);

for (int i = 0; i < halfelements; i++) {
    farray.push_back(originalarray[i]); //adds element at i to the end of vector
}

for (int i = halfelements, x = 0; i < (halfelements * 2); i++, x++) {
    sarray.push_back(originalarray[i]);
}

You can also use .at(index) to add bounds checking to the vector access.

2 Comments

please don't use the "pre" HTML tag for code - instead, select the code with your mouse and type ctrl-K or click on the code icon
He should be using the vector(iter, iter) constructors. vector<int> farray(originalarray.begin(), &originalarray[half]), sarray(&originalarray[half], originalarray.end()); It eliminates the copy afterward. But that's probably confusing.
1

I would also recommend std::vector. However if you are stuck with an array you can always malloc the memory and then realloc if you need to make the array larger.

Do a search here on SO, there is information about malloc and realloc.

1 Comment

Me too use this convention.
0

If you want to know why your first idea compiled but didn't seem to work:

When you omit braces in an if-statement:

if ((n % 2) != 0) int farray[halfelements + 1];

it's just the same as if you'd used them:

if ((n % 2) != 0) {
  int farray[halfelements + 1];
}

So it is making an 'farray' of the correct size -- and then it immediately goes out of scope and is gone, and you're left with only the original one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.