unexpected result

This is a discussion on unexpected result within the C++ Programming forums, part of the General Programming Boards category; Hi everybody i tried to run a simple code to find the mean value of an array using gsl library ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    8

    unexpected result

    Hi everybody
    i tried to run a simple code to find the mean value of an array using gsl library with eclipse. but i got "0" as a result. i would appreciate any help or advices.
    the code is below with header file.

    The code is
    #include <iostream>
    #include <cstdlib>
    #include <ctime> // used for other parts of code
    #include <cmath> // used fr other part of code
    #include <gsl/gsl_statistics_double.h>
    using namespace std;

    double foo(double *data)
    {
    size_t data_size = sizeof(data)/sizeof(data[0]);
    return gsl_stats_mean(data, 1, data_size);
    }

    int main()
    {
    double k;
    double mod_data[] = {4.88,3.56,3.81,4.01,2.5,3.71,4.8,5.2};
    k=foo(mod_data);
    cout<<k<<endl;
    system("pause");
    return 0;
    }

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    Without knowing anyting about gsl, I can tell you that

    size_t data_size = sizeof(data)/sizeof(data[0]);

    will not give you what you are expecting.

    Code:
    #include <iostream>
    using namespace std;
    
    void foo(double *data)
    {
      cout << "sizeof(data) in foo(): " <<  sizeof(data) << "\n"
           << "sizeof(data[0]) in foo(): " << sizeof(data[0]) << endl;
    }
    
    int main()
    {
      double mod_data[] = {4.88,3.56,3.81,4.01,2.5,3.71,4.8,5.2};
      cout << "sizeof(mod_data) in main(): " << sizeof(mod_data) << endl;
      foo(mod_data);
      return 0;
    }
    In main(), sizeof(mod_data) gives you the size, in bytes, of mod_data. In foo(), sizeof(data) gives you the size of a pointer, while sizeof(data[0]) gives you the size of that element.

    sizeof() doesn't know anything about the array outside of the scope in which the array was defined, so you'll need to calculate the size of mod_data before hand and send it to foo() along with the array.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    thank you very much for the replay.
    actually what i want to do is to find out length of the vector or array automatically, but i can not do that and i tried many ways to find that like read file in c++ but it is still not clear for me.
    could you please give me advices or simple example to do that......

    thanks in advance

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    You cannot find out the length of arrays automatically.
    You will need to either pass in size or use a container such as std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    Code:
    int main() {
      double mod_data[] = {4.88,3.56,3.81,4.01,2.5,3.71,4.8,5.2};
      size_t data_size = sizeof(mod_data)/sizeof(mod_data[0]);
      return 0;
    }
    Here, data_size will be what you expect because sizeof(mod_data) was used in the same block that mod_data[] was defined, so sizeof() knows enough about it. You could then follow it up with a call to

    double k = foo(mod_data, data_size);

    if you change the foo() function appropriately to take the data_size as a parameter instead of trying to calculate it. Assuming you are using the gsl function properly, k would then be what you expect. The point was that using sizeof() to calculate the size of an array defined in a different block won't work. (Remember, a block is enclosed in { and } ).

    Since this is C++, you should look at using std::vector<double> for your mod_data type. This would allow you to pass only the mod_data to a function and get its size with

    mod_data.size()

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    Quote Originally Posted by Elysia View Post
    You cannot find out the length of arrays automatically.
    You will need to either pass in size or use a container such as std::vector.
    or create a template function where the size of the static array is a template parameter.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Ah, that is a good idea.
    Something like this then:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime> // used for other parts of code
    #include <cmath> // used fr other part of code
    #include <gsl/gsl_statistics_double.h>
    using namespace std;
    
    template<typename T, int N> double foo(T data[N])
    {
        size_t data_size = sizeof(data); // N elements
        return gsl_stats_mean(data, 1, data_size);
    }
    
    int main()
    {
        double k;
        double mod_data[] = {4.88,3.56,3.81,4.01,2.5,3.71,4.8,5.2};
        k=foo(mod_data);
        cout<<k<<endl;
        system("pause");
        return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    I don't think you can pass the array directly, as it would decay to a pointer, and you would lose the static size. You need to pass the array by reference:

    Code:
    #include <iostream>
    #include <gsl/gsl_statistics_double.h>
    
    template<size_t N>
    double foo(double (&data)[N]) {
       return (gsl_stats_mean(data, 1, N));
    }
    
    int main() 
    {
       double data[] = { 4.88, 3.56, 3.81, 4.01, 2.5, 3.71, 4.8, 5.2 };
       double k = foo(data);
       std::cout << k << std::endl;
       std::cin.get();
    }

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    Hmm.I cannot be sure of the why right now, but a mix of the two works fine:
    Code:
    #include <iostream>
    
    template<typename T, size_t N>
    double foo(T (&data)[N])
    {
    	std::cout << "Size: " << sizeof(data) << std::endl;
    	return 0.0;
    }
    
    int main()
    {
        double k;
        double mod_data[] = {4.88,3.56,3.81,4.01,2.5,3.71,4.8,5.2};
        k=foo(mod_data);
        return 0;
    }
    Output: Size: 64
    Of course, it works without the T, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    I didn't include T in the template because gsl_stats_mean takes a const double* as its first parameter, so passing any other type would produce a compiler error anyway.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,920
    No doubt. I'm just putting it there to show the possibility.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    Thank you very much guys, all these information are good and useful.
    i am now find out way to calculate the length of a vector by reading data file as follow:
    #include <iostream>
    #include <fstream>
    #include <gsl/gsl_vector.h>
    #define MAX 200
    double x, data,ff[MAX];
    FILE * input = fopen("example.dat", "r");
    int i = 0,j;
    while (fscanf (input, "%lf", &data) != EOF) ++i;
    cout<<i;
    fclose(input);
    gsl_vector* v = gsl_vector_alloc(i);
    FILE * input2 = fopen("buff.dat", "r");
    gsl_vector_fscanf(input2,v);
    fclose(input2);
    for(j=0;j<i;j++)
    {
    ff[j]=gsl_vector_get(v,j);
    cout<<"ff[ "<<j<<" ]= "<<ff[j]<<endl;
    }
    system("pause");
    return 0;
    }
    i would appreciate if be some of you try to improve the code .
    Thanks

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by abotaha View Post
    Thank you very much guys, all these information are good and useful.
    i am now find out way to calculate the length of a vector by reading data file as follow:

    i would appreciate if be some of you try to improve the code .
    Thanks
    Code:
    int main()
    {
    }
    There ... vastly improved! Now it actually compiles!

    Other than that, we don't write your code for you, we just help you to get it right on your own.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Nov 2009
    Posts
    8
    yes iMalc....thank you fo your comments.
    I am now trying to find way how to read and print a matrix from data file without knowing its dimension, but it seems to me very difficult. Is there any way please to do that. ...
    thanks.

  15. #15
    Registered User
    Join Date
    Nov 2009
    Location
    Maryland, USA
    Posts
    46
    There's a problem here:
    Code:
    double x, data,ff[MAX];
    FILE * input = fopen("example.dat", "r");
    int i = 0,j;
    while (fscanf (input, "%lf", &data) != EOF) ++i;
    The same double pointer is being passed to fscanf() in every loop. So it is overwriting the previous value leaving only the last one read. Maybe you meant something like this:
    Code:
    while (i < MAX  &&  fscanf(input, "%lf", &ff[i++]))
        ;
    fscanf() returns the number of items scanned, not EOF, so it will keep looping until it's doesn't read a value, or i equals MAX.

    As for reading unknown data, I always fread() it into a big buffer of char or unsigned char and examine each byte and convert the appropriate substrings with strtol() or strtod().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 10:36 AM
  2. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 12:29 PM
  3. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 02:29 PM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM

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