|
Hello All,
I was just stopping-by to make a rare post and I poked into this thread.
In my mind I think of these kinds of problems as 'least energy state' problems. These very often correlate to conditions in analog setttings. By thinking one's way forth-and-back through analog-digital conversions, some unnoticed feature of the problem often presents itself. Your analogue thougtht-picture of blocks in a tall wall is an example.
I imagined myself with a large appliance box and spheres of varying diameter representing the jobs. With spheres in the box I shake the box, stop, note the arrangement of spheres, and repeat.
Anyone else with a thought picture?
Tadeusz Westawic
An ounce of Clever is worth a pound of Experience.
|
|
|
|
|
Need C# codez urgentzzzzz!!!!!
Not really, I have code. However, I am after some advice: under embedded .NET, is this better implemented under event-driven code or in a more linear fashion with the dreaded Application.DoEvents() ? An event-driven implementation looks a bit of a brain-f*** IMO...
|
|
|
|
|
|
I have this 3rd party software that spits out a list of numbers formatted into text. They are integers but of varying number of digits. I usually get around 10,000 numbers in one string.
I'm turning all these numbers into an array of integers in C++ but it's taking me over 200 milli-seconds.
I was told that LabView can do the same thing in less than a milli-second.
Then a kind CP patron ran an experiment on C# and told me it only takes up to 20 milli-seconds.
I'm using CString::Find() to look for the space that separates each number in the string and CString::Mid() to get a piece of string per number and finally atoi() to turn it into integer. I know CString is slow, but I'm a bit surprised at the difference in the time of execution.
What is LabView doing that is so wonderful?
How can I improve my code in C++? Someone said something about not using Mid() to create new string each time but to put a section of char* directly into atoi() . I'm not sure how to do that except for doing things like memcpy in which case I might as well create a new CString as I do now.
Any ideas appreciated!
Almost, but not quite, entirely unlike... me...
|
|
|
|
|
One major observation:
- Get a profiler, and profile your code.
One minor observation:
- it is likely the over-allocation and deleting of memory of CString::Mid killing your performance.
Two options:
- modify the string in place, replacing spaces with \x0's, and use atoi to convert values.
- parse the digits by hand. Pseudo-pseudo code:
set n to 0.
Look at next character
Is it a space? store the integer in the result list.
Is it a number. set n = n * 10 + (int)character - 48.
Also:
- pre-size the resulting list to roughly input string length / estimated digit size, if you happen to know the string length up front.
|
|
|
|
|
My C# implementation, as posted in the Lounge... takes 1ms or less for 10000 numbers. This shows you how to avoid Mid() and therefore the overhead of creating strings, as well as a custom string-to-number function to avoid the overhead of a more generalized function.
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
int j = 100;
for (int i = 100; i < 101000; i++)
{
if (j > 999) j = 100;
sb.Append((j*10).ToString() + " ");
j++;
}
string s = sb.ToString();
int idx = 0;
int startidx = 0;
int length = s.Length;
List<int> nrs = new List<int>((int)(length*0.25));
var watch = Stopwatch.StartNew();
do
{
if (s[idx] == ' ')
{
if(idx>startidx)
nrs.Add(atoi(s, startidx, idx));
startidx = idx + 1;
}
idx ++;
}
while (idx < length);
Console.WriteLine(watch.ElapsedMilliseconds);
Console.ReadLine();
}
static int atoi(string c, int start, int len) {
int res = 0;
int i = start;
len += start;
while (i<len && c[i] >= '0' && c[i] <= '9')
res = res * 10 + c[i++] - '0';
return res;
}
|
|
|
|
|
PaulowniaK wrote: How can I improve my code in C++?
Next time, show us your code so we can answer that.
PaulowniaK wrote: CString::Find()
Depending on the overload, this could be your problem. You searching from the beginning of the string each time?
Those superficial observations aside, the above post (by J. Dun... something) looks like a great answer. Though it would have been nice to see your code for comparison.
|
|
|
|
|
This (plain C ) takes 1 ms circa on my system (Dual Core 1.8 GHz).
int get_all_numbers( const char * txt, int txt_size, int * a, int max_count)
{
const char * p0 = txt;
const char * p1 = NULL;
const char * guard = p0+txt_size-1;
int count = 0;
while (p1 < guard && count < max_count)
{
a[count++] = (int) strtol(p0, (char**)&p1, 10);
p0 = p1;
}
return count;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
hii..i need the source code for 15 puzzle problem in C or C++
|
|
|
|
|
then why do you post in the algorithm forum, if all you care about is getting code handed over to you?
|
|
|
|
|
On top of that, they cross posted in the Wicked Code forum too.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
If you want someone to just hand over code to you, you've come to the wrong site. You're looking for Rent-A-Coder.com[^].
|
|
|
|
|
i am sorry but i need an algorithm itself for 15 puzzle problem for which i trying hard but unable to get it !
|
|
|
|
|
123lucy45 wrote: i need an algorithm itself
Aha, that is different now. I used to solve such puzzles with an exhaustive search, breadth first. The only caveat is one must detect positions that have been reached before. Not hard at all.
|
|
|
|
|
Wow. A little Googling for "15 puzzle solve algorithm" reveals a bunch of interesting stuff, and examples.
|
|
|
|
|
I'll give you a starting point. This puzzle can be solved efficiently with three algorithms:
1. The simple case: For numbers not in the rightmost column or the last two rows, move tiles to create a blank space between the next tile to place, and the correct location for this tile. Then move the tile into the blank space, bringing you one step closer to correct placement for this tile. Repeat until the tile is in the correct location.
2. For a position in the rightmost column (but not the last two rows), the problem is getting the wrong tile out and the right tile in. Get the right tile under the correct location (in the rightmost column). Then move another tile in the same row as the correct location down one (temporarily moving it out of its correct location), slide the rest of the row to the left, move the right tile up (to its correct position), get a blank spot under the wrong tile, move the wrong tile down, move the rest of the row right, then move the tile you moved out of its correct position back, thus completing the row.
3. For the last two rows, move them in a circular pattern until you get the opportunity to swap two tiles that are in the wrong order. Repeat until the last two rows are correct.
|
|
|
|
|
Does anyone have an algorithm that avoids a month table which computes the last day in a month?
|
|
|
|
|
convert to date (1- x+1 - year); // where x is the month number - obviously you need to take care of December.
reduce 1 day from it.
Hope it helps
It is Good to be Important but!
it is more Important to be Good
|
|
|
|
|
I'd get the 1st day of the month after the one you're interested in, then subtract 1 day.
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
|
Hi, I posted this on the C# section but then I got to think maybe the solution could be mathematical so I should have posted here instead.
I want to create a filled cylinder or a tube that can be slanted.
So what I do is I created a closed curve using 4 points (using FillClosedCurve) with 0.8f tension. Then I translated the same closed curve vertically to create a sense of height. But then how do I fill the sides?
Here is an image to illustrate my explanation: http://img692.imageshack.us/img692/9461/100319codeprojectquesti.png[^]
Thanks for any help.
|
|
|
|
|
You can calculate the semi-major axis length. Have a look at:http://en.wikipedia.org/wiki/Semimajor_axis[^].
If you start from the centre of the ellipse (which in your example is the centre of your rectagle) and add this length, you get the point on the circumference of the ellipse where you can start (and end) drawing your vertical line.
Starting from there, you can adjust the angle to find the perfect spot for your verical lines.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Can you give me the equation to find the semi-major axis length, knowing the four points I got? My head just in the verge of exploding reading the article. I'm really not that good at math, honest.
I also read and tried the code from http://en.wikipedia.org/wiki/Ellipse[^], following the links in wikipedia. This function to generate an ellipse is almost the same as DrawClosedCurve() or DrawEllipse(), using that I still can't get the outer most point to put the line.
|
|
|
|
|
Valeriant wrote: I also read and tried the code from http://en.wikipedia.org/wiki/Ellipse[^], following the links in wikipedia. This function to generate an ellipse is almost the same as DrawClosedCurve() or DrawEllipse(), using that I still can't get the outer most point to put the line.
Yep that's a good starting point too. See where it makes you go through the whole circumference:
for (var i = 0; i < 360; i += 360 / steps)
You can use that to get the coordinates to the interesting points, using the angle.
I'll try to put together some code when I have some spare time, but it won't be soon I'm afraid.
2+2=5 for very large amounts of 2
(always loved that one hehe!)
|
|
|
|
|
Appreciate the time, will wait your help.
I did try each points generated from the steps, but none are exactly the outer most two. And formulating the angle, I cannot.
|
|
|
|