Any Smart guy can figure this out? Should be simple problem.

This is a discussion on Any Smart guy can figure this out? Should be simple problem. within the C Programming forums, part of the General Programming Boards category; I am getting this error when want to compile. Code: /tmp/ccmvPe9e.o: In function `stdData': problem.c:(.text+0x56b): undefined reference to `sqrt' collect2: ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    17

    Any Smart guy can figure this out? Should be simple problem.

    I am getting this error when want to compile.
    Code:
    /tmp/ccmvPe9e.o: In function `stdData':
    problem.c:(.text+0x56b): undefined reference to `sqrt'
    collect2: ld returned 1 exit status


    And this is the "StdDate" below. Where is the problem? sqrt???

    Code:
    
    double stdData (double x[], int n)/* function to find the standard deviation */
    
    
    {
            int i;
    
    
            double sum = 0, sum1 = 0;
    
    
            double sum2, std;
    
    
            for (i=0; i<n; i++){
    
    
                    sum1 = sum1 + (x[i]*x[i]);
    
    
                    sum = sum + x[i];
    
    
            }
    
    
            sum2 = (sum * sum)/(n*n);
    
    
            std = sqrt(sum1- sum2);
    
    
            return (std);
    
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you included math.h?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    Have you included math.h?
    Yes. I did do the math.h Could you look at my code and see if you can fix it??

    Code:
      Descrition: Implement a menu managing array data such that the program asks multiple questions to the user and executes the operation using the method he chooses.
    */
    
    
    #include <stdio.h>
    #include <math.h>
    
    
    /*function definitions*/
    double sumData (double x[], int n);//returns the sum of the given set.
    double medianData (double x[], int n);//returns the median of the given set.
    double stdData (double x[], int n);// returns the standad derivation of the given set.
    double rangeData (double x[], int n);//returns the range of the given set.
    void freqData (double x[], int n);//displays the frequency distribution in the given ranges.
    double meanData(double x[], int n);//returns the mean of the data set.
    void setData(double x[], int n);// returns the array as a whole.
    void bubble_sort(double x[], int n);//to search the array.
    
    int main()
    {
            int size=100;
            double array[size];
            int i, x, c;
    
    
            printf ("Please enter the size of you array:");
            scanf ("%d", &size);
    
    
            for (i=0; i<size; i++) {
                    printf("Enter element %d of the array:", i);
                    scanf("%lf", &array[i]);
            }
    
    
            printf("Please select a number among the following operations:\n");
            printf("1.Display the data set\n2.Calculate the mean and");
            printf("sum of the data set\n3.Calculate the median of the data set\n");
            printf("4.Calculate the standard deviation of the data set\n5.");
            printf("Find range of the data set\n6.Display a frequency distribution\n");
            printf("7.Quit\n");
            scanf("%d", &c);
         while (c<1 || c>7) {
                    printf("\aYou have entered an ivalid number; try again.\n");
                    printf("Please select a number among the following operations:\n");
                    printf("1.Display the data set\n2.Calculate the mean and");
                    printf("sum of the data set\n3.Calculate the median of the data set\n");
                     printf("4.Calculate the standard deviation of the data set\n5.");
                    printf("Find range of the data set\n6.Display a frequency distribution\n");
                    printf("7.Quit\n");
                    scanf("%d", &c);
            }
            if (c==1) {
                    setData (array, size); //call the function to display the array.
            }
            else if (c==2){// displays both the mean and sum.
                    meanData(array, size);
                    sumData(array, size);
                    printf("mean of the data set:%f\n the sum is:%f\n", meanData(array, size), sumData(array, size));
            }
     else if (c==3){//displays median
                    medianData(array, size);
                    printf("median of the data set: %f\n", medianData(array, size));
    
    
            }
            else if (c==4){
                    stdData(array, size); //to display the deviation.
            printf("The standard deviation of the data set:%f\n", stdData(array, size));
            }
            else if (c==5){// Printing the range on the screen.
                    rangeData(array, size);
                    printf("The range of this data set is:%f\n", rangeData(array, size));
            }
            else if (c==6){//returning the frequency in interval ranges.
                    freqData(array, size);
            }
            else if (c==7){//Quit the program.
                    printf("The program ends here.\n");
            }
            return 0;
    }
    /*function definitions*/
    
    
    void setData (double x[], int n)/* function to display the array pr set data*/
    
    
    {
    
    
            int i;
    
    
            for (i=0; i<n; i++){
                    printf("%f ", x[i]);
            }
    }
    
    
    double meanData( double x[], int n)/* function to calculate the mean*/
    
    
    {
    
    
            int i;
    
    
            double sum =0;
            for (i=0; i<n; i++){
                    sum = sum + x[i];
            }
            return (sum/n);
    
    
    }
    
    
    double sumData( double x[], int n)/*function to calculate the sum*/
    
    
    {
    
    
            int i;
    
    
            double sum = 0;
    
    
            for (i=0 ;i<n; i++){
                    sum = sum + x[i];
            }
    
    
            return (sum);
    
    
    }
    
    
    void bubble_sort( double x[], int n)/* function to sort data in increasing order */
    
    
    {
    
    
            int i,j;
    
    
            double temp;
    
    
            for (i=0; i<n-1; i++){
                    for (j=i+1; j<n; j++){
    
    
                            if ( x[i] > x[j]){
    
    
                                    temp = x[i];
    
    
                                    x[i] = x[j];
    
    
                                    x[j] = temp;
    
    
                            }
    
    
                    }
    
    
            }
    }
    
    
    double medianData (double x[], int n) /*function to calculate the median of the data set */
    
    
    {
    
    
            double Middle;
            int h1, h2;
    
    
            bubble_sort(x,n);
    
    
            if (n%2 == 0){/*if n is even */
    
    
                    h1 = n/2;
                    h2 = (n/2) - 1;
    
    
                    Middle = ((x[h1] + x[h2])/2);
    
    
            }
    
    
            else { //if n is odd
                    Middle = x[n/2];
    
    
            }
    
    
            return ( Middle );
    
    
    }
    
    
    double stdData (double x[], int n)/* function to find the standard deviation */
    
    
    {
    
    
            int i;
    
    
            double sum = 0, sum1 = 0;
    
    
            double sum2, std;
    
    
            for (i=0; i<n; i++){
    
    
                    sum1 = sum1 + (x[i]*x[i]);
    
    
                    sum = sum + x[i];
                     
    
    
    
            }
    
    
            sum2 = (sum * sum)/(n*n);
    
    
            std = sqrt((sum1/n) - sum2);
    
    
            return (std);
    
    
    }
    
    
    double rangeData(double x[], int n)/* calculting the range of the data set */
    
    
    {
    
    
            int i;
    
    
            double max, min, range;
    
    
            max = x[0];
    
    
            min = x[0];
     for (i = 0; i<n; i++){
    
    
                    if( x[i] < min){//searching for the minimum element
    
    
                            min = x[i];
    
    
                    }
    
    
                    if (x[i] > max){//searching for the maximum element
    
    
                            max = x[i];
    
    
                    }
    
    
            }
    
    
            range = max - min;//calculating the range
            return (range);
    
    
    }
    
    
    void freqData (double x[], int n)/*displaying the frequency */
    {
    
    
            int i;
            int freq1 = 0;
            int freq2 = 0;
            int freq3 = 0;
            int freq4 = 0;
    
    
            bubble_sort(x,n);/*sorting the array values */
    
    
            for (i=0; i<n; i++){
                    if (x[i] < 0){
                            freq1 = freq1 + 1;
    
    
                    }
    
    
                    else if (x[i]>= 0 && x[i]<10){
    
    
                            freq2 = freq2 + 1;
    
    
                    }
                    else if (x[i]>= 10 && x[i]<100){
    
    
                            freq3 = freq3 + 1;
    
    
                    }
    
    
                    else {
    
    
                            freq4 = freq4 +1;
    
    
                    }
    
    
            }//displaying ranges.
    
    
            printf ("%s%13s\n", "Interval", "Frequency" );
            printf ("(-inf , 0)%13d\n", freq1);
            printf ("[0, 10)%10d\n", freq2);
            printf ("[10, 100)%9d\n", freq3);
            printf ("[100,+inf )%11d\n", freq4);
    }

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    You need to add "-lm" to your compiler command line.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by brewbuck View Post
    You need to add "-lm" to your compiler command line.
    I did and I am getting this
    gcc: lm: No such file or directory

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by smokeyangel View Post
    Hey guys I got it!!!!! Thank you so much for your times! I am including a link below for everyone for lunch. FREE IHOP lunch.
    Last edited by Salem; 11-19-2011 at 12:27 AM. Reason: link spam deleted

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    [QUOTE=kevave;1070123]Hey guys I got it!!!!! Thank you so much for your times! I am including a link below for everyone for lunch. FREE IHOP lunch.


    Ugh, marketing spam? srsly?
    Last edited by Salem; 11-19-2011 at 12:28 AM. Reason: snip quoted spam link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 06-03-2011, 11:19 AM
  2. Replies: 19
    Last Post: 09-28-2009, 01:45 AM
  3. C++ simple problem that I can't figure out
    By dvessey in forum C++ Programming
    Replies: 9
    Last Post: 05-25-2008, 10:56 AM
  4. I simple little problem but i cant figure it out
    By SebastionV3 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2006, 05:55 PM
  5. Simple code :: Cant figure out the problem...
    By pritin in forum C++ Programming
    Replies: 11
    Last Post: 09-02-2005, 04:02 AM

Tags for this Thread


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