Assigning Variables Within an Array

Hi guys, just wondering if its possible, instead of assigning values to an array, can you let the user define them. as in:

int userinput
int MajorInterval[8] {userinput, userinput ++ 2, etc etc}

then also, is it possible to index these values within a sendMidiMessage() function? as in:

sendMidiMessage (0x90, userinput, 0x7f)
delay 96
sendMidiMessage (0x80, userinput, 0x7f)

cheers all =]
Read in the user input first, then assign the values of the array based
upon the user's input.
well its for a scale player, where the user has to be able to define the starting note, so the values within the array cant be input as numbers.

i was thinking about using a loop, but the interval between notes is different each time, so i don't think a loop would work.

cheers
well you can create a function depending on which scale you play.
I don't remember the scales, but let's assume there are several leaps.

int major(scale[], input)
{ // there are only 8 notes to play.. right?
input = num1note
num2note = num1note+halfinterval //basic half a step, you can then multiply it to
get the interval you want, *2 would get you a full
note, etc.
num3note = num2note+ etc... you get the picture
}

the thing is you would need a new function for every scale, because the scales don't follow any given rule, and there are dozens of them.

never used midi so i have no idea about that, but i hope the rest helps..
thanks very much, this does help a fair bit actually =]

i have to do a chromatic scale as well, but i think i should be able to use a loop for that. what do you think?
Loops work great if you have a repeating scale, or a scale with growing intervals, or a scale with a given rule

for example, this is a melodic minor scale, decending, so you start with the highest note.
for convinience reason i'll use "8" as the biggest note.

so if you had a function for example
1
2
3
4
5
6
7
8
9
10
11
12
13
int scale[8] // just a decleration to be clear it exists

melodic(scale[],note)  //creating the function
{int i; // counter for the scales
 int note; // a way to reduce the note.
  
scale[0]=8;
for(i=1;i<=7;i++)
         {if(i==3 || i==6)   //in this scale, the fourth and 7th notes are after half a step
                {scale[i] = note-0.5}
          else
          {scale[i] = note-1};
}

this SHOULD organize your scale, starting from the top note, to the bottom, on a minor melodic scale.
notice that it's quite simple to add more conditions how to skip tones or semi-tones, but you cannot have a "one size fits all" solution for building scales.

at least in my opinion.
Hope it helps.
it does, but i've ended up building it a different way, im just up to the looping part now and i have a problem:

int play = 0;
while(play <= TimesPlayed)
{
for(int i = MajorIntervalAscend[0]; i<= 7; i++)
{
sendMidiMessage(0x90, MajorIntervalAscend[0], 0x7f);
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[0], 0x7f);
}
play++;

}

i know for the for loop to work, it needs to be MyIntervalAscend[i], but when i do that, it gives me an error saying that 'i' is being used without being initialized.
any thoughts?
cheers
well two problems.
you compare play to TimesPlayed but TimesPlayed has no value.
I have an important question, What values do you want to store, and Where do you want to store them?

i'm assuming i is your counter for the notes and majorintervalascend[i] is the note itself;

i think the problem is that majorintervalascend[0] has no value in it, and even if it did, you're giving your COUNTER a VALUE.

unless your "MajorIntervalAscend[0] has the value of "0", this will not work, as you will quickly pass the 8 note limit.
assuming it's 0,
i=0, then i increases after the playm which is okay.
but if i=15, or i=0x534, or i=5, you will get a diffrent amount of notes. clearly not the desired result.
if it is 0, why not just initialize it with for(i=0; i<=7;i++)?

think of it this way :
1
2
3
4
5
6
7
8
9
10
11
12
13
int play = 0;
while(play <= TimesPlayed)  //TimesPlayed isn't declared
{
for(int i = MajorIntervalAscend[0]; i<= 7; i++) // i==MajorIntervalAscend[0], But what value?
{
sendMidiMessage(0x90, MajorIntervalAscend[i], 0x7f); //if i is bigger than the max value of the array - problem.
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[i], 0x7f); //if i is bigger than the max value of the array - problem.

}
play++;

} 


how about this

1
2
3
4
5
6
7
8
9
10
11
12
13
int play = 0;
int note=0;
while(play <= TimesPlayed)  //TimesPlayed isn't declared
{
for(int i = 0; i<= 7; i++) // i==MajorIntervalAscend[0], But what value?
{MajorIntervalAscend[i] = note + i; // this increases the interval of the note.
sendMidiMessage(0x90, MajorIntervalAscend[i], 0x7f); 
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[i], 0x7f); 
}
play++;

} 


the main problem is that "i" can take many diffrent values in the opening initialization, unless you force it 0, and if you force it 0, why not just put a 0 there in the first place?
hope it helps
Last edited on
thanks for the help,

however, TimesPlayed is a user defined value that is assigned a value earlier in the code
my full code is posted in another thread here http://www.cplusplus.com/forum/beginner/19373/ so if you're not totally fed up of helping me you could have a look, which would be much appreciated.

thanks
First of all no one is fed up of anything, if i wanted i would just stop posting.
People helped me here, so I'm helping others if i can here..
anyhow, it's just like i said..

1
2
3
4
5
cout << "Which note would you like the scale to start on? (60 = Middle C)" << endl;
	cin >> MajStartingNote;
	 
	//Assign Array Values
	MajorIntervalAscend[0] = MajStartingNote ;

this is from your code in the URL you gave me.
notice how "majorintervalascend[0] can without problem be worth 60.(middle c), let's assume we picked 60.

Now let's look at the loop..
1
2
3
4
5
6
7
8
9
10
11
12
int play = 0;
while(play <= TimesPlayed)
{
for(int i = MajorIntervalAscend[0]; i<= 7; i++) // we're saying that i=60 here
{
sendMidiMessage(0x90, MajorIntervalAscend[i], 0x7f); //array[60] doesn't exist.
delay(userDelay);
sendMidiMessage(0x80, MajorIntervalAscend[i], 0x7f);//array[60] doesn't exist.
}
play++;

} 


as i said, you cannot have your counter be worth the actual VALUE inside the array. If i want to have an "age" array, with a counter i, doing i=age[0] will only mean that i is worth what ever is placed in the first position of the array. could be ANY number i want, and most likely will not be 0.

If this doesn't answer your question, then please define your question specificly.
For your original question of why the compiler said you didn't initialize i although you did, i have no answer, but it probably has something to do with the mistakes in syntax in your code.

as usual, hope this helps.
Thanks, its good to know that people are willing to help.

and finally i've got it working, turns out everything was right, but for some reason my compiler didn't like the fact that i was calling on 'i' twice in that for loop so i moved a few bits around until it behaved as it was supposed to.

All sorted now, but thanks to everybody that replied and tried to help.
Topic archived. No new replies allowed.