1

This is for the Traveling Sales program assigned to me in my Computing Foundations Class. One of the rules in the class is that are NOT allowed to use the STL in any way other than what our professor tells us for each individual assignment. In this case we are allowed to use string.

My current problem is creating a dynamic amount of cities/routes. My plan was to create a 13 by 13 array and then remove any elements that aren't used. In Java this is no big deal, but in C++ I am finding it hard to solve.(without using vector)

Is there a way (without using vector/anything in the STL) to remove any unfilled slots from an array? In other words decrease the size of the array until it reaches the first slot that has been taken up?

Another solution to this would be to create an array that is of a dynamic size, but google and cplusplus.com are telling me that you can't do that without using vector.

Example:

int matrix [13][13] = {};

for (int i = 0; i < 12; i++){
  matrix [i][i] = 4;
}

That would leave me with slots [12][13] and [13][13] ect.. empty how would I delete any slots that are empty?

Remixt
  • 597
  • 6
  • 28
  • 2
    You can't resize an array, that's a fact. What I am thinking on top of my head is that you can iterate the original array to see WHERE it ends, create a new dynamic array based on that size (or a vector), fill/copy the previous to the new, and continue to the next array, check, create, fill/copy, continue, and so on – Khalil Khalaf Mar 31 '16 at 23:44
  • Okay, thanks. I'll try that then. – Remixt Mar 31 '16 at 23:45
  • 5
    Define "unfilled slots". – Sam Varshavchik Mar 31 '16 at 23:48
  • See edit.@SamVarshavchik – Remixt Mar 31 '16 at 23:49
  • There's a reason everything's telling you to use `std::vector` from the Standard Library and that reason is it's the right thing to do. Why the opposition to it? – tadman Mar 31 '16 at 23:59
  • 1
    @tadman I'm not allowed to use the STL for any programming assignments. My professor says that using the STL is like letting someone else do the work for you. He said we need to really understand how C and C++ work. blah blah blah – Remixt Apr 01 '16 at 00:02
  • 1
    @ClaytonBrant If you're trying to understand the algorithms and re-implement the Standard Library so you understand it, sure, that's valid, but if you're teaching C++ and skipping that library that's just plain ignorant. It's like teaching C without letting anyone use `libc`. Hopefully this isn't encouraged in a professional environment, as people who re-write core libraries usually do an awful job. – tadman Apr 01 '16 at 00:05
  • 1
    He is right. I learned C++ the hard way as well. It helped me a lot because C++, at the end of the day, is more like micro programming instead of just programming – Khalil Khalaf Apr 01 '16 at 00:05
  • @ClaytonBrant That is fine, but your prof should have explained that an "unfilled slot in an array" is not even a thing in C++. Also, the vector we're talking about is in the one from C++ standard library, not the STL. – juanchopanza Apr 01 '16 at 00:07
  • @juanchopanza maybe he just meant that it's != 4, means "unfilled" because they didn't fill it.. still null or junk. I don't know, maybe he should ask him for the vector or/and this unfilled thing – Khalil Khalaf Apr 01 '16 at 00:08
  • @juanchopanza, well this class isn't exactly about teaching syntax. I'm a transfer student and my old school never taught me C++ so I'm coming at this problem as a Java programmer trying to write in C++. Java is so much easier when it comes to handling arrays and stuff, so this is quite a challenge for me. – Remixt Apr 01 '16 at 00:08
  • 1
    @ClaytonBrant Fair enough. Just note that the terminology is important when communicating with fellow programmers. – juanchopanza Apr 01 '16 at 00:09
  • 2
    C++ is also easy for arrays.... if you use the easy versions . – M.M Apr 01 '16 at 00:18

1 Answers1

2

Can you?

Yes, by using realloc().

Should you?

No, except if you really know what you are doing.


There are plenty of reasons for not doing what you want to do. You are using , thus an std::vector should be used instead. Moreover, realloc() may need to copy all the new array you are creating (but that's just for increasing the array's size).


how to have a dynamic amount of cities/routes?

Mark the empty slots of your array (or simple leave them empty), so that on the processing step, you simply ignore them.

Example:

Assume you want to find the sum of all the elements of the matrix, but ignore all the elements who are equal to 4. Then you could do this:

#include <iostream>

int main() {
        int matrix[13][13] = {0};
        for (int i = 0; i < 12; i++) {
                matrix[i][i] = 4;
        }
        // we want to sum all the elements
        // except the "emptyslots" (= 4)
        int sum = 0;
        for(int i = 0; i < 13; ++i)
                for(int j = 0; j < 13; ++j)
                        if(matrix[i][j] != 4)  // ignore empty slots!!
                                sum += matrix[i][j];
        std::cout << sum << std::endl;
        return 0;
}

Output:

gsamaras@gsamaras:~$ g++ -Wall px.cpp 
gsamaras@gsamaras:~$ ./a.out 
0        <-- as expected, since we initialized the matrix to 0

An alternative solution would be to use a simple linked list and delete the empty slots.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    I agree that using vector would be nice, but I'm not allowed to use anything in the STL other than string for this assignment. – Remixt Mar 31 '16 at 23:59
  • @ClaytonBrant What is the problem in the assignment? Post it. Maybe you got it rounded – Khalil Khalaf Apr 01 '16 at 00:02
  • @FirstStep It's implementing the traveling sales person problem using an adjacency matrix. – Remixt Apr 01 '16 at 00:03
  • @ClaytonBrant yes, that's a common thing for starters your professor is right. I think you got your mind tangled up. My answer, *answers* this question. However, I feel that you need to ask a new one, asking how to tackle the problem, since you seem a bit lost, in the approach. :) – gsamaras Apr 01 '16 at 00:05
  • hmm I don't feel lost in anything except how to have a dynamic amount of cities/routes. I guess I could ask the entire question, but I thought that was frowned upon. – Remixt Apr 01 '16 at 00:06
  • @gsamaras, I'm not exactly sure how to ignore elements in an Array. Could you provide an example? – Remixt Apr 01 '16 at 00:12
  • 1
    @ClaytonBrant sure, I updated my answer. If you have any questions, please let me know. – gsamaras Apr 01 '16 at 00:19
  • 1
    Can the downvoter **please** say what's wrong with my answer? :) – gsamaras Apr 01 '16 at 00:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107909/discussion-between-clayton-brant-and-gsamaras). – Remixt Apr 01 '16 at 00:36
  • @gsamaras I have another question can you join chat? – Remixt Apr 01 '16 at 00:44