# arrays question



## ste2425 (Feb 3, 2010)

i have a small function that contains a loops which plays a series of midi notes. I have different arrays contaiing the different midi notes for different the tunes. Now what i want to do is find a way have the user input which array to use and then just use the one function and one loop and change the array that used in the loop rather then just having a switch with ceach case containg the function but with different arrays for each tune. Hope this makes sense so far. Ive tried using strings where the user inputs the name of the array then putting that in the loop but doing that i get litteraly hundreds of errors im a bit stumped. There must be a way to do this otherwise my code is stupidly long using the same thing minus one variable being different. hope thatmakes sense


----------



## unibrow1990 (Feb 3, 2010)

What language are you using, it sounds like a hash table is what you need regardless.

It would be useful if you posted the code you are working with.


----------



## ste2425 (Feb 4, 2010)

c++


> for (int count = 0; count <=8; count ++)
> {
> sendMidiMessage(0x90, major[n], 0x7f);
> delay(d);
> ...


major is the array and n is a variable used to cycle through the cells in the array. and the n=cycle1(n); at the bottom is a function to send it back to cell zero when it reaches the end otherwise it would thro up an error. What i want to do is make it so that where the array MAJOR[N] is i put something that allows me to select an array out of a few. hope that helps
that whole bit of code is in a seperate function which i call from the main so it keeps my main clear as this may i have a few of them with the different arrays. But if i find a way to call it once allowing to put different arrays in i can but it back into main.


----------



## unibrow1990 (Feb 4, 2010)

Well I have never used hash tables in c++ though I'm sure it implements them somehow. since you only have a small data set you could get the same effect with an array of structs, something like:


```
struct Scale

{
    string key;
    scaleType scale[8];
};

Scale scaleArray[numOfScales];

// Add all of you scale structs to the array

// Have the user enter a string to select a scale and match the string they 
// enter against the key values in the struct.

// When the string they entered matches a key pass the associated scale to your function.
```

Something along those lines should work and get you what you are looking for I think.


----------



## ste2425 (Feb 4, 2010)

unibrow1990 said:


> Well I have never used hash tables in c++ though I'm sure it implements them somehow. since you only have a small data set you could get the same effect with an array of structs, something like:
> 
> 
> ```
> ...



ill give it a try thans for the help, im new to c++


----------



## Kreij (Feb 4, 2010)

You can just pass a pointer to the correct array into the loop function.


----------



## ste2425 (Feb 4, 2010)

Kreij said:


> You can just pass a pointer to the correct array into the loop function.



hi what do you mean by pointer? cheers

ive researched pointers and sort of have a basic idea but i dont fully understand them. I want it so the uder types the name of the array and a variable will input that name and then output it into the loop, i crnt seem to get the pointers to do that, is it even possible in c++? sorry to keep asking


----------



## unibrow1990 (Feb 5, 2010)

There is no way to have a user refer to a variable by name directly, variable names are something the user programmer puts into source code to make it more readable, they don't actually mean anything to the computer. To have the user directly reference a specific variable from a group you need to associate it with something else, such as an array index or a string in a struct or hash table like I showed above.


----------



## Kreij (Feb 5, 2010)

Unibrow is correct. However you implement the arrays, you will need some way of associating the user input to the correct array.

Let say that you create 3 arrays called MidiScaleC, D and E.

```
int MidiScaleC [];
int MidiScaleD [];
int MidiScaleE [];
```

The name of the array when used without an index to an element is the pointer to the array;

You can create the loop function to accept a pointer to the array as one of it's parameters.

```
void MidiLooper (int* ChosenArray)
{
    ... // perform task using the passed in array
}
```

There are several ways that you can associate the user input to the correct array.
Using a struct like Uni said is one way. Another is to create an array of pointers and put the MidiScale arrays into it

```
int* AllMidiScales [3] { MidiScaleC, MidiScaleD, MidiScaleE };
```

When the user selects the scale have it return the correct index for the array.

So lets say that if the user selects MidiScaleD, the selection method returns a 1 into a variable named "_Index" (since there are three selections at this point they would return 0, 1 or 2. Remember arrays are zero based)
Then use that as the index into the pointer array when you call the looper function.

```
MidiLooper (AllMidiScales[_Index]);
```

Here is a decent tutorial on C++ pointers and how to use them

You will want to learn pointer use very well. There are times when there is no other way to accomplish something without the use of pointers.

As I said there are many ways to accomplish things in code, so just pick the one you are most comfortable with, or even better, make it work in a variety of ways for the learning experience 

Forgive me if my C++ syntax is off a bit.


----------



## Clement (Feb 12, 2010)

Kreij said:


> Unibrow is correct. However you implement the arrays, you will need some way of associating the user input to the correct array.
> 
> Let say that you create 3 arrays called MidiScaleC, D and E.
> 
> ...



I second the notion of pointers.

Learn and use pointers. As your application grows, it will be simpler and save memory.

If you only had 64k of ram you wouldn't have a choice


----------

