Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,110 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,723 people online right now. Registration is fast and FREE... Join Now!




Boolean Values

 
Reply to this topicStart new topic

Boolean Values

hollsmeags
10 Apr, 2007 - 07:20 AM
Post #1

D.I.C Head
**

Joined: 29 Mar, 2007
Posts: 87


My Contributions
Okay after burning midnight and daytime oil I finally finished my code. It's working like a charm.
I just have a question though.
How does the boolean work?

Meaning, the results show if ANY of the numbers were within a range ..either
< 20
and between 10-90.
Where is this kept track of?? Everytime it loops and the answer is either true or false, is there a memory bank in which all the iterations are stored to tell the user that a certain amount of numbers are in a range?? After looking at it, I would just think that the LAST input would show up as in a range UNLESS bool works or something else works as a memory storage to to keep track of each iteration and of how many are in the range...
Can anyone answer this for me??
Ill post my code if this explanation makes NO sense......:)
Thanks!!1



CODE

namespace std;


int main(int argc, char *argv[])
{
    int x;
    int sum =0;
    int test_count = 1;
    double average;
    int smallest= INT_MAX;
    int largest= INT_MIN;
    bool less20 = false;
    bool range = false;
    
    
    
    cout << " Enter your numbers: then 9999 to stop.\n";
    cin >> x;
    
    while (x != 9999)
   {
      if (x < 20)
        less20 = true;                              // Does this line act as a memory bank to keep track of how many are < 20.
      if ( x > 10 && x < 90)
      range = true;
      sum += x;
      if ( x < smallest )
      smallest = x;
      if ( x > largest)
       largest = x;
        cout << "\n";
        test_count++;
      cin >> x;
                          }
                                  
        cout << "\nThe total is:  " <<sum << endl;
        cout<< "\nNumber of integers:    "<< test_count -1<< endl;
        cout<< "\n";
        average = 1.0 * sum/ test_count;
        cout<< "Average is:   "  << average << "\n";
        cout<< "\n";
        cout<< "The smallest number is:  " << smallest<< "\n";
        cout<<"\n";
        cout<< "The largest number is:  " << largest<< "\n";
        cout<<"\n";
        cout<< "Numbers less than  20? ";
          if (less20)
           cout<< "true\n";
           else
            cout<< "false\n";
        cout<< " Are all numbers within the range 10-90 ";
          if (range)
           cout<<"true\n";
          else
           cout<< "false\n";
    
        
      
        
    system("PAUSE");
      return 0;                          
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Boolean Values
10 Apr, 2007 - 07:25 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
Boolean values work as you expect...they are true or false. In the code provided, you are quite correct- nothing is keeping track of the boolean value for each iteration. If you want to know how many numbers tested true, and how many tested false, you would have to declare a variable to be incremented each time the condition occurs, and then report it at the end. Technically, you need only one counter variable, as the other can be extrapolated by subtracting the first counter from the total number of entries.
User is offlineProfile CardPM
+Quote Post

hollsmeags
RE: Boolean Values
10 Apr, 2007 - 02:52 PM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2007
Posts: 87


My Contributions
Oh okay
so if a user enters 3, 40 ,15
The way it is set up now is that since 3 and 15 are < 20, it will go to true.
It isnt keeping track. Just one has to be true for that statement to be true.
How would I do the counter like you are saying/
Its not part of my problem but I would like to know for my own knowledge.
Thanks!
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Boolean Values
11 Apr, 2007 - 04:22 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
CODE

int less20counter = 0;
int rangecounter = 0;
//everytime you set the  less than 20 flag to true, increment
less20counter++;
//same for the other
rangecounter++;
//when the loop is done, each of those counter contains the number of true //values. the number of false values is
(test_count-1)-(less20counter+rangecounter)


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month