C problem

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

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 on other sites
This sounds suspiciously like a homework problem.

Share on other sites
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 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 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 piecesstd::vector<int> pieces;// The first point in the vectorint next_piece = 0;// Set the intial piece lengthpieces[next_piece] = initial_length;// You should use recurrsion instead of a goto, but this is a lazy hack that should workREITERATE:// Go through each piece and split it upfor(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 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 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 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 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 on other sites
the code i wrote you would do that

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• Forum Statistics

• Total Topics
633346
• Total Posts
3011451
• Who's Online (See full list)

There are no registered users currently online

×