square root solver program

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3004 Views - Last Post: 01 March 2011 - 09:04 AM Rate Topic: -----

#1 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

square root solver program

Posted 28 February 2011 - 08:04 PM

hi everyone

im having trouble compiling this program. its basically finished but when compiling it fails and gives me the error: line 32 expected expression before 'else.'
I can't seem to find anything wrong with my code or logic, maybe someone here can point it out?

Quote

Write a program that will solve the quadratic equation, ax2 + bx + c = 0. Your program should prompt the user to enter coefficients a, b and c, and then print the roots of the equation. Use standard Electrical Engineering notation for complex numbers.

Following is a sample run:

Enter a: 4.4
Enter b: 5.5
Enter c: 3.3

There are two complex roots:
root1 = -0.62 + j0.60
root2 = -0.62 - j0.60

Your program must meet the following specifications.
• if coefficient a is zero and b is not zero, print "This is a linear equation. The root is ..."
• if both a and b are zero, print "This is not an equation."
• Print all real numbers with 2 digits to the right of the decimal point.
• Be sure to handle the cases where both roots are real (i.e., b2-4ac > 0) and where there is a double real root (i.e., b2-4ac = 0), as well as where there are two complex roots as in the sample run above (i.e., b2-4ac < 0).
• When printing a complex root with negative imaginary part, be sure to put the minus sign before the j. That is, do not print root2 above as -0.62 + j-0.60 .


#include <stdio.h>
#include <math.h>

main ()
{
	float a, b, c, d, x, y, root1, root2;
	
	printf("Enter a: ");
	scanf("%f", &a);
	printf("Enter b: ");
	scanf("%f", &B)/>;
	printf("Enter c: ");
	scanf("%f", &c);
	
	if (a == 0 && b != 0) {
		printf("This is a linear equation. The root is: %.2f", -c / B)/>;
	} else if (a == 0 && b == 0) {
		printf("This is not a quadratic equation.\n");
	} else {
		d = (b * B)/> - (4 * a * c);
		x = -b / (2 * a);
		
		if (d < 0) {
			
			d = d * -1;
			y = sqrt(d) / (2 * a);
			
			printf("\nThere are two complex roots:\n");
			printf("root1 = %.2f + j%.2f\n", root1, root2);
			printf("root2 = %.2f - j%.2f\n\n", root1, root2);
		}
	} else if (d == 0) {
		y = sprt(d) / (2 * a);
		root1 = x + y;
		printf("There is one root: \n");
		printf("root = %.2f\n", root1);
	} else if (d > 0) {
		y = sqrt(d) / (2 * a);
		root1 = x + y;
		root2 = x - y;
		printf("There are two real roots: ");
		printf("root1 = %.2f\n", root1);
		printf("root2 = %.2f\n", root2);
	}
	return 0;
}




Any help or insight will be much appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: square root solver program

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 08:14 PM

The order of those statements is supposed to be
if
else if
...
else if
else (optional)
with "else" (if it's there at all) being the last in a series.

You have
if
else if
else
else if
else if

That's why the compiler is complaining.

This post has been edited by r.stiltskin: 28 February 2011 - 08:16 PM

Was This Post Helpful? 1
  • +
  • -

#3 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 08:23 PM

View Postr.stiltskin, on 28 February 2011 - 07:14 PM, said:

The order of those statements is supposed to be
if
else if
...
else if
else (optional)
with "else" (if it's there at all) being the last in a series.

You have
if
else if
else
else if
else if

That's why the compiler is complaining.


the order I have it in is:

if
else if
else
if (within previous else)
else if
else if

i think this is allowed.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 08:28 PM

Yes, I realize that that is what you intended -- but you have one of your closing braces misplaced, resulting in the order that I described.

By the way, I would be delinquent if I didn't also say that you should make main's return type explicit: it should return an int.

And, you are using root1 and root2 in a printf statement before those variables are initialized.

And finally, you want "sqrt", not "sprt"

This post has been edited by r.stiltskin: 28 February 2011 - 08:30 PM

Was This Post Helpful? 1
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: square root solver program

Posted 28 February 2011 - 08:28 PM

For your error:

Quote

main.c|32|error: ‘else’ without a previous ‘if’|

After looking at the logic of your program it appears that the closing brace } on line 31 is in the wrong location. It probably should be on inserted between the closing brace on line 44 and the return 0 statement.

What the error message is actually saying is that you have a closing else statement on line 19 and then you have another else statement on line 32 with no if statement in between these 2 else statements (the else on 19 closing brace is on line 31). Every else statement must belong to an if statement.


Jim
Was This Post Helpful? 1
  • +
  • -

#6 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 08:35 PM

View Postr.stiltskin, on 28 February 2011 - 07:28 PM, said:

Yes, I realize that that is what you intended -- but you have one of your closing braces misplaced, resulting in the order that I described.

By the way, I would be delinquent if I didn't also say that you should make main's return type explicit: it should return an int.

And, you are using root1 and root2 in a printf statement before those variables are initialized.

And finally, you want "sqrt", not "sprt"


gosh!! i can't stop making the sprt mistake, its soo annoying. Thanks rumble stiltskin, I will make the appropriate corrections

View Postjimblumberg, on 28 February 2011 - 07:28 PM, said:

For your error:

Quote

main.c|32|error: ‘else’ without a previous ‘if’|

After looking at the logic of your program it appears that the closing brace } on line 31 is in the wrong location. It probably should be on inserted between the closing brace on line 44 and the return 0 statement.

What the error message is actually saying is that you have a closing else statement on line 19 and then you have another else statement on line 32 with no if statement in between these 2 else statements (the else on 19 closing brace is on line 31). Every else statement must belong to an if statement.


Jim


wow...surprisingly, I completely understood what you just said.

EDIT:
perhaps I did not totally understand what you're saying but after looking at my code and my IDE telling me, i no longer believe my braces are misplaced. xcode is telling me line 31 is needed to close the if statment nested within the else statement. perhaps something else maybe causing the error?

This post has been edited by iqbalmmz: 28 February 2011 - 08:54 PM

Was This Post Helpful? 0
  • +
  • -

#7 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 08:52 PM

View Postjimblumberg, on 28 February 2011 - 07:28 PM, said:

For your error:

Quote

main.c|32|error: ‘else’ without a previous ‘if’|

After looking at the logic of your program it appears that the closing brace } on line 31 is in the wrong location. It probably should be on inserted between the closing brace on line 44 and the return 0 statement.

What the error message is actually saying is that you have a closing else statement on line 19 and then you have another else statement on line 32 with no if statement in between these 2 else statements (the else on 19 closing brace is on line 31). Every else statement must belong to an if statement.


Jim


Okay I totally understand what you're saying but after looking at my code and my IDE telling me, i no longer believe my braces are misplaced. xcode is telling me line 31 is needed to close the if statment nested within the else statement. perhaps something else maybe causing the error?
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 09:02 PM

The closing brace that you have at the beginning of line 32 doesn't belong there. It belongs immediately before the return statement.
Was This Post Helpful? 1
  • +
  • -

#9 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 09:31 PM

View Postr.stiltskin, on 28 February 2011 - 08:02 PM, said:

The closing brace that you have at the beginning of line 32 doesn't belong there. It belongs immediately before the return statement.


okay, I fixed it. It now compiles but now I think I have another error, either syntax or logic.

when I run this:
#include <stdio.h>
#include <math.h>

main ()
{
	float a, b, c, d, x, y, root1, root2;
	
	printf("Enter a: ");
	scanf("%f", &a);
	printf("Enter b: ");
	scanf("%f", &B)/>;
	printf("Enter c: ");
	scanf("%f", &c);
	
	if (a == 0 && b == 0) {
		printf("Not a quadratic equation!\n");
	}
	else if(a == 0) {
		printf("This is a linear equation. The root is: %.2f\n", (-c / B)/>);
	}
	else {
		d = (b * B)/> - (4 * a * c);
		x = -b / (2 * a);
		
		if (d < 0) {
			d = d * -1;
			y = sqrt(d) / (2 * a);
			
			printf("\nThere are two complex roots:\n");
			printf("root1 = %.2f + j%.2f\n", root1, root2);
			printf("root2 = %.2f - j%.2f\n\n", root1, root2);
		} else if (d == 0) {
		y = sqrt(d) / (2 * a);
		root1 = x + y;
		printf("There is one root: \n");
		printf("root = %.2f\n", root1);
	} else if (d > 0) {
		y = sqrt(d) / (2 * a);
		root1 = x + y;
		root2 = x - y;
		printf("There are two real roots: ");
		printf("root1 = %.2f\n", root1);
		printf("root2 = %.2f\n", root2);
		}
	}return 0;
}



I get the output:

Quote

Enter a: 1
Enter b: 1
Enter c: 1

There are two complex roots:
root1 = 0.00 + j0.00
root2 = 0.00 - j0.00


the output should be:

Quote

Enter A: 1
Enter B: 1
Enter C: 1
There are two complex roots:
root1 = -0.50 + j0.87
root2 = -0.50 - j0.87


Please tell me where I need to fix my code??
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 10:03 PM

On lines 29 and 30 you are printing the values of root1 and root2. But where did you assign any values to them?
Was This Post Helpful? 1
  • +
  • -

#11 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 10:07 PM

View Postr.stiltskin, on 28 February 2011 - 09:03 PM, said:

On lines 29 and 30 you are printing the values of root1 and root2. But where did you assign any values to them?


no i didn't, I just set root1 to be the sum of x and y and root2 as x - y (x and y are defined). So are you implying I should initialize root1 and root2? Sorry, I must say I am a complete amateur to programming.

This post has been edited by iqbalmmz: 28 February 2011 - 10:08 PM

Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 10:16 PM

Maybe I'm going blind, but the only assignments to root1 and root2 that I see are on lines 39 and 40 so those would apply to the print statements on lines 42 and 43. But where do you assign any values to be printed on lines 30 and 31. They are going to remain undefined until you get a situation with real roots, and then those values will continue to be carried over until there is another set of real roots, and so on. I don't see any assignments for complex roots. Am I missing something?
Was This Post Helpful? 1
  • +
  • -

#13 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 10:28 PM

View Postr.stiltskin, on 28 February 2011 - 09:16 PM, said:

Maybe I'm going blind, but the only assignments to root1 and root2 that I see are on lines 39 and 40 so those would apply to the print statements on lines 42 and 43. But where do you assign any values to be printed on lines 30 and 31. They are going to remain undefined until you get a situation with real roots, and then those values will continue to be carried over until there is another set of real roots, and so on. I don't see any assignments for complex roots. Am I missing something?


the complex roots are root1 and root2, they're just written in EE (electrical engineering) format, you probably know this already. you're right about the assignments on lines 42 and 43 but what does that say? there is however an assignment to root1 on line 34. like I can't print out values for root1 and root2 before they have been assigned any values?
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: square root solver program

Posted 28 February 2011 - 10:47 PM

Exactly.

First of all as a general rule you can't print out the value of a variable before you have assigned a value to that variable.

In this case, on lines 30 and 31 you are CLAIMING to be printing the complex roots, but you NEVER assign the values of the complex roots to root1 and root2. The assignment on line 34 has nothing to do with complex roots -- why are you bringing that up now?
Was This Post Helpful? 0
  • +
  • -

#15 iqbalmmz   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 94
  • Joined: 15-February 11

Re: square root solver program

Posted 28 February 2011 - 10:58 PM

View Postr.stiltskin, on 28 February 2011 - 09:47 PM, said:

Exactly.

First of all as a general rule you can't print out the value of a variable before you have assigned a value to that variable.

In this case, on lines 30 and 31 you are CLAIMING to be printing the complex roots, but you NEVER assign the values of the complex roots to root1 and root2. The assignment on line 34 has nothing to do with complex roots -- why are you bringing that up now?


i'm just trying to throw out anything that might yield something of worth. so i moved root1 and 2 above and into the if statement. now i am actually getting an answer but it's not correct.

here is my code now:
/*	Lab 1.c Practice if...else
 *  Created by Iqbal on 2/27/11.
 *  Copyright 2011 __CompE 160: Intro to Programming__. All rights reserved.
 
 Write a program that will solve the quadratic equation, ax^2 + bx + c = 0. Your program should 
 prompt the user to enter coefficients a, b and c, and then print the roots of the equation.
 Use standard Electrical Engineering notation for complex numbers. Following is a sample run:
 
 Enter a: 4.4
 Enter b: 5.5
 Enter c: 3.3
 There are two complex roots:
 root1 = -0.62 + j0.60
 root2 = -0.62 - j0.60
 
 Your program must meet the following specifications.
 
 •	if coefficient a is zero and b is not zero, print "This is a linear equation. The root is ..." 
 •	if both a and b are zero, print "This is not an equation."
 •	Print all real numbers with 2 digits to the right of the decimal point.
 •	Be sure to handle the cases where both roots are real (i.e., b^2-4ac > 0) and where there is a double real root
 (i.e., b^2-4ac = 0), as well as where there are two complex roots as in the sample run above (i.e., b2-4ac < 0).  
 •	When printing a complex root with negative imaginary part, be sure to put the minus sign before the j. That is,
 do not print root2 above as -0.62 + j-0.60 .
 
 You will need the function sqrt in the math library.  You must include the header file math.h (i.e., #include <math.h> ).
 When using math.h, you must compile with option “-lm” (ex, if x.c is your source program, use command “cc x.c –lm” to compile.)
 The l in lm is lower-case L, not numeral 1.
 */

#include <stdio.h>
#include <math.h>

int main ()
{
	float a, b, c, d, x, y, root1, root2;
	
	printf("Enter a: ");
	scanf("%f", &a);
	printf("Enter b: ");
	scanf("%f", &B)/>;
	printf("Enter c: ");
	scanf("%f", &c);
	
	if (a == 0 && b == 0)
	{
		printf("This is not a quadratic equation.\n");
	} 
	else if (a == 0 && b != 0)
	{
		printf("This is a linear equation. The root is: %.2f", -c / B)/>;
	}
	else 
	{
		d = (b * B)/> - (4 * a * c);
		x = -b / (2 * a);
		
		if (d < 0)
		{
			
			d = d * -1;
			y = sqrt(d) / (2 * a);
			
			root1 = x + y; // moved up from line 82
			root2 = x - y; // moved up from line 83
			
			printf("\nThere are two complex roots:\n");
			printf("root1 = %.2f + j%.2f\n", root1, root2);
			printf("root2 = %.2f - j%.2f\n\n", root1, root2);
		}
	
	else if (d == 0)
	{
		y = sqrt(d) / (2 * a);
		root1 = x + y;
		printf("There is one root: \n");
		printf("root = %.2f\n", root1);
	}
	else if (d > 0)
	{
		y = sqrt(d) / (2 * a);
		root1 = x + y;
		root2 = x - y;
		printf("There are two real roots: ");
		printf("root1 = %.2f\n", root1);
		printf("root2 = %.2f\n", root2);
	}}return 0;
}



The output I get is:

Quote

Enter a: 1
Enter b: 1
Enter c: 1

There are two complex roots:
root1 = 0.37 + j-1.37
root2 = 0.37 - j-1.37


I should be getting:

Quote

Enter a: 1
Enter b: 1
Enter c: 1
There are two complex roots:
root1 = -0.50 + j0.87
root2 = -0.50 - j0.87


sorry for the redundancy. my head is about to explode
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2