2 Replies - 198 Views - Last Post: 20 April 2013 - 10:34 AM Rate Topic: -----

#1 mohamed_200259  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-April 13

i don't understand why we use variable bol in this code

Posted 20 April 2013 - 07:26 AM

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int darr[7],bol=0;
    cout<<"enter ur data please\n";
    for(int i=0;i<7;i++)
        darr[i]=getche();
    for(int i=0;i<7;i++)
        if(darr[i]!=darr[7-i-1])
        bol=1;
    if(bol==0)
        cout<<"\n symmetric\n";
    else
        cout<<"\n not symmetric\n";
    return 0;
}

This post has been edited by jimblumberg: 20 April 2013 - 07:32 AM
Reason for edit:: Added missing code tags. Please learn to use them properly.


Is This A Good Question/Topic? 0
  • +

Replies To: i don't understand why we use variable bol in this code

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3987
  • View blog
  • Posts: 12,299
  • Joined: 25-December 09

Re: i don't understand why we use variable bol in this code

Posted 20 April 2013 - 07:36 AM

Since you're not using braces with your control statements, there is no reason for the bol variable, the program will always print "\n not symmetric\n". It would be a different issue if you used the braces to surround the statements that belong in your control statements. Remember that without the braces only one line following the control statement is executed.


Please use code tags when posting code, and remember to ask the question in the body of your post. Not in the title or in your code.

:code:


Jim
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1304
  • View blog
  • Posts: 4,488
  • Joined: 19-February 09

Re: i don't understand why we use variable bol in this code

Posted 20 April 2013 - 10:34 AM

In C integers are used for boolean values : 0 for false, any other number for true (usually 1 is used).
Your using C++ so a bool type variable would work.

If the name asymmetric is used instead of bol :

    /* asymmetric is false */
    int darr[7], asymmetric = 0;



    /* if one pair not equal, array is asymmetric */
    for(int i=0;i<7;i++)
        if(darr[i]!=darr[7-i-1])
          asymmetric = 1;




    if( !asymmetric)
        cout<<"\n symmetric\n";
    else
        cout<<"\n not symmetric\n";




Edit: and using a variable called symmetric (and changing the logic round) would probably be clearer - you can try that.

This post has been edited by #define: 20 April 2013 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1