• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

arrays question

Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
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

New Member
Joined
Mar 19, 2009
Messages
194 (0.04/day)
Location
Detroit, Michigan
System Name FUBAR
Processor AMD Phenom II 720BE @ 3.6Ghz
Motherboard Jetway HA06, 780g sb700
Cooling Sunbeamtech Core Contact Freezer, 2x 40mm fan 1x 70mm hdd fan, 2x 80mm case fans, 4x 120mm case fans
Memory 4gb OCZ Reaper ddr2 800@ 4-4-4-12-1t 2.2v
Video Card(s) HIS 4850 ICEQ4 512mb crossfired with XFX 4850 512mb
Storage WDC Black 640gb SATA, 250gb Hitachi Deskstar SATA
Display(s) Acer X193W+ 19" 5ms 1680x1050
Case Modded Raidmax Smilodon Extreme Black
Audio Device(s) Integrated W/ Altec Lansing 2.1 speakers
Power Supply Rosewill RP550V2-D-SL 550W with FSP Booster X3
Software Win 7 Home Premium 64-bit
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.
 
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
c++
for (int count = 0; count <=8; count ++)
{
sendMidiMessage(0x90, major[n], 0x7f);
delay(d);
sendMidiMessage(0x80, major[n], 0x7f);
n ++;
n=cycle1(n);
}
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.
 
Last edited:

unibrow1990

New Member
Joined
Mar 19, 2009
Messages
194 (0.04/day)
Location
Detroit, Michigan
System Name FUBAR
Processor AMD Phenom II 720BE @ 3.6Ghz
Motherboard Jetway HA06, 780g sb700
Cooling Sunbeamtech Core Contact Freezer, 2x 40mm fan 1x 70mm hdd fan, 2x 80mm case fans, 4x 120mm case fans
Memory 4gb OCZ Reaper ddr2 800@ 4-4-4-12-1t 2.2v
Video Card(s) HIS 4850 ICEQ4 512mb crossfired with XFX 4850 512mb
Storage WDC Black 640gb SATA, 250gb Hitachi Deskstar SATA
Display(s) Acer X193W+ 19" 5ms 1680x1050
Case Modded Raidmax Smilodon Extreme Black
Audio Device(s) Integrated W/ Altec Lansing 2.1 speakers
Power Supply Rosewill RP550V2-D-SL 550W with FSP Booster X3
Software Win 7 Home Premium 64-bit
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:

Code:
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.
 
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
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:

Code:
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.

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

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
You can just pass a pointer to the correct array into the loop function.
 
Last edited:
Joined
May 27, 2008
Messages
3,628 (0.62/day)
System Name Ultra 64
Processor NEC VR4300 (MIPS R4300i)
Motherboard proprietary design
Cooling Fanless aircooled
Memory 4.5MB 250 MHz RDRAM
Video Card(s) 62.5 MHz Reality Coprocessor
Storage 32 - 512 Mbit ROM Cartridge
Display(s) 720x576
Case Clear Blue Funtastic
Audio Device(s) 16-bit CD quality
Power Supply proprietary design
Mouse N64 mouse for use with N64DD
Keyboard N64 keyboard for use with N64DD
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
 
Last edited:

unibrow1990

New Member
Joined
Mar 19, 2009
Messages
194 (0.04/day)
Location
Detroit, Michigan
System Name FUBAR
Processor AMD Phenom II 720BE @ 3.6Ghz
Motherboard Jetway HA06, 780g sb700
Cooling Sunbeamtech Core Contact Freezer, 2x 40mm fan 1x 70mm hdd fan, 2x 80mm case fans, 4x 120mm case fans
Memory 4gb OCZ Reaper ddr2 800@ 4-4-4-12-1t 2.2v
Video Card(s) HIS 4850 ICEQ4 512mb crossfired with XFX 4850 512mb
Storage WDC Black 640gb SATA, 250gb Hitachi Deskstar SATA
Display(s) Acer X193W+ 19" 5ms 1680x1050
Case Modded Raidmax Smilodon Extreme Black
Audio Device(s) Integrated W/ Altec Lansing 2.1 speakers
Power Supply Rosewill RP550V2-D-SL 550W with FSP Booster X3
Software Win 7 Home Premium 64-bit
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

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.20/day)
Location
Cheeseland (Wisconsin, USA)
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.
Code:
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.
Code:
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
Code:
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.
Code:
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 :toast:

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

Clement

New Member
Joined
Feb 11, 2010
Messages
192 (0.04/day)
Location
SouthEast Blue Mountain, Pa
System Name Work Horse
Processor E5200@3.000@1.2 (12.5x240) (Limited by mobo mem volts) ::[Evil Glare]-I'm gonna mod you!::
Motherboard 4COREDUAL-SATA2 R2
Cooling AC Freezer Pro 7; AC Accelero S1 <-These two are bad ass!
Memory (2gb G.Skill F2-8500-CL5D-2GBPK) @ 1.9v/320MHz/3:4/4-4-4-10-16-2T
Video Card(s) X1950GT 621/729 (Will do more)
Storage Seagate 320; 2x1TB WD
Display(s) ViewSonic 19" Widescreen
Case Black, Sturdy.
Audio Device(s) Onboard DAC works great.
Power Supply Enermax EG365P-VE
Software Fedora x64; WinXP32
Benchmark Scores Once, I programmed for a little over 22.5 hours straight.
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.
Code:
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.
Code:
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
Code:
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.
Code:
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 :toast:

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

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 :eek:
 
Top