C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old November 20th, 2004, 07:57 PM
Parkin_m Parkin_m is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 10 Parkin_m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 58 sec
Reputation Power: 0
Unhappy Help with c++ array problem please!

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)


{
for (index=0; index<10; index++) {
cout<<"please enter your midi note. The number must be between 0 and 127 "<<endl; cin>>midi_note[index];
if (midi_note[index]<0 || midi_note[index]>127) {
cout<<"The midi note value entered is invalid. "<<endl; index--;
}
}
}

Reply With Quote
  #2  
Old November 21st, 2004, 01:06 PM
Kernel Mustard Kernel Mustard is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 32 Kernel Mustard User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with c++ array problem please!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT