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
    Code:
    /tmp/ccgjEovl.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);
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    That's a linker error -- the code compiled ok. Have you told the linker to link with the math library? Try adding "-lm" to the end of your gcc command if you're invoking the linker via gcc, or the link command if you're invoking ld directly.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Quote Originally Posted by smokeyangel View Post
    That's a linker error -- the code compiled ok. Have you told the linker to link with the math library? Try adding "-lm" to the end of your gcc command if you're invoking the linker via gcc, or the link command if you're invoking ld directly.

    How can I "Have you told the linker to link with the math library?" can you show me? using the code? I added the lm at the end and says no such as file exits

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    Wow, triple post...

    Any Smart guy can figure this out? Should be simple problem.
    Problem with my code. C programming C++ Anyone can look at mine?

    Looks like you've got it sorted. This wasn't a problem with your code, just a problem with the options you were passing to GCC.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    Indent your code.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-19-2011, 12:29 AM
  2. mine sweeper help..
    By transgalactic2 in forum C Programming
    Replies: 8
    Last Post: 01-04-2009, 10:18 PM
  3. mine sweeper code help..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 01-01-2009, 08:44 AM
  4. 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
  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