Like Tree1Likes
  • 1 Post By quzah

Computing the value of a function

This is a discussion on Computing the value of a function within the C Programming forums, part of the General Programming Boards category; Hi there, I'm having trouble getting the desired output. whenever I enter a number, my program just freezes and doesn't ...

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

    Computing the value of a function

    Hi there,

    I'm having trouble getting the desired output. whenever I enter a number, my program just freezes and doesn't do anything. if it does do something, it's always the number I input. e.g: if I put in 2, it will give me 2 in return.

    Here's the code
    Code:
    
    #include <stdio.h>
    #include <math.h>
    
    
    int main(void)
    {
      double x;
    
    
      printf("Enter a number x: ");
      scanf("%lf\n", &x);
      if ((x*x-5*x) == 0)  {
        printf("f(x) is not defined\n");
        }
      else if ((3*x + 6) < 0)  {
        printf("f(x) is not defined\n");
        }
      else (((1+(x*x)/sqrt(3*x+6))/(x*x-5*x)));  {
        printf("f(x) = %lf\n", x);
        }
      return(0);
    }
    Should I implement a function to call on? any other type of loop maybe?

    Please help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    else (((1+(x*x)/sqrt(3*x+6))/(x*x-5*x))); {
        printf("f(x) = %lf\n", x);
        }
    This is attached to the else. This happens regardless of all of your if checks.


    Quzah.
    TheBigH likes this.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Ok, so I revamped my code a little bit:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    double function(double x);
    
    
    int main()
    {
      double x;
    
    
      printf("Enter a number x: ");
      scanf("%lf\n", &x);
    
    
      if (x != 0 || x > -2)  {
        printf("f(x) = %lf\n", function(x));
        }
      else   {
        printf("f(x) is not defined\n");
        }
      return(0);
    }
    
    double function(double x)
    {
     return (((1+(x*x)/sqrt(3*x+6))/(x*x-5*x)));
    }
    So here's my problem now: It will ask me to enter a value of x, and I do. but I have to enter another number after it in order for the program to give me output. This is what it looks like:
    Code:
    Enter a number x: 2.74 //This doesn't do anything
    2.74 //This is the number that gets the program to give output
    f(x) = -0.482997
    What am I doing wrong?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Ditch the newline in the scanf.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    17
    Thank you! It works fine now.

    edit: I also changed the || to &&, and the program gives what I need now.
    Last edited by lolcats22; 10-27-2011 at 05:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me pls...About computing a grade
    By Sunday in forum C Programming
    Replies: 2
    Last Post: 11-03-2007, 12:41 PM
  2. Help with Computing Divisors
    By MrPink in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2005, 07:35 PM
  3. time computing
    By Fibre Optic in forum C++ Programming
    Replies: 3
    Last Post: 09-17-2005, 04:58 PM
  4. Meccano Computing
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-29-2004, 01:51 PM
  5. computing a HUUUGEE value...
    By moonwalker in forum C Programming
    Replies: 8
    Last Post: 08-20-2002, 09:36 PM

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