How to call this function with correct arguments?

This is a discussion on How to call this function with correct arguments? within the C Programming forums, part of the General Programming Boards category; Hi, I'm Steve from Germany. First of all, I'm not a programmer, at least, not a good one. I'm a ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    How to call this function with correct arguments?

    Hi,

    I'm Steve from Germany.

    First of all, I'm not a programmer, at least, not a good one. I'm a researcher and I need to implement this function and test it and use it for my research. I tested some clustering methods in JAVA and Matlab and also I want to test it on C. I don't know too much about programming, especially about C, I know nothing. I tried to implement some basic methods but I failed. So I think maybe you can help me.

    It's all about K-Means Algorithm. I'm working on a disease and I'm trying to find ways to early diagnosis. Anyway, these are details. The thing is, I found a 'free to use' function but I don't know how can I use it. I tried to learn something from Net, I downloaded a compiler, I paste the code and I get many errors, yeah, it's fear enough... And I heard that I have to do some "calling function" stuff but I don't know how to... So I need your help!

    The code is in the link below:

    http://cs.smu.ca/~r_zhang/code/kmeans.c

    It's not imperative that using this function, it can be another one but it had to written in C.

    Thanks in advance for your help!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Do you know how to call a generic maths function? If so, you know how to call a C function. You just need to know a little extra about variables and pointers.
    The source's comments are quite descriptive ( although I'd prefer better variable names ). If you know what the function does, what arguments it should be passed and what it will return, you've got everything you need.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Sorry but as I said, I know nothing but some Input/Output codes, maybe a little bit loops but no functions, no arrays or no pointers... I want to learn programming and do this own my own but I have a few days to do it and this is a research that I need to complete in the upcoming days. So I need a helpful hand...

    Thanks for your reply!

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Why is it that "it had to be written in C"?
    Why did you leave your "research" until the last minute?
    Sounds like homework.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Not going to happen, I think.

    The way that function works, you would be taking a moderate a risk in using code you don't understand from someone else to call it. The code isn't bad (although it's not great) but it is easy to misuse it (technically, it can cause problems, but the main misuse is not properly understanding the results it produces). It's a bit of a blunt instrument that, if you don't use it right, will hinder you more than help you.

    And if someone here was to give you a partial solution, the outcome would be you having to do MORE work to understand that partial solution if you need to adapt it in any way for your purposes. After all, nobody here understands what you intend to do with that function.

    The best suggestion I can offer is to find a basic text on C and read the first few chapters. If you're motivated, that will take a day or so, and there is no crime in having a basic textbook beside you as you try to get things working. That's all you need to do to get up to speed enough to use this function effectively .... and be able to adapt it for whatever task you have at hand.

    Your best strategy as a researcher - unless you have access to a paid support team who will do things like this - is to be self-sufficient in finding workable solutions to problems you are trying to solve, not to try to find partial solutions you don't understand and try to get someone else to make them work for you.

    Apart from anything else, your problem looks a lot like homework. And this site has a policy that essentially says people should do their own homework.
    Last edited by grumpy; 09-13-2013 at 05:13 PM.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    @oogabooga

    Because I need a program which had to be written in C I found some 'ready to use' JAVA and Matlab packages but I couldn't find anyone for C but this function.

    I said "few days" and my 'research' is not about "Clustering/Programming" at all, it's just a section of my research and this section is my next step. But yeah, you are right, we can name it as a homework.

    @grumpy

    Okay, I get the message. I just wanted to see how to land lies. Thanks for your reply.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by SLiam View Post
    @oogabooga

    Because I need a program which had to be written in C I found some 'ready to use' JAVA and Matlab packages but I couldn't find anyone for C but this function.

    I said "few days" and my 'research' is not about "Clustering/Programming" at all, it's just a section of my research and this section is my next step. But yeah, you are right, we can name it as a homework.
    "Researchers" do not do homework. You are a student and your initial post was misleading.


    If your homework is not a programming assignment, then why does it need to be in C? Why not use the Java you found?


    Anyway, you may get some help if you show what you've attempted.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    I said "we 'can' name it", didn't say it is exactly a homework. Sorry, but you're understanding things as the way you want to understand, so I won't insist I just wanted to enrich my research, that was the reason why I'm here, that's all. It's not important anymore, but thank you for your helpful approach.

    Look at the bright side, I won't mislead you about something again
    Best regards...

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I'm not "understanding" anything here. And I'm perfectly willing to believe you're not a liar.
    But my question still stands: if it's not homework, why can't you just use the Java you found?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by oogabooga View Post
    "Researchers" do not do homework.
    That's not true. Professional researchers often do homework. Ranging from work for additional training and study, stuff done out of interest to keep up, taking work home, etc etc.

    Quote Originally Posted by oogabooga View Post
    You are a student and your initial post was misleading.
    Students (undergraduate, graduate, postgraduate) who are doing research often describe themselves as researchers. That is not a lie (after all they are doing research) but it is a selective interpretation of the truth (in the sense that people conventionally attach more meaning to the word "researcher").

    It's actually a form of academic and professional grandstanding. Akin to the fact that some professions in law and medicine have a title of "doctor" despite not meeting the original definition, which is holding a Doctor of Philosophy degree. Or that surgeons (and other people who have a Doctor of Philosophy in medicine) adopt a title of "Mister" to distinguish themselves from medical professionals who do not have such formal qualifications (which can be really confusing for people who encounter a female surgeon who insists on the title).
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Although it's true that professionals often take additional training, he didn't give the impression of being a professional.


    And he couldn't or wouldn't explain why his program had to be in C.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by oogabooga View Post
    Although it's true that professionals often take additional training, he didn't give the impression of being a professional.


    And he couldn't or wouldn't explain why his program had to be in C.
    No argument from me on those points, boogaman.
    Right 98% of the time, and don't care about the other 3%.

  13. #13
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    @oogabooga

    Please listen, I have an academic ID, I'm working on this project for five months and I'm tired and I don't need to do an explanation about something that I don't want to. If you have a special question, you can ask me by using Private Message button.

    I don't have to give any information to someone who calls me to account... If I said 'this is a research', then it is so. And if I said "I need help about 'this' subject", I'd refer to something that specific like a program which 'had to be written' in C. I have no special reason for it, do I have to? It's a forum about C Programming, if I need some help about JAVA or another programming language, I'd not be here. And above all, I'm not here to lie someone about something... Please think twice before you speak once.

    If here are someone who's gonna help me, I'm waiting for it and I can answer your questions with pleasure.

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I think what people are trying to say is that in order to use this function, you're going to have to learn some C. If you know nothing about arrays or pointers, it's going to be difficult for you to use this code. I can (and will) give you an example, but if you don't understand it you'll find it difficult to modify it for your own uses.

    It would help if you posted your attempt, and the compiler errors you got. Then we could explain the problems and point you at what you need to learn about to get it right.

    I should prefix this by saying I have no idea whatsoever what ' k-means clustering' is. Looking at the code and looking at wikipedia, it seems that you will have an input set of N data points with D dimensions, and you'll want to get out a set of C 'clusters' also of D dimensions. The function also gives you a list N items long of numbers indicating which point went into which cluster.

    So here's the basics of the function - they've been unhelpful by not giving the arguments helpful names.
    Code:
    Parameters:
    data: double** dimensions n x k. Means n pointers to n arrays of doubles of length k.
    n: number of data points
    m: dimensions
    k: desired number of clusters
    t: error tolerance
    centroids: double** array, dimensions k x m, meaning k pointers to k arrays of m doubles
    
    int *k_means(double **data, int n, int m, int k, double t, double **centroids)
    Here's an example of how you might call it, if you wanted to enter the data on the command line.

    Code:
    int main(void)
    {
       int dimension, points, clusters;
       int d, p, c;
       double tolerance;
       double temp;
       int *labels;
       double **centroids;
       double **data;
       
       printf("\nHow many data points? ");  
       scanf("%d", &points);
       printf("Dimension? ");
       scanf(" %d", &dimension);   
       printf("num clusters? ");
       scanf(" %d", &clusters); 
       printf("error tolerance? ");
       scanf(" %lf", &tolerance);        
       
       printf("\nenter data:\n");
          
       data = malloc(sizeof(double*) * points);
       
       /* Read data into array */
       for (p = 0; p < points; p++)
       {
           data[p] = (double*)malloc(sizeof(double) * dimension);
           for (d = 0; d < dimension; d++)
           {
             scanf(" %lf", &temp);
             data[p][d] = temp;
           }      
       }
       
       /* Allocate space for results */
       centroids = malloc(sizeof(double*) * clusters);
       for (c = 0; c < clusters; c++)
       {
           centroids[c] = malloc(sizeof(double) * dimension);
       }
       
       labels = k_means(data, points, dimension, clusters, tolerance, centroids);
    
       printf("\n");
       
       for (p = 0; p < points; p++) {
           printf("data point %d is in cluster %d\n", p, labels[p]);
       }   
       
       printf("\n");
       
       for (c = 0; c < clusters; c++) {
           printf("\nCluster %d:\n", c);
           for (d = 0; d < dimension; d++)
           {
               printf("%lf ", centroids[c][d]);
           }
       }      
    }
    You can compile this with gcc with:
    gcc test.c -o test -lm

    The "-lm" tells the linker to link the maths library, which you need as k_means uses "pow".

    Example run (data points borrowed from Iris flower data set - Wikipedia, the free encyclopedia)

    Code:
    How many data points? 5
    Dimension? 4
    num clusters? 2
    error tolerance? 0.001
    
    enter data:
    5.1 	3.5 	1.4 	0.2
    4.9 	3.0 	1.4 	0.2
    4.7 	3.2 	1.3 	0.2
    4.6 	3.1 	1.5 	0.2
    5.0 	3.6 	1.4 	0.2
    
    data point 0 is in cluster 0
    data point 1 is in cluster 1
    data point 2 is in cluster 1
    data point 3 is in cluster 1
    data point 4 is in cluster 0
    
    
    Cluster 0:
    5.050000 3.550000 1.400000 0.200000 
    Cluster 1:
    4.733333 3.100000 1.400000 0.200000
    If you want to read the data from a file, the code will be very similar.
    If you're thinking that you can do this:
    Code:
       double data[5][4]= {{5.1, 3.5, 1.4, 0.2},
                           {4.9, 3.0, 1.4, 0.2},
                           {4.7, 3.2, 1.3, 0.2},
                           {4.6, 3.1, 1.5, 0.2},
                           {5.0, 3.6, 1.4, 0.2}};
    Don't. It won't work. You can do this if you change the type of data in the function, but otherwise you can't.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-10-2011, 01:50 PM
  2. Correct way to call boost::bind?
    By jmd15 in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2008, 03:24 PM
  3. Correct way to pass arguments to this function
    By eponymous in forum C Programming
    Replies: 6
    Last Post: 03-19-2008, 06:56 AM
  4. a program that will call an execute but with arguments
    By what3v3r in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2006, 08:44 PM
  5. Replies: 2
    Last Post: 06-21-2005, 02:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21