function......

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nor farhana yaakub
    New Member
    • Sep 2006
    • 12

    function......

    hi.i.hope u can help me..juz study during the holiday break..i dun understand this code..but i try to understand by myself...i am not sure either my understanding is correct or not.......

    i got the output T,F,F,T,F,F,T,F ,F,T ..is it i have to plus the number with 4 then it should be like this.... 0,4,8,12,16,20, 24,28,32,36.... ..so after that,i have to consider the same numbers that will result the values zero when we modulus them with 2 and 3???(x%2==0 && x%3==0),right?? ?hope u understand it......can u please explain to me in details????plz reply me as soon as possible.......



    #include <iostream>
    using namespace std;

    bool strange(int x);

    int main()
    {
    int num=0;
    while (num<=36)
    {
    if(strange(num) )
    cout<<"True"<<e ndl;

    else
    cout<<"False"<< endl;
    num=num+4;
    }
    return 0;
    }
    bool strange(int x)
    {
    if(x%2==0 && x%3==0)
    return true;
    else
    return false;
    }






    .
  • MariyaGel
    New Member
    • Nov 2006
    • 7

    #2
    Your absolutely correct every time it goes through the loop 4 is added to the number and it turns out just as you have said it.
    Then the function decideds whether the remainder of that number divided by 2 is 0 as well as the remainder of that number divided by 3 is 0 and if both of these remainders are 0 then and only then will the function return true, otherwise it returns false.
    So it will only return true for the numbers that are multiples of 2 and 3.
    I think your understanding is perfectly fine.

    Comment

    • thefarmer
      New Member
      • Nov 2006
      • 55

      #3
      hi,

      i think there are some problem with your lines,

      i advise you to use some code which is not difficult to read,

      like:
      for ( num =0, num++, num <=36) // this will help you eliminate adding 4 to your code

      then declare your statement here!!!
      {
      statement here,,,,,,,,,,, ,,,,
      }
      if ( x%2==0 && x%3==0 )
      cout << "true",,,,,,,,, ,,,,,,,,,
      else
      cout << "false",,,,,,,, ,,,,,,,,,,
      etc..........

      }

      regards,
      thefarmer

      Comment

      • MariyaGel
        New Member
        • Nov 2006
        • 7

        #4
        Originally posted by thefarmer
        hi,

        i think there are some problem with your lines,

        i advise you to use some code which is not difficult to read,

        like:
        for ( num =0, num++, num <=36) // this will help you eliminate adding 4 to your code

        then declare your statement here!!!
        {
        statement here,,,,,,,,,,, ,,,,
        }
        if ( x%2==0 && x%3==0 )
        cout << "true",,,,,,,,, ,,,,,,,,,
        else
        cout << "false",,,,,,,, ,,,,,,,,,,
        etc..........

        }

        regards,
        thefarmer
        Well if you wanted to use a for loop instead on a while loop than I think you would have to do it like this:
        for (int num=0; num<=36; num+=4) // since I believe you have to go by four.
        {
        if (strange(num))
        cout<<"True"<<e ndl;
        else
        cout<<"False"<< endl;
        }

        Comment

        • thefarmer
          New Member
          • Nov 2006
          • 55

          #5
          i think adding four to this does'nt read the number preceeding or succeding to it, so it does'nt make sense if you add four to the number coz your program will decided whether the remainder of that number divided by 2 is 0 as well as the remainder of that number divided by 3 is 0 , if your number turns out 6 well definitely that was "true"and if both of these remainders are 0 then and only then will the function return true, otherwise it returns false.
          so even he reads the number from 0-36 your program will tell you completely wheather its true or not

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            True, except he is only looking for multiples of only(2 & 3) out of those that ARE multiples of 4, that is he is not testing against every number so he will get extra output if he tests extra values

            Comment

            • thefarmer
              New Member
              • Nov 2006
              • 55

              #7
              yes, right but i think it has more sense for the statement "num<=36" if he consider every other number beyond this limit so he can actually visualize and prove that theres a success in his program, i believe that nothings wrong by adding a variable 4 with his program , but it has more sense considering other numbers beyond this limits,

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                You may be right, but as I understand it the original program (which I think he (or she) was trying to interpret rather than had written, was dealing with multiples of 4 which are also multiples of 3.

                Interestingly (which may well be your point, and I apologise if i missed it) there is no need to check (mod 2) since all the numbers being dealt with are multiples of 4. In reality, the same results could be gained by merely testing for (mod 3) <ie %3> each time.

                Testing for each value is slightly different in that he wouldn't pick up any result for values like 6 (ie multiples of 3 but not 4) whereas your algorithm does. Given the initial post it is hard to read his intention (and there is probably no reason for us to go into it as much as we already have, let alone more)

                Comment

                Working...