Thread: Flag-Controlled Loops

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    Flag-Controlled Loops

    Hi, I'm new to the board and to C++!

    I am trying to design a program that will basically read in 8 numbers and then indicate whether any of them were larger than 50 or smaller than 10.

    I need to write the program so that it reads in 8 numbers and echoes them to the screen using a loop.
    Then I need to add two flags, one to detect if the numbers are larger than 50 and one to detect if the numbers are larger than 10.

    I've been trying to understand loops for about 3 hours and I'm having an extremely hard time grasping it.

    Any suggestions would be greatly appreciated!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, try writing some code as best you can. If it doesn't work -- compiles with errors, or behaves unexpectedly, etc, -- just post the code with an explanation and someone will help you with it.

    Here's a loop with a flag:
    Code:
    int i;
    bool flag = false;
    for(i=0;i<10;i++) {
         if (i == 666) flag = true;
    }
    if (flag) cout << "Inconceivable!";
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Ok, so this is pretty much what I want to do. However, I think I'm defeating the purpose of a loop.

    I can't grasp the semantics of reiteration. But here's the general idea.

    Also, I noticed I was a bit lazy with the indentation so forgive me!

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
    
    bool greater = true;
    
    int one, two, three, four, five, six, seven, eight;
    
    int num1, num2, num3, num4, num5, num6, num7, num8; 
    
    one = 0;
    two = 0;
    three = 0;
    four = 0;
    five = 0; 
    six = 0; 
    seven = 0;
    eight = 0;
    
    cout <<"Please enter eight numbers." << endl; 
    cin >> num1 >> num2 >> num3 >> num4 >> num5 >> num6 >> num7 >> num8;
    
    
    while (greater)
    {
    	if (num1 > 50) 
    		one ++;
    	else 
    		greater = false;
    
    if (num2 > 50)
    two ++;
    else 
    greater = false; 
    
    if (num3 >50)
    three ++;
    else greater = false; 
    
    if (num4 >50)
    four ++;
    else greater = false; 
    
    if (num5 >50)
    five ++;
    else greater = false; 
    
    if (num6 >50)
    six ++;
    else greater = false; 
    
    if (num7 > 50 )
    seven ++;
    else greater = false;
    
    if (num8 > 50 )
    eight ++;
    else greater = false;
    
    } 
    
    if (one == 1)
    cout << "Number 1 is greater than 50!" << endl;
    
    if (two == 1)
    cout << "Number 2 is greater than 50!" << endl; 
    
    if (three == 1)
    cout << "Number 3 is greater than 50!" << endl;
    
    if (four == 1)
    cout << "Number 4 is greater than 50!" << endl; 
    
    if (five == 1)
    cout << "Number 5 is greater than 50!" << endl;
    
    if (six == 1)
    cout << "Number 6 is greater than 50!" << endl; 
    
    if (seven == 1)
    cout << "Number 7 is greater than 50!" << endl;
    
    if (eight == 1)
    cout << "Number 8 is greater than 50!" << endl; 
    
    
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Yeah, good indentation would be nice. . .

    Here you want to use an arrays instead of variables that include a number in their name.

    Instead of int num1, num2 ect, use int num[8].

    Then you can do the same operation for each index.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Ok, I did what you said. Although I still feel as if I am defeating the purpose of looping.

    What can I do to make this loop work and shorten the amount of code? Again I'm trying to create a flag controlled loop.

    Thanks.


    Code:
    #include <iostream>
    
    using namespace std; 
    
    int main ()
    {
    
    
    bool greater = true;
    int num[8];
    int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0;
    
    cout <<"Please enter three numbers." << endl; 
    cin >> num [0] >> num[1] >> num[2] >> num[3] >> num[4] >> num[5] >> num[6] >> num [7];
     
    while (greater)
      {
    	if (num[0] > 50) 
    		one ++;
    			else 
    				greater = false;
    
    	if (num[1] > 50)
    		two ++;
    		
    				else 
    					greater = false; 
    
    	if (num[2] >50)
    		three ++;
    				else 
    					greater = false; 
    
    	if (num[3] >50)
    		four ++;
    				else 
    					greater = false; 
    
    	if (num[4] >50)
    		five ++;
    				else 
    					greater = false; 
    
    	if (num[5] >50)
    		six ++;
    				else 
    					greater = false; 
    
    	if (num[6] > 50 )
    		seven ++;
    				else 
    					greater = false;
    
    	if (num[7]> 50 )
    		eight ++;
    				else	
    					greater = false;
    
    
    	while (greater)
    
    		
    		if (num[0] < 10)
    			one --;
    				else 
    				greater = false;
    
    	
    		 if (num[1] <10)
    			two --;
    				else 
    				greater = false; 
    
    		if (num[2] < 10)
    			three --;
    				else 
    					greater = false; 
    
    		if (num[3] < 10)
    			four --;
    				else 
    					greater = false; 
    
    		if (num[4] < 10)
    			five --;
    				else 
    					greater = false; 
    
    		if (num[5] < 10)
    			six --;
    				else 
    					greater = false; 
    
    		if (num[6] < 10 )
    			seven --;
    				else 
    					greater = false;
    
    		if (num[7] < 10 )
    			eight --;
    				else 
    					greater = false;
    
    
    	
    
    		if (one == 1)
    			cout << "Number 1 is greater than 50!" << endl;
    		else if (one == -1)
    			cout << "Number 1 is less than 10!" << endl; 
    
    		if (two == 1)
    			cout << "Number 2 is greater than 50!" << endl; 
    		else if (two == -1)
    			cout <<"Number 2 is less than 10!" << endl;
    
    		if (three == 1)
    			cout << "Number 3 is greater than 50!" << endl;
    		else if (three == -1)
    			cout << "Number 3 is less than 10!" << endl; 
    
    		if (four == 1)
    			cout << "Number 4 is greater than 50!" << endl; 
    		else if (four == -1)
    			cout << "Number 4 is less than 10!" << endl;
    
    		if (five == 1)
    			cout << "Number 5 is greater than 50!" << endl;
    		else if (five == -1)
    			cout << "Number 5 is less than 10!" << endl; 
    
    		if (six == 1)
    			cout << "Number 6 is greater than 50!" << endl; 
    		else if (six == -1) 
    			cout << "Number 6 is less than 10!" << endl;
    
    		if (seven == 1)
    			cout << "Number 7 is greater than 50!" << endl;
    		else if (seven == -1) 
    			cout << "Number 7 is less than 10!" << endl;
    
    		if (eight == 1)
    			cout << "Number 8 is greater than 50!" << endl; 
    		else if (eight == -1)
    			cout << "Number 8 is less than 10!" << endl;
    }
    
    return 0;

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should still not have variables called one, two, three, etc. I'm not sure what your assignment is, exactly, but I see no reason for their existence. Also there is no reason to not put your input in the loop. Basically any time you find yourself doing the same thing but changing 1 to 2, 2 to 3, etc, that means you need a loop.

    A hint, just for input:
    Code:
    for (i = 0; i < 8; i++) {
        cin >> num[i];
    }
    Last edited by tabstop; 03-22-2010 at 10:03 AM. Reason: Fix from DavidP below incorporated :|

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by King Mir View Post
    Here you want to use an arrays instead of variables that include a number in their name.

    Instead of int num1, num2 ect, use int num[8].

    Then you can do the same operation for each index.
    Quote Originally Posted by CPPN0OB View Post
    Ok, I did what you said. Although I still feel as if I am defeating the purpose of looping.
    AHHHH! What?? This is for the purpose of looping...

    Quote Originally Posted by tabstop View Post
    A hint, just for input:
    Hopefully that will put the pieces together for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Just a small correction on tabstop's code he posted:

    Code:
    for (i = 0; i < 8; i++) {
        cin >> num[i];
    }
    My Website

    "Circular logic is good because it is."

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Ok, I'm not sure I'm getting anywhere and I'm getting really frustrated with this! I've read the chapter in my book on loops like 10 times, and I've been looking all over the web. It's driving me insane because it's such a simple task in my head. I just keep drawing blanks when I go to write the code.

    So here's what I'm shooting for:

    Use a loop to echo the 8 numbers input

    VV

    Use a flag-controlled loop to check if any of the numbers are greater than 50

    VV

    Use another flag-controlled loop to check if any of the numbers are less than 10.

    VV

    After both loops are run I want to output which numbers were greater than 50, which were less than 10 or if they were neither.




    The problem I'm having is understanding how to use a loop to do the same thing 8 times. I know that's what loops are for but for some reason I just can't grasp it. It would be much easier for me to write my own functions and then call them but we aren't allowed to do that for this lab. I'm so confused!

  10. #10
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    So let's get to the core of the problem: what is it that you do not understand about loops?

    Do you understand how to write a loop?
    Do you understand why you need a loop?
    Do you understand what loops are used for?
    Do you understand when you need to use a loop?

    A loop is essentially meant to achieve your goals when you have a repetitive behavior that needs to be done (such as inputting several numbers).

    Instead of writing:

    Code:
    cin >> x1;
    cin >> x2;
    cin >> x3;
    cin >> x4;
    cin >> x5;
    cin >> x6;
    cin >> x7;
    cin >> x8;
    I can write a for loop:
    Code:
    int x[8];
    for (int i = 0; i < 8; i++ )
    {
    	cin >> x[i];
    }
    Or I can write a while loop:
    Code:
    int x[8];
    int i = 0;
    while (i < 8)
    {
    	cin >> x[i];
    	i++;
    }
    Or I can write a do-while loop: (the least common type of loop)
    Code:
    int x[8];
    int i = 0;
    do
    {
    	cin >> i;
    	i++;
    } while(i < 8);
    We are trying to help you out without actually doing your assignment for you.

    Apparently, your teacher is requiring you to use several loops (although you can do this with only 1 or 2 loops, your teacher wants you to use 4 or 5 loops, which is okay...because your teacher wants you to learn about loops).

    So here is a hint. Let's say we are finished inputting our 8 numbers. Now we want to see which are greater than 50. We could do something like this:

    Code:
    bool greater[8];
    
    //Use a loop to check if any numbers are greater than 50
    for (int i = 0; i < 8; i++)
    {
    	if (input[i] > 50) greater[i] = true;
    	else greater[i] = false;
    }
    You could do something very very similar to check which numbers are less than 10.
    My Website

    "Circular logic is good because it is."

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The point right now is that all this:
    Code:
    int num[8];
    int one=0, two=0, three=0, four=0, five=0, six=0, seven=0, eight=0;
    while (greater)
      {
    	if (num[0] > 50) 
    		one ++;
    Can be reduced to something like:
    Code:
    int input[8];   // formerly "num"
    int numbers[8] = {0};  // all zero  
    int i;
    while (greater) {
          for (i=0;i<8;i++) {
                if (input[i] > 50) numbers[i]++;
                else greater = false;
          }
    Please note I changed the name of "num" to "input", for clarity.

    I am not promising your code will now do exactly what you want, but this will help get it under control* and hopefully also help you to see the "use value" and potential of loops.

    * in the sense that if it does not do what you want, you now have a single 2 line block to debug rather than the same thing repeated eight times.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  3. need help on error handling.
    By broli86 in forum C Programming
    Replies: 9
    Last Post: 06-19-2008, 11:55 AM
  4. Checking for flag input through argv
    By cisokay in forum C Programming
    Replies: 6
    Last Post: 05-11-2005, 10:51 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM