![]() |
|
#1
|
|||
|
|||
Problem with Array Passing/Kmeans AlgorithmHi all...my name is Larry and I am a new member to the forum. I have registered since I am having trouble with my homework. Please bear with me as I am not a CS major, but an EE/CE major, so I am aware that my code is very inefficient.
I came across a K-Means algorithm code on a website and am writing a program for my image processing class. I am trying to incorporate the code and specifying the person as the source. However, I am very new to C++ and can not get an array to pass to the program correctly. I was also wondering what the num_points and dimensions variables refer to (if anyone is familiar with K means clustering). I basically have 12 "test" images that I must cluster into 4 groups. Each of the 12 images has 9 feature points associated with it. I have hardcoded these values in the function call (hope I have the parameters correct). My greatest problem is that I got my program to compile but it seems to error out when it gets to the function call. I put a print statement right before the call and it shows up, and when the function call is commented out the program runs fine. Another problem I am having is with the float Laws 3D arrays. I have tried to make them 2D arrays, and the program compiles. But when I try to run the program, it just errors out. I tried to put a print statement at the very beginning of the code, but it doesn't print. I can not figure out why it works fine when I am using a 3D array. I only need a 2D array for my use. I am using Bloodshed Dev C++ compiler on Windows XP. The K-means algorithm code I am using is written for C/C++ by Roger Zhang (I saved the file as a C++ file) and can be found here: cs.smu.ca Link is disabled so the site is here: cs.smu.ca/~r_zhang/code/kmeans.c If I have violated some posting rules, please let me know...Thanks in advance! My code is listed below. CPP / C++ / C Code:
|
|||
|
#2
|
|||
|
|||
Re: Problem with Array Passing/Kmeans AlgorithmQuote:
Well I have a number of problems with your code. 1. Maybe not a biggie for you, but bad news for others. Standard C++ does not allow variable-length arrays so things like the following won't compile with some commonly-used C++ compilers: CPP / C++ / C Code:
This feature of the latest C standard may be available with some C++ compilers (GNU, for example). But it's not standard C++. If your C++ compiler accepts it, with no errors, then changing it won't solve your problem, so don't worry about it for now. I just thought I would mention it in case some other people want to help and they find that their compiler gags on this. 2. A biggie. Standard C++ does not allow array dimensions to be zero, so things like the following will not be accepted by some C++ compilers: CPP / C++ / C Code:
Even if your compiler accepts declarations of objects with size equal to zero, absolutely nothing good can come from it. You don't use BoundaryExtend anywhere, but if you ever tried to store or retrieve information from that array, it would be illegal (undefined behavior up to and including program crash, or maybe even worse, some silent bug that would not show up in testing but would bite you in the butt later). The amount of memory allocated to an array is the product of the individual dimension sizes. If you absolutely need a 3-D array for some purpose not obvious in the code that you posted, you can make the third dimension [1] instead of [0]. But I can't imagine the situation where that is necessary or sufficient to get your application working. As far as the Lawsx[][][] arrays, just make the declarations 2-D, like CPP / C++ / C Code:
CPP / C++ / C Code:
3. This is the last biggie, and it's a dusie: You say that you "got my program to compile," and I'm thinking that the only you got it to compile was by lying to the compiler: CPP / C++ / C Code:
I'm not trying to give you a hard time, but this is not uncommon: Someone with no experience with a language gets some canned program from somewhere and tries to cobble up some kind of application without any basic understanding. To prove my honorable intentions and not just flame you for not knowing, I offer a suggestion. Give it what it needs. CPP / C++ / C Code:
You have created the equivalent of a 2-D array, but now it is in the form that the function needs. Now, I have some other concerns, that may or may not be a problem. You have more than a few megabytes of storage allocated by things like CPP / C++ / C Code:
If you fix the kmeans thing like I showed above and the program still crashes with no error messages, it is just possible that your compiler/linker won't support this much automatic storage. You might have to go back and make all of these "static", (the memory comes from another place). That is: CPP / C++ / C Code:
In fact, why don't you do it now: CPP / C++ / C Code:
(You won't be able to make the variable-length arrays "static", but the Laws are the big hitters. Change them.) One final note: If you are going to put the kmeans code in a separate file, the easiest way to get it to compile properly with the C++ code of your main() function is to call the other one kmeans.cpp. The kmeans code is C code, but will compile OK as C++ also. Make sure they both get compiled as C++. (Otherwise linking C code with C++ code requires a little more work). Regards, Dave "No one was born knowing this stuff, you know." ---davekw7x Last edited by davekw7x : 12-Jul-2007 at 14:30.
|
|
#3
|
|||
|
|||
Re: Problem with Array Passing/Kmeans AlgorithmDave...Thank you for schooling me...I managed to get the program working because of you. The static portion fixed the exact issue I was trying to describe, and defining the array as you specified made it work.
Under point 1, my arrays are not dynamic. They are fixed sizes due to the #define. Again...I would like to express my gratitude for your assistance. I was not able to PM you to deliver this more personally. Larry |
|
#4
|
|||
|
|||
Re: Problem with Array Passing/Kmeans AlgorithmQuote:
I just thought I would mention it in case some of the readers of this thread try it with Borland or Microsoft compilers like the ones that I have used. Or if GNU users turn on the "-pedantic" command line switch to make sure that the code complies with the Standard (Won't work.) I am very glad to have been given the chance to help. Good Luck! Regards, Dave |
Recent GIDBlog
Review: Gel laptop cooling pad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to sort in C++ alphabetically | wilen | C++ Forum | 5 | 20-Apr-2007 15:43 |
| problem with filling a char array in a windows GUI app. | ryver | C++ Forum | 1 | 13-Aug-2006 09:22 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 22:26 |
| Problem in array | Kay Chan | C Programming Language | 2 | 05-Oct-2004 22:16 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The