GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Apr-2005, 15:10
internetbug internetbug is offline
New Member
 
Join Date: Mar 2005
Posts: 6
internetbug is on a distinguished road
Post

::Need help Please:: using If & Switch in Quadratic equation.


i need help for this program.

in the following program, i need help using switch, so that i could choose all the possible roots for the program. such as 1) real roots
2) imaginary roots
3) repeated roots
4) exits (default)

the program.
CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>
    int main(void) {
    	double a, b, c, d, x1, x2, x;
    	printf(" Quadratic Equation ax^2+bx+c=0 \n");
    	printf("**************************************\n");
    	printf("\na = ");
    	scanf("%lf", &a);
    	printf("b = ");
    	scanf("%lf", &b);
    	printf("c = ");
    	scanf("%lf", &c);

        	if(a==0) {

            		if(b==0 && c!=0) {
            			printf("\n**************************************\n");
            			printf("The equation hasn't root(s)\n\n");
            		}

                		else if(b!=0) {
                			x=-c/b;
                			printf("\n**************************************\n");
                			printf("X=%lf\n\n", x);
                		}


                    		else if(b==0 && c==0) {
                    			printf("\n**************************************\n");
                    			printf("The equation has infinite roots\n\n");
                    		}
                    	}

                        	else {
                        		d=(b*b)-(4*a*c);


                            		if(d<0) {
                            			printf("\n**************************************\n");
                            			printf("The equation hasn't root(s)\n\n");
                            		}

                                		else if(d==0) {
                                			x=-b/(2*a);
                                			printf("\n**************************************\n");
                                			printf("X = %lf\n\n", x);
                                		}


                                    		else if(d>0) {
                                    			x=sqrt(d);
                                    			x1=(-b+x)/(2*a); x2=(-b-x)/(2*a);
                                    			printf("\n**************************************\n");
                                    			printf("\nX1 = %lf\nX2 = %lf\n\n", x1, x2);
                                    		}
                                    	}
                                    	return 0;
                                }
Last edited by internetbug : 07-Apr-2005 at 15:36. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 07-Apr-2005, 15:34
internetbug internetbug is offline
New Member
 
Join Date: Mar 2005
Posts: 6
internetbug is on a distinguished road
Need three choices for the switch:
1) real roots
2) imaginary roots
3) repeated roots
4) exits (default)
  #3  
Old 07-Apr-2005, 16:04
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline
Junior Member
 
Join Date: Apr 2005
Location: Arizona
Posts: 35
Stack Overflow will become famous soon enough
Hello,

The switch statement
The switch statement is a multi-way decision that tests whether an expression matches one of a number or constant integer values, and branches accordingly:

CPP / C++ / C Code:
switch (expression) {
	case const-expr:	statements
	case const-expr:	statements
	default:	statements
}

Each case is labeled by one or more integer-valued constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.

The break statement causes an immediate exit from the switch. Because cases serve just a labels, after the code or one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch. A break statement can also be used to force an immediate exit from while, for, and do loops.

Here's an example of using a switch statement in your program:

CPP / C++ / C Code:
#include <stdio.h>
#include <math.h>

/* Prototypes */
void realRoots(double, double, double);
void imaginaryRoots(double, double, double);

int main() {
	/* Local variables */
	int option;
	double a, b, c, d;

	printf(" Quadratic Equation ax^2+bx+c=0 \n");
	printf("**************************************\n");

	/* Print list */
	printf("\n1) Real roots\n");
	printf("2) Imaginary roots\n");
	printf("3) Repeated roots\n");
	printf("4) Exit\n");

	/* Get option from user */
	scanf("%d", &option);

	printf("**************************************\n");

	/* Get a, b, and c from user */
	printf("\na = ");
	scanf("%lf", &a);
	printf("b = ");
	scanf("%lf", &b);
	printf("c = ");
	scanf("%lf", &c);

	/* Calculate determinant */
	d = (b * b) - (4 * a * c);
	
	/* Use switch statement */
	switch(option) {
	/* If 1 was selected */
	case 1:
		realRoots(a, b, sqrt(d));
		break;
	/* If 2 was selected */
	case 2:
		imaginaryRoots(a, b, sqrt(-d));
		break;
	/* If 3 was selected */
	case 3:
		break;
	/* If 4 was selected */
	case 4: 
		break;
	}

	/* End program */
	return 0;
}

/* Evaluate real roots */
void realRoots(double a, double b, double d) {
	/* Calculate */
	double firstRoot = (-b/(2 * a)) + (d/(2 * a));
	double secondRoot = (-b/(2 * a)) - (d/(2 * a));
	/* Print */
	printf("\nFirst Real Root: \t%lf\n", firstRoot);
	printf("\nSecond Real Root: \t%lf\n", secondRoot);
}

/* Evaluate imaginary roots */
void imaginaryRoots(double a, double b, double d) {
	/* Calculate */
	double x = 2 * a;
	double first_term = (-b)/x;
	double second_term = (d)/x;
	/* Print */
	if(second_term >= 0) {
		printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
		printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
	}else {
		second_term = -(second_term);
		printf("\nFirst Imaginary Root: \t%lf + %lfi\n", first_term, second_term);
		printf("\nSecond Imaginary Root: \t%lf - %lfi\n", first_term, second_term);
	}
}

I didn't add the Repeated roots function, so you can have something to do. I also split the program into different functions. If you have questions, please feel free to ask.


- Stack Overflow
__________________
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [C] / [C++] tags. Your question may have been asked before, try the search facility.
  #4  
Old 07-Apr-2005, 17:38
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by internetbug
i need help for this program.

in the following program, i need help using switch, so that i could choose all the possible roots for the program. such as 1) real roots
2) imaginary roots

If a is equal to zero, it's not a quadratic equation and you have handled that.

For a not equal to zero,your conditional statements (d < 0), (d == 0), and (d > 0), tell you whether there are complex roots, repeated real roots, and distinct real roots, respectively.

Why do you think you need a switch statement? What value would that add to your program?

Regards,

Dave
  #5  
Old 07-Apr-2005, 17:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Stack Overflow
Hello,

Here's an example of using a switch statement in your program:

How is the user supposed to know the type of roots his equation has? What if he gives the coefficients for an equation that has complex roots, but tells the program to find real roots (or vice versa)?

Shouldn't the program at least check before trying to take the square root of a negative number? But, wait a minute: if the program can check to see whether the roots are real or complex, then why make the user tell it in the first place? I guess I missed the point.

Regards,

Dave
  #6  
Old 07-Apr-2005, 18:03
internetbug internetbug is offline
New Member
 
Join Date: Mar 2005
Posts: 6
internetbug is on a distinguished road
Quote:
Originally Posted by davekw7x
If a is equal to zero, it's not a quadratic equation and you have handled that.

For a not equal to zero,your conditional statements (d < 0), (d == 0), and (d > 0), tell you whether there are complex roots, repeated real roots, and distinct real roots, respectively.

Why do you think you need a switch statement? What value would that add to your program?

Regards,

Dave

you are right. but this is a Hw problem, and i really needed help using switch statements, cause we had to use switch statements for this problem, to make a choice kind a problem.


thank you Stack overflow for the help. it works.
  #7  
Old 07-Apr-2005, 18:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by internetbug
you are right. but this is a Hw problem, and i really needed help using switch statements, cause we had to use switch statements for this problem, to make a choice kind a problem.


thank you Stack overflow for the help. it works.

At least I was correct in my response to Stack Overflow:

Quote:
Originally Posted by davekw7x
I guess I missed the point.

Sorry.

Regards,

Dave
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with switch statement dontcare C++ Forum 4 29-Nov-2004 19:28
problem with switch case if13121 C Programming Language 3 22-Nov-2004 22:26
Re: Command Line Arguments, Part 1 WaltP C Programming Language 0 11-Jul-2004 00:34
Help a C++ Idiot: I am trying to fill an array based on a switch statement. Psycop C Programming Language 2 14-Apr-2004 04:12
Microsoft Equation conkermaniac Computer Software Forum - Windows 1 15-Feb-2003 12:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.