473,379 Members | 1,201 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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"<<endl;

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;
}






.
Nov 30 '06 #1
7 1285
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.
Nov 30 '06 #2
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
Nov 30 '06 #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
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"<<endl;
else
cout<<"False"<<endl;
}
Nov 30 '06 #4
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
Nov 30 '06 #5
DeMan
1,806 1GB
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
Nov 30 '06 #6
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,
Nov 30 '06 #7
DeMan
1,806 1GB
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)
Nov 30 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
7
by: VK | last post by:
I was getting this effect N times but each time I was in rush to just make it work, and later I coudn't recall anymore what was the original state I was working around. This time I nailed the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.