| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi, i wonder if you can help me with a problem i am having with c++.
I was set this question: Write a program that promps the user to enter 10 midi notes. These values should be stored in an array. MIDI notes cannot be less than 0 or greater than 127, ensure the user does not enter an invalid note number. The program should output the highest and lowest midi notes contained within the array. I have completed the question as far as entering an invalid number, but i dont know how to get the program to output the highest and lowest midi notes. I guess i can create a new variable and copy the value in the first array element to this. Then use a loop to compare all of the remaining array elements, beginning at subscript 1, to the value in highest. But im not sure how to do this! PLEASE HELP! this is the code i have written: Code:
#include <iostream.h>
#include <math.h>
float midi_note[10];
int index=0;
void main (void)
{
|
|
#2
|
|||
|
|||
|
Every thing you need to know to do this, you've already done in your program so far. Initialize your variable to first element of the array just as you said. Loop through each of the elements in the array, just as you did here:
Code:
for (index=0; index<10; index++) {
}
but start from 1, not 0. Each time through the loop, compare the value of the array at postion "index" to your maximum value so far (stored in the variable mentioned above). To access the element at position "index" you do just as you did here: Code:
midi_note[index] if the value at midi_note[index] is greater than the maximum so far, just set the variable containing max so far to the value stored in midi_note[index]. eg. Code:
midi_note[index] = maxVal; Have another variable for the minimum note as well, and you can check both in the same loop. You have to initialize this variable to something greater than 127, since this is the maximum note value you could have. If you initialized it to anything less, say 50, any note greater than 50 would not get considered for the minimum value, even if all notes were above 50. Hope this made things a little clearer. Kernel Mustard |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with c++ array problem please! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|