Small Logic Issue

This is a discussion on Small Logic Issue within the C++ Programming forums, part of the General Programming Boards category; Or atleast thats what I THINK the problem is. Here is the function I am having toubles with. The idea ...

  1. #1
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50

    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
    Attached Files Attached Files
    Currently using Visual C++ 2005 Express Edition, 'cause it was free!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35
    Edit: i'm slow

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

    highRange will only == studentNumber once

  4. #4
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    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.
    Last edited by Flakster; 05-29-2006 at 02:36 PM. Reason: Pure Noobery
    Currently using Visual C++ 2005 Express Edition, 'cause it was free!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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?

    [edit]Guess you figured it out... nevermind.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    #include <me!> Flakster's Avatar
    Join Date
    May 2005
    Location
    Canada
    Posts
    50
    =D Too slow! Thanks anyways =)
    Currently using Visual C++ 2005 Express Edition, 'cause it was free!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  2. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 08:32 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21