# Small Logic Issue

• 05-26-2006
Flakster
Small Logic Issue
Or atleast thats what I THINK the problem is. Here is the function I am having toubles with.

The idea is that two numbers are entered, and the code should check to see how many numbers in the array are within those two numbers.

The array was sorted from least to highest in a different function. I always end up with the program only returning '1' for the number of students with in the range... which leads me to believe that my logic in the for loops are incorrect.

Any ideas? Attached is the entire program.

Code:

```void RangeData(int *grades, int numStudents) {         int studentNumber;         int highRange = 0;         int lowRange = 0;         int lowRangeReturn = 0;         int highRangeReturn = 0;         int gradesInRange = 0;         char rangeAgain;         cout<<"\n\nEnter the low value for a grade range look up: ";         cin>>lowRange;         cout<<"Enter the high vaule for a grade range look up: ";         cin>>highRange;                 for (studentNumber = 0; lowRange == studentNumber; studentNumber++)         {                 if(lowRange == grades[studentNumber])                 {                         lowRangeReturn = grades[studentNumber];                 }         }//find low range number         for (studentNumber = 0; highRange == studentNumber; studentNumber++)         {                 if(highRange == grades[studentNumber])                 {                         highRangeReturn = grades[studentNumber];                 }         }//find high range number         gradesInRange = (highRangeReturn - lowRangeReturn)+1;         cout<<"\nThere are "<<gradesInRange<<" students within that range."<<endl;         cout<<"\nWould you like to look up another range? (y/n) ";         cin>>rangeAgain;         if(rangeAgain == 'y' || rangeAgain == 'Y')         {                        RangeData(grades, numStudents);         }         else if(rangeAgain == 'n' || rangeAgain == 'N')         {                 return;         } }//end RangeData```
• 05-26-2006
hk_mp5kpdw
Code:

```for (studentNumber = 0; studentNumber < numStudents; ++studentNumber) {     if(  lowRange < grades[studentNumber] && grades[studentNumber] < highRange ) ++gradesInRange; }```
That would get the number of grades between the low and high ranges (exclusive)... if you wanted to include the low and high points you would change the < to <=.

Code:

```void RangeData(int *grades, int numStudents) {     ...     cout<<"\nWould you like to look up another range? (y/n) ";     cin>>rangeAgain;     if(rangeAgain == 'y' || rangeAgain == 'Y')     {                RangeData(grades, numStudents);     }     else if(rangeAgain == 'n' || rangeAgain == 'N')     {         return;     } }//end RangeData```
I would suggest using another loop within the function to take the place of the recursion. Something similar to:
Code:

```void RangeData(int *grades, int numStudents) {     ...     do     {         cout<<"\n\nEnter the low value for a grade range look up: ";         cin>>lowRange;         cout<<"Enter the high vaule for a grade range look up: ";         cin>>highRange;                 ...         cout<<"\nWould you like to look up another range? (y/n) ";         cin>>rangeAgain;     } while( rangeAgain == 'Y' || rangeAgain == 'y' ); }//end RangeData```
• 05-26-2006
computation
Edit: i'm slow

Hint:
for (studentNumber = 0; highRange == studentNumber; studentNumber++)

highRange will only == studentNumber once
• 05-29-2006
Flakster
Code:

```for (studentNumber = 0; studentNumber < numStudents; ++studentNumber) {     if(  lowRange < grades[studentNumber] && grades[studentNumber] < highRange ) ++gradesInRange; }```
Using this still only gives me a result of 1 students in the range, even though there are more.

Edit: Nevermind, it does work, I just didn't implement it into my code properly. Thanks for all the help guys.
• 05-29-2006
hk_mp5kpdw
Quote:

Originally Posted by Flakster
Using this still only gives me a result of 1 students in the range, even though there are more.

What are the values in the grades array and what are your choices for lowRange and highRange?

Guess you figured it out... nevermind.[/edit]
• 05-29-2006
Flakster
=D Too slow! Thanks anyways =)