Computing Problem

This is a discussion on Computing Problem within the C Programming forums, part of the General Programming Boards category; I am trying to code a program to solve quadratic equations. The problem is that after entering the input data, ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    9

    Computing Problem

    I am trying to code a program to solve quadratic equations. The problem is that after entering the input data, the program just seems to terminate and I can't figure out why. The code is:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(void)
    {
    
    	/* declare and initialise variables	*/
    	
    	char	again		= '\0';
    	int	control		= 0;
    
    	double	a 		= 0.0,
    		b 		= 0.0,
    		c		= 0.0,
    		radicand	= 0.0,
    		root		= 0.0,
    		x1 		= 0.0,
    		x2		= 0.0,
    		real		= 0.0,
    		imag		= 0.0;
    
    	/* Make sure the quadratic coefficient is non-zero	*/
    
    	while (control == 0)
    	{
    		printf("Enter a value for \"a\": \n");
    		scanf("%d", &a);
    	
    		printf("Enter a value for \"b\": \n");
    		scanf("%d", &b);
    
    		printf("Enter a value for \"c\": \n");
    		scanf("%d", &c);
    
    		radicand = pow(b, 2) - 4 * a * c;
    	
    		if (radicand >= 0)
    		{
    			x1 = (-b + sqrt(radicand)) / (2 * a);
    			x2 = (-b - sqrt(radicand)) / (2 * a);
    			printf("x1 = %6.2lf\n", x1);
    			printf("x2 = %6.2lf\n", x2);
    		
    		}
    		else 
    		{
    			radicand = -radicand;
    			real = -b / (2 * a);
    			imag = sqrt(radicand) / (2 * a);
    			printf("x1 = %6.2lf + %6.2lf i\n", real, imag);
    			printf("x1 = %6.2lf - %6.2lf i\n", real, imag);
    		}
    
    		printf("\n\nAgain? (y/n) : ");
    		scanf("%c", &again);
    
    		if (again == 'y')
    		{
    			printf("\nExcellent!\n");
    			control = 0;
    		}
    		else
    		{
    			printf("\nThank you for using quadratic.\n");
    			control = -1;
    		}
    	}
    
    	return (0);
    }
    Does anyone know what I am doing wrong? I also tried setting the code up to use a multiple-return function but couldn't get that to work. Does anyone know of any tutorials on how to use them?

    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    printf("Enter a value for \"a\": \n");
    		scanf("%d", &a);
    	
    		printf("Enter a value for \"b\": \n");
    		scanf("%d", &b);
    
    		printf("Enter a value for \"c\": \n");
    		scanf("%d", &c);
    a, b and c are all floating point variables. Why are you treating them as integers?

    I also tried setting the code up to use a multiple-return function but couldn't get that to work. Does anyone know of any tutorials on how to use them?
    What exactly is a 'multiple-return function'? Funtions can only return one value. This isn't to say you can't have more than one return point in the function, but that only one value is ever returned when a funtion returns.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    9
    "a, b and c are all floating point variables. Why are you treating them as integers?"

    A darn good question. And probably art of my problem.

    I took a C class some time ago and the teacher told us that we could have functions that return multiples values in the form of pointers. I think she also called it "pass by reference". Kind if like this:

    Code:
    void swap(int *x, int *y); // function prototype
    
        int main(void)
        {
          int x,y;
    
          x = 10;
          y = 20;
          swap(&x,&y) // addresses passed
          return 0;
        }
    
        void swap(int *x, int *y)
        {
          int temp;
    
          temp = *x;
          *x = *y;
          *y = temp;
        }
    In the above, the x and y are returned from the function to the function call. I think. Please feel free to hit me with a clue stick.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, you can use pointers as per your example. However, it isn't considered "returning multiple variables". Rather, it's modifying the original variable, since you're passing a pointer (or reference) to that variable.
    Code:
    int foo( void )
    {
        return 5;
    }
    This returns a variable.
    Code:
    void foo( int *f )
    {
        * f = 5;
    }
    This changes the value of whatever f points at. It isn't really considered "returning" however.

    The difference of using a pointer and not using one, is that if you pass by value a variable to a function, it makes a copy of that (the value) which is destroyed when the function ends. By reference/pointer, you're giving the ability to modify the original passed variable, so it can change (potentially) inside the function.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    9
    OK, thanks. For some reason, when I was trying that in my program, the values were not being modified. I'll give it another go. Thanks again.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Its the classic scanf() problem... see

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    for a better (safer) way
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    9

    Thumbs up

    That never fails to get me. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

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