Problem with my code. C programming C++ Anyone can look at mine?

This is a discussion on Problem with my code. C programming C++ Anyone can look at mine? within the C Programming forums, part of the General Programming Boards category; This is my code and when I gcc it it won't let me and gives me these below errors. Code ...

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

    Problem with my code. C programming C++ Anyone can look at mine?

    This is my code and when I gcc it it won't let me and gives me these below errors.
    Code below the error I am getting trying to compile it

    This is the error I am getting
    Code:
     
    Code:
    tmp/ccS5besW.o: In function `stdData':
    problem.c:(.text+0x570): undefined reference to `sqrt'
    collect2: ld returned 1 exit status



    And this is the code. Anybody can fix the problem?

    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);
    }
    Last edited by kevave; 11-18-2011 at 08:55 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Add "-lm" to the gcc command line (there has to be a space before "-lm" and a space after it, unless it is at the end of the command line). That will cause the gcc driver to link your program against the math library. For various historical reasons, gcc and other compilers from the unix world do not link in the math library by default.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,554
    undefined reference to `sqrt'
    The "undefined reference" means linker error; the "sqrt" implies you have failed to link in the math library.
    Note: The math library is often called just the letter "m".

    Tim S.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by grumpy View Post
    Add "-lm" to the gcc command line (there has to be a space before "-lm" and a space after it, unless it is at the end of the command line). That will cause the gcc driver to link your program against the math library. For various historical reasons, gcc and other compilers from the unix world do not link in the math library by default.
    Where to add the "-lm"??? this is my gcc -->>> gcc problem.c -o problem.exe

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    Virtually anywhere you like after the "gcc" hence my comment in brackets.

    The worst that will happen if you get it wrong is that you will get an error complaining about it.

    Try "gcc problem.c -o problem.exe -lm" and see how that goes.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by stahta01 View Post
    The "undefined reference" means linker error; the "sqrt" implies you have failed to link in the math library.
    Note: The math library is often called just the letter "m".

    Tim S.
    What do you mean? Can you copy my code and fix it and explain where is linker and sqrt error?

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by grumpy View Post
    Virtually anywhere you like after the "gcc" hence my comment in brackets.

    The worst that will happen if you get it wrong is that you will get an error complaining about it.

    Try "gcc problem.c -o problem.exe -lm" and see how that goes.
    It gives me this error
    Code:
    gcc: lm: No such file or directory

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,554
    The below command worked for me; are you sure you did NOT forgot the "-" dash?
    Note: That is a lower case "L" before the letter "m".

    Code:
    gcc problem.c -o problem.exe -lm
    Tim S.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mine sweeper help..
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 01-04-2009, 10:18 PM
  2. mine sweeper code help..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 01-01-2009, 08:44 AM
  3. a C++ library of mine.
    By jinhao in forum A Brief History of Cprogramming.com
    Replies: 72
    Last Post: 09-13-2008, 02:19 PM
  4. Using other programs in mine
    By mramazing in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2006, 02:19 AM
  5. Another game of mine...
    By valar_king in forum Game Programming
    Replies: 7
    Last Post: 01-03-2002, 08:44 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