Page 1 of 1

[solved] trouble with code

Posted: Wed May 19, 2010 10:39 am
by polyneem
by looking at the code below you can see that I doing is finding out if a number is a multiple of another number, by either 11 or 13, what I am trying to find out is how to make it so if a blocker number is in between them then to stop going down that path, I'll try to explain what I mean by that below the first piece of code.

Code: Select all

	for (int i = 1; i<=7; i+=1)
	{
		if (newNumber == lastNumber - (i * 11) || newNumber == lasNumber + (i * 11) || newNumber == lastNumber - (i *13) || newNumber ==lastNumber + (i * 13))
		{
		sayNumber();
		}
	}
lets say newnumber is 11 and lastnumber is 55, and there is a blocking number at 33, this would be a false statement because 55 - i reaches the blocker number first. Should I split this if loop into 4 different statements and test this 1 at a time, or is it easier to test everything at the same time?

Re: trouble with code

Posted: Wed May 19, 2010 10:40 am
by avansc
we dont do homwork here.

edit: seems like homework, if not, sorry.

Re: trouble with code

Posted: Wed May 19, 2010 10:40 am
by polyneem
who said this is homework?

Re: trouble with code

Posted: Wed May 19, 2010 10:47 am
by short
he did, and I agree. You didn't deny it.... so it probably is.

Re: trouble with code

Posted: Wed May 19, 2010 10:51 am
by polyneem
well its not homework, dunno why you guys would think so, I don't even take any computer courses in school.

Re: trouble with code

Posted: Wed May 19, 2010 11:02 am
by avansc
If you explain the problem a bit better, i'll see what i can do.

Re: trouble with code

Posted: Wed May 19, 2010 11:10 am
by zeid
If you are trying to work out whether a number is a multiple of another number you are going about it the wrong way. I'm not 100% sure what you are asking but I hope this helps:
see % operator (modulus).
you can even go on google and try it out, google this:
26%13
try changing the numbers about look up what modulus is and you should be able to work it out.

Re: trouble with code

Posted: Wed May 19, 2010 11:12 am
by Genesis
polyneem wrote:by looking at the code below you can see that I doing is finding out if a number is a multiple of another number, by either 11 or 13, what I am trying to find out is how to make it so if a blocker number is in between them then to stop going down that path, I'll try to explain what I mean by that below the first piece of code.

Code: Select all

	for (int i = 1; i<=7; i+=1)
	{
		if (newNumber == lastNumber - (i * 11) || newNumber == lasNumber + (i * 11) || newNumber == lastNumber - (i *13) || newNumber ==lastNumber + (i * 13))
		{
		sayNumber();
		}
	}
lets say newnumber is 11 and lastnumber is 55, and there is a blocking number at 33, this would be a false statement because 55 - i reaches the blocker number first. Should I split this if loop into 4 different statements and test this 1 at a time, or is it easier to test everything at the same time?
A few points to remember:
1. "if loop": The if statement is a conditional structure, not a looping structure - watch your terminology.
2. When incrementing values by one, it is generally a good idea to use ++i (e.g. for(int i = 1; i <= 7; ++i))
3. If I understood your question correctly, the following will achieve the same end result as your single if statement:

Code: Select all

if(newNumber == lastNumber - (i * 11)) sayNumber();
else if(newNumber == lasNumber + (i * 11)) sayNumber();
else if(newNumber == lastNumber - (i * 13)) sayNumber();
else if(newNumber == lastNumber + (i * 13)) sayNumber();
There is probably a more efficient and elegant method of doing what you want, but we'll need more information.

Re: trouble with code

Posted: Wed May 19, 2010 11:25 am
by polyneem
@Genesis I know about the ++ and -- but previously I was using different numbers and the += was an efficient way to change the number, your code was what I was thinking of changing to, but what the big thing that I am trying to ask is really tough to get across.

I'll try explaining it with text instead of code this time.

if I have a number set at 11 and another number set at 55, and a number to stop the counting set at 33, I then tell the program to decrement 55 by 11 until it either reaches 33 or it reaches 11, I want it to stop the loop at 33 and at 11 I want it to call a function.

Is that explained better? I must apologise for the misunderstanding I've never been good at trying to get my message across. After rewriting my question has given myself some new ideas on how to approach the problem.

Re: trouble with code

Posted: Wed May 19, 2010 11:27 am
by avansc
i dont think 11 and 13 have common factors.. prime...

55-11
44-11
33 exit loop, everytime.

not sure what you want to do.

edit : nvm, im sure i just dont understand what you want.

Code: Select all

int func(int low, int high, int stop)
{
	while(high > low)
	{
		high -= low;
		if(high == stop)
			return 1; // hit the stop number.
	}
	if(high == low)
		return 0; // call function
	return -1; //got below the low
}

Re: trouble with code

Posted: Wed May 19, 2010 11:41 am
by polyneem
never mind, I thank you all for your help, but its too hard for me to explain what I want to do, when I figure it out I'll post what the solution was and then maybe we can create a question to help me explain myself better in the future, I once again thank you all for your time.

Re: trouble with code

Posted: Wed May 19, 2010 11:58 am
by polyneem
so I solved it, it might not be the best way but it gets the job done, but how would you rephrase my question, so that I can be more clear next time?

Code: Select all

int x = 11;
int y = 55;
int block = 33;
bool moving = false;
int counting =0;
bool blocking = false;

	do
	{
		if (counting < 7)
		{
		counting ++;
		}
		if (block == (y - (counting * 11)))
		{
		printf("blocked\n");
		blocking == true;
		moving = true;
		}
		if (x == (y - (counting * 11)) && blocking == false)
		{
		printf("done\n");
		moving = true;
		}


	}while (moving == false);