-4

I'm trying to use a linear search function on a dynamic array I created but then the teaching assistant for my computer science class told us that most search functions use const arrays.

Is there any way I can edit my dynamic array to become contant? Or is it also possible to make a search function that uses a dynamic array (and would not give errors?). I'll give a brief insight into my code:

I dynamically create an array using the rows I read in from a file and then dynamically allocate each row to an array of columns.

char ** domain[] = new char * [rows];

for(int i = 0; i < rows; i++)
{
    *domain = new char[columns];  
    domain++;  
}

The type of function that we were taught for searching is:``

char searchArray( const char list[], char letter, int maxSize)
{
    >code goes here  
}

Is there any other method of using a search function that takes in dynamic multidimensional arrays?

In response to the comments, I can't use vectors. This is an assignment for us to use normal arrays. I haven't been taught how to to use vectors yet.

yivi
  • 42,438
  • 18
  • 116
  • 138
Kotlin Monk
  • 105
  • 5
  • 1
    Why not use a `std::vector` and `std::find()`? `char ** domain[] = new string * [rows];` won't work BTW. – πάντα ῥεῖ Oct 29 '18 at 18:59
  • @πάνταῥεῖ like i said before i haven't been taught all those complicated stuff, I'm in Cs 1337. vectors is Not covered in my class – Kotlin Monk Oct 29 '18 at 19:00
  • 1
    That's not complicated stuff but mere basic for c++. – πάντα ῥεῖ Oct 29 '18 at 19:01
  • @πάνταῥεῖ well you were once at my level, Not everyone starts c++ knowing everything, thats why i am here. Plus i dont know vectors yet. I'd prefer a normal approach – Kotlin Monk Oct 29 '18 at 19:02
  • Kevin, he's not kidding. `vector` is a LOT easier to use what what you're trying to do now because it hides all the nasty stuff you're trying to do now. – user4581301 Oct 29 '18 at 19:03
  • 1
    @KelvinOjiako The _normal approach_ in c++ is to use `std::vector` and not a raw array. – πάντα ῥεῖ Oct 29 '18 at 19:03
  • `char ** domain[]` (sort-of) says I want an array of `char **` which is kind-of a `char ***`. Lose the `[]`. – user4581301 Oct 29 '18 at 19:05
  • You're custom-building your array of pointers (to arrays of char). With that, you're going to have to custom-build a search algorithm for what you custom-built. There is no automatic canned algorithm for enumerating/searching your manufactured object. You can get pretty creative with he linear search algorithms from the standard lib, but you'll have to loop to splay that across your table. – WhozCraig Oct 29 '18 at 19:08
  • There is danger in `domain++`. Incrementing a pointer's a great way to advance the pointer to the next element in an array. I wholeheartedly approve... BUT all you have is `domain` and by incrementing it you lose the pointer to the start of the array making it difficult to use later. Make a temp variable `char ** temp = domain;` and increment `temp` so you don't lose the start in `domain`. – user4581301 Oct 29 '18 at 19:09
  • @4581301 thanks a lot, i can add that in. But in terms of the function, how can i edit it to take in a multidimensional array – Kotlin Monk Oct 29 '18 at 19:11
  • @WhozCraig we have been taught a sample search function and i already have the code but then the problem is that, the code is for constant arrays. I'm trying to see how i can edit the one i have been taught to work on my multidimensional array. – Kotlin Monk Oct 29 '18 at 19:21

1 Answers1

2

In the line

char ** domain[] = new char * [rows];

char ** domain[] tries to make an array of char **. If the compiler didn't complain about not having a valid array size in the [] and you would have a 3D structure. You want just plain old char ** for a 2D structure, so

char ** domain = new char * [rows];

The loop filling out the inner dimension is correct except it loses track of the starting point of domain

for(int i = 0; i < rows; i++)
{
    *domain = new char[columns];  
    domain++;  
} 

Should be something like

char ** temp = domain;

for(int i = 0; i < rows; i++)
{
    *temp = new char[columns];
    temp++;
}

To preserve the starting point, but for this case array notation is probably the smarter and easier-to-read option.

for(int i = 0; i < rows; i++)
{
    domain[i] = new char[columns];
}

On to searchArray. It needs to know it's getting two dimensions, (const char **) and that there are two max sizes (maxRow and maxColumn). It will look something like

char searchArray(const char ** list, 
                 char letter, 
                 int maxRow, 
                 int maxColumn)
{
    >code goes here  
}

Code goes here is your problem, but will probably be two nested for loops iterating to maxRow and maxColumn and returning when letter is found.

But... Why return a char? Returning the location in the array is much more useful. We could use std::pair, but if std::vector is off limits, pair probably is as well. Consider something like the following instead:

struct coord
{
    int row;
    int column;
};

coord searchArray(const char ** list, 
                  char letter, 
                  int maxRow, 
                  int maxColumn)
{
    coord location;
    >code goes here
    return location;
}

If the item is not found, set row and column to something impossible to get like -1 so you can easily test for the not found case.

Stop here unless you want to <expletive deleted> with your teacher's brain.

The above doesn't build a 2D array. You can't get a dynamically allocated 2D array in C++. What you have is an array of arrays. There are a couple downsides to this, look at all the work that goes into stitching one together and computers love it when things go in straight lines. Array of arrays doesn't. Every different allocation can be somewhere completely different in memory forcing the program to hop around, waiting on and loading different chunks of memory. Sometimes The program will spend more time sitting around waiting for stuff to be found and loaded than it'll spend doing the actual work. This sucks.

The solution is to make a 1D array and make it look like a 2D array. Here's an example of that from the C++ FAQ

You'll learn a lot of neat stuff from following this example, not the least of which being RAII and the Rule of Three, two concepts without which you cannot write non-trivial high quality C++ code.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thanks a lot, You pretty much fixed my mood. I have a quick question thou, wouldn't making a dynamic array to be constant affect the pointer and prevent it from moving along the array?, cause like constants can't change and wouldn't that also mean that i wouldn't be able to move around ? – Kotlin Monk Oct 29 '18 at 20:09
  • 1
    @KelvinOjiako the placement of `const` relative to the type and the `*`s changes what is constant. A discussion on that that should explain: [What is the difference between const int*, const int * const, and int const *?](https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const). Take the time to understand the Clockwise/Spiral Rule. It can save you a lot of trouble over time. In this case the data pointed at is constant, not the pointer to it. – user4581301 Oct 29 '18 at 20:33
  • Hate to risk ruining your mood again, but C++ is an extremely complicated language. There are maybe a few hundred people in the world who *don't* have a long way to go. I'm not one of them. Don't be ashamed if you finish your class and you still don't think you know much. There literally isn't enough time in one class. – user4581301 Oct 29 '18 at 20:43
  • Yea i totally agree with you on that, but then what matter is that I kinda love it tbh. Nah your good lol, Just some specific stuff trigger me – Kotlin Monk Oct 29 '18 at 20:48