Jump to content
  • Advertisement
Sign in to follow this  
Xloner12

C problem

This topic is 4667 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok im working in c, and making a program. The function im working on is to split a certin number. I need to take x and if its greater then 61 split it in half. And keep doing this to each half after that untile there all under 61. Im having trouble coming up with a loop to get this done. If some one could point me in ther right direction I would be really greatfull. Ive been working on this for the past weekend. Any help would be welcome. Thanks

Share this post


Link to post
Share on other sites
Advertisement
SiCrane: you're not the only one who thinks that.

Xloner12: Split it up into the action and the conditional, just like loops work:

for( initialization(s) ; conditional(s) ; action(s) )
{
more action(s)
}


This should be reallllly easy :-).

Share this post


Link to post
Share on other sites
Actually Its for a neon footage calculator im making at work. I dont take programing till next year in college.

Actually its quite tough, at least for me. I have no schooling on this yet. Im taking a series of differnt transformers.

Each transformer can handle a certien length of neon. IE 10 - 20 for example handles a 1030fpx. Im looking to make a function to sorth the neon in to segments if neccacary since the highest transformer can only handle 61 feet of neon. So I need to split it. Then take the split sums and pick out the transformers.

I was just having trouble starting the loop Im sorry it sounded like a home work problem.

Share this post


Link to post
Share on other sites
owned :)

EDIT: this is NOT good code , im using goto ! :). But you need something in a jiffy this should work. You should probably use a recursive function instead. Im presuming you want to split this up into a variable number of segments and store them somewhere , hence the reason for the vector.

In your calculator you will then want to iterate over the vector and you will have the length of each piece, do what you want with it at that point

EDIT2: just noticed you said you were working in C not C++, since you dont have access to the standard library then you will want to use realloc to create a dynamic array, im not modifying my code to do this as there are tutorials on it.



// Your going to end up with a variable number of pieces
std::vector<int> pieces;

// The first point in the vector
int next_piece = 0;

// Set the intial piece length
pieces[next_piece] = initial_length;

// You should use recurrsion instead of a goto, but this is a lazy hack that should work
REITERATE:

// Go through each piece and split it up
for(int n=0; n<pieces.count(); n++)
{
if(pieces[n] > 61)
{
next_piece++;
int tmp = pieces[n]/2 + pieces[n]%2;
pieces[n] = pieces[n]/2;
pieces[next_piece] = tmp;
}
};

bool passed = true;

for(int n=0; n<pieces.count(); n++)
{
if(pieces[n] > 61)
passed = false;
};

goto REITERATE;




Share this post


Link to post
Share on other sites
Yeah I thought it would take recursion. i just got to it in my book and its making my head spin. I guess i'll rest this program for a month or so untile I get recursion down.

Thanks alot.

Share this post


Link to post
Share on other sites
...

Number of transformers needed to run x feet of neon: floor(x/61) 61ft transformers and 1 (x%61)ft transformer.

Share this post


Link to post
Share on other sites
I just wrote it how he specified he wanted it done ;). But anyways, you dont NEED recursion, in fact it would make the code look more complicated IMHO. But clean-room no-goto code is prefered by most coders so I had to give you that statement. For a simple calculator, i wouldent fret it.

Share this post


Link to post
Share on other sites
Well I didnt mean to sound harsh. Its just that its not for home work. So my appoliges if it did.

Ok I'll simplifie it or try to simplifiy it one more time.

Im making a neon footage program.


x = neon

I will take x footage and must divid it among a set of const transformers.

they range differntly. 330, 530, 630, 930..... transformers. Each can support Y amount of neon. With each high number it can support more. The highest (*1530) can support 61 feet of neon.

So if you have a higher then 61 feet of neon count your going to have to split it.

Example: 62 feet of neon would need to be spilt among 2 transfomers capble of handling 31 feet each.

My problem is, what happens when you get like 720 feet of neon. I need to keep choping it till its all under 61 one, weather i have 20 transfomers that can support 51 feet or 20 feet as long as the division gets under 61.

Does that explain the problem a little bit better, I hope so. Thanks again for any help;.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!