Sign in to follow this  

C problem

This topic is 4480 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
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
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
How about this:


std::vector<int> peices;
int n_peices, peice_length, remaining_length;

/* Calculate number of peices needed */
n_peices = (total_length_required + 60) / 61;

/* Calculate shortest length - longest one will be this +1 */
peice_length = total_length_required / n_peices;
remaining_length = total_length_required % n_peices;

/* Insert short and long peices */
peices.insert(peices.end(), remaining_length, peice_length+1);
peices.insert(peices.end(), n_peices - remaining_length, peice_length);


Share this post


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


Didn't sound harsh to me :-). You've got a good enough explaination I'm willing to assume it isn't home work, especially considering how simple this is with a little explaination, and that you'll be flunking your course anyways if this IS a homework problem, most likely :-).

unsigned int segments_of_neon = 1;
double length_of_individual_segment_of_neon = initial_feet_of_neon;

/* while each individual piece of neon is too big... */
while ( length_of_individual_segment_of_neon > 61.0 ) {
/* ...divide each one in two! */
length_of_individual_segment_of_neon /= 2;

/* This also gives us twice as many segments: */
segments_of_neon *= 2;
}

/* We could also calculate the total number of segments after the fact: */

double segments_of_neon = initial_feet_of_neon / length_of_individual_segment_of_neon ;



[Edited by - MaulingMonkey on September 11, 2005 12:22:45 AM]

Share this post


Link to post
Share on other sites

This topic is 4480 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this