Thread: Trouble making a quadratic formula code

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
2
Rep Power
0

Trouble making a quadratic formula code

For my assignment I have to create a code that does the quadratic formula, I can't seem to get it to work though.. You have to take input from the user, 3 numbers, then the roots must be calculates (2 roots) and if the number is less than zero it must say that the roots are imaginary (sqrt(0)). Here's my code, what am I doing wrong ?
Code:
```#include<stdio.h>
#include<math.h>

int main(){
float a,b,c;
int decision;
int disc;
float root_1;
float root_2;
printf("These will be calculated using the quadratic formula.\n");
scanf_s("%.1f%.1f%.1f",&a,&b,&c);										/*Entering Variables*/
printf("You have chosen %.1fx^2 + %.1fx + %.1f = 0, is this correct?\n",a,b,c);		/*Verify Selection*/
printf("y/n (Choose 1 for yes and 0 for no)\n");
scanf_s("%d",&decision);
if(decision==1){
printf("You have chosen yes. Calculating roots now.\n");
disc=(pow(b,2)-4*a*c);
if (disc>=0){						/*Checking for Imaginary roots*/
printf("Your roots are imaginary (Square root of a number less than 0.)\n");
}
else{
root_1=(-b+sqrt(disc))/(2*a);			/*Calculating First Root*/
root_2=(-b-sqrt(disc))/(2*a);			       /*Calculating Second Root*/
printf("Root one is x = %.1f.\n",root_1);
printf("Root two is x = %.1f.\n",root_2);
}}
else{
printf("You have chosen no! Please start over.\n");
}
return 0;
}```
Feedback is greatly appreciated, I'd like help working through it because i'm just learning, not just the answer please :)
2. From the scanf_s, I assume that you're using Microsoft's Visual Studio. When I create a Win32 console project in VS2008 and allow the .cpp default, I get all kinds of warnings, so I renamed it .c and I just get warnings in lines 22, 27, 28 about the assignment from double to int or to float possibly causing loss of data:
Code:
```		disc=(pow(b,2)-4*a*c);
if (disc>=0){						/*Checking for Imaginary roots*/
printf("Your roots are imaginary (Square root of a number less than 0.)\n");
}
else{
root_1=(-b+sqrt(disc))/(2*a);			/*Calculating First Root*/
root_2=(-b-sqrt(disc))/(2*a);			       /*Calculating Second Root*/```
You should probably use doubles throughout, converting to int at the last moment for display if still necessary.

What's the problem? "I can't seem to get it to work though.. " can have a near-infinite number of possible meanings so as to render those words meaningless. You need to tells us what the symptoms are, just as you would need to tell your doctor if you called him feeling sick.

I assume that you can get it to run and that it is giving you outputs that are not what you expected. So to enable us to help you, at the minimum you need to tell us what the input values are, what output values you expected to get, and what you got instead.

It might also help to tell us what compiler/IDE, OS, and language you're using.
3. Code:
```	scanf_s("%.1f%.1f%.1f",&a,&b,&c);										/*Entering Variables*/
printf("You have chosen %.1fx^2 + %.1fx + %.1f = 0, is this correct?\n",a,b,c);		/*Verify Selection*/```
Good idea to verify that the correct values were read in. Well, were they?

You are telling scanf that the numbers will be jammed up next to each other without any spaces. How is it supposed to tell where one ends and the next begins?

I normally prefer to write that as:
scanf_s("%f %f %f",&a,&b,&c);
I also don't feel that it's necessary to specify the size of the fraction. Others may disagree with me, so don't take my word for it. But give that a try if the problem, which you have not actually described to us yet, turns out to be that the verification shows that it is not reading the input as you expect.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Oct 2013
Posts
2
Rep Power
0
I assume that you can get it to run and that it is giving you outputs that are not what you expected. So to enable us to help you, at the minimum you need to tell us what the input values are, what output values you expected to get, and what you got instead.

It might also help to tell us what compiler/IDE, OS, and language you're using.
I had the code working before, I am using Visual Studio 2008 and trying to code in C, I am currently on Windows 8. When I had it working the math wasn't working correctly and would only output the vales "#I.0" or something along those lines, where I wanted it to output the roots. Your feedback is appreciated though, thank you, I will try and fix the errors you listed.
5. Exact input values, please. Exact output, please.

Or for that matter, copy and paste a run of the program. If you don't know how, right-click the caption bar of the console window, in the pop-up menu select Edit and under that submenu select Mark. The position the mouse over the upper left corner of what you want to copy, press and hold the left button, drag to the lower right corner -- it will be a rectangular region. Release the left button and press Enter to copy to the clipboard. Then go to the forum editor and paste it into your message -- I would recommend placing it within a quote box.

If all you're going to do is wave your hands and mumble some vague stuff about what you think you might remember, then nobody will be able to help you.

As for what I suggested, all of my professional C programming in the past 23 years has been embedded, so I have no practical experience with scanf (our product doesn't have a console). I'm just saying that what you have doesn't look like what I normally have seen, so you might try playing with it. If your verification output verifies that the input values were entered correctly, then your scanf should be okay. We just don't have that information to work with ... yet.