[solved] trouble with code

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

[solved] trouble with code

Post 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?
Last edited by polyneem on Wed May 19, 2010 11:59 am, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: trouble with code

Post by avansc »

we dont do homwork here.

edit: seems like homework, if not, sorry.
Last edited by avansc on Wed May 19, 2010 10:45 am, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Re: trouble with code

Post by polyneem »

who said this is homework?
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: trouble with code

Post by short »

he did, and I agree. You didn't deny it.... so it probably is.
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Re: trouble with code

Post by polyneem »

well its not homework, dunno why you guys would think so, I don't even take any computer courses in school.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: trouble with code

Post by avansc »

If you explain the problem a bit better, i'll see what i can do.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: trouble with code

Post 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.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
Genesis
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 6
Joined: Mon May 17, 2010 9:01 am

Re: trouble with code

Post 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.
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Re: trouble with code

Post 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.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: trouble with code

Post 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
}
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Re: trouble with code

Post 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.
polyneem
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Tue May 11, 2010 7:23 pm

Re: trouble with code

Post 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);
Post Reply