Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Brief Description of Code: It's a quadratic equation calculator. It helps you find the roots of an equation.
Code:
#include <stdio.h>
#include <math.h>
main(){
int a, b, c, real;
float root1, root2, img, dis;
char solve;
printf("Do you want to solve an equation (y/n): ");//Ask user if they want to solve an equation
scanf("%c", &solve);
if(solve == 'n'){//Terminate program
return 0;
}
if(solve == 'y'){//Code for calculation
printf("\nInput the number");
printf("\n````````````````");
printf("\nA: ");//Store number for a, b, c for the quadratic formula
scanf("%d", &a);
printf("\nB: ");
scanf("%d", &b);
printf("\nC: ");
scanf("%d", &c);
dis = (b*b) - (4*a*c);//calculation for the discriminent
//printf("%f", dis); Check the discriminant value
if(dis > 0){//Calculation for the real root
root1 = ((b*-1) + sqrt(dis))/(2*a);
root2 = ((b*-1) - sqrt(dis))/(2*a);
printf("\nRoot 1: %.2f", root1);
printf("\nRoot 2: %.2f", root2);
return 0;
}
if(dis = 0){//Calculation for no discriminent
root1 = (b*-1)/(2*a);
printf("\nRoot 1 and 2: %.2f", root1);
return 0;
}
if(dis < 0){//Calculation for complex root
dis = dis * -1;
//printf("\n%f", dis); !!!Testing to see why the code isn't functioning!!! It skipped this
root1 = (b*-1)/(2*a);
img = (sqrt(dis))/(2*a);
printf("Root 1 and 2: %.2f ± %.2f", root1, img);
return 0;
}
}
}
Problem: It works perfectly fine if the discriminant is greater than zero. But when it's equal to or less than zero, it skips everything in the code for some reason. I'm having trouble finding the error. I put in printf statement to see what is the value of the discriminant and I kept a printf statement in the if statement to see if it will printf anything, but it skipped that.
Output I got:
gcc version 4.6.3
Do you want to solve an equation (y/n): y
Input the number
````````````````
A: 1
B: 2
C: 5 //It ends here
Output I want:
gcc version 4.6.3
Do you want to solve an equation (y/n): y
Input the number
````````````````
A: 1
B: 2
C: 5
Root 1 and 2: -1±2i
If you look at you if statement for dis = 0, it should:
if(dis == 0)
That should fix all your problems. Nice job with the code. Just a beginner mistake.
You're using the operator = when you want the operator ==.
a == b checks if 2 numbers are equal, while a = b sets the first to the value of the second.
In other words, change dis = 0 to dis == 0.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I ran into a problem while calculating density of a box.
The output always comes out to be 0.00.
I tried type-casting but nothing worked.
Please help, I will be very grateful.
Here is the code...
#include <stdio.h>
void main()
{
int l, b, h;
float wt, vol;
float den = 0.0;
// Taking Length
printf("Enter Length ");
scanf_s("%d", &l);
//Taking Breadth
printf("\nEnter Breadth ");
scanf_s("%d", &b);
//Takhing Height
printf("\nEnter Heigh ");
scanf_s("%d", &h);
//Calculating Volume of the Box
vol = (l * b * h);
//Printing Volume
printf("\nVolume Of The Box Is %.2f\n\n", vol);
//Now taking mass of the box
printf("Enter Weight ");
scanf_s("%f", &wt);
//Calculating Density
den = wt /(float) vol;
//Printing Density
printf("\nDensity of the Box Is %.2f\n\n", &den);
}
The output of the program is as follows :
Enter Length 10
Enter Breadth 10
Enter Height 10
Volume Of The Box Is 1000.00
Enter Weight 5000
Density of the Box Is 0.00
Press any key to continue. . .
Note
Density = Mass(Weight)/Volume
Thanks :)
At least: wrong format specifier.
// double address
printf("\nDensity of the Box Is %.2f\n\n", &den);
Save time, enable all warnings. A good well enabled compiler will warn about this mismatch.
Recall &v prints the address of the variable v in memory, we use this in scanf (if you're a beginner), so change your code to:
printf("\nDensity of the Box Is %.2f\n\n", den);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to learn C and I've started by building a simple change calculator. At the moment, I'm just trying to take the user input 8.68 and output it, output the loonies and output the quarters. However, the last 3 printf() calls return nothing:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void){
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", value);
printf("%lf", value);
printf("%d", loonies);
printf("%lf", remainder);
}
This is the output. The prompt asks for the user input and then the program ends:
Please enter the amount to be paid: $8.68
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
You are missing an '&'. Scanf requires you to pass pointers to the variables you want to read into.
scanf("%lf", &value);
The scanf expects the address of the variable that you are passing. Here in your case you have to give &value.
The full code I am pasting here
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void) {
double value = 0;
int loonies = value;
double remainder = value - loonies;
printf("Please enter the amount to be paid: $");
scanf("%lf", &value);
printf("Values- %0.2lf\n", value); //'\n' and 'Values-' added
printf("Loonies - %d\n", loonies); //'\n' and 'Loonies-' added
printf("remainder - %0.2lf\n", remainder); //'\n' and 'remainder-' added
}
Output I am getting is
Please enter the amount to be paid: $8.68
Values- 8.68
Loonies - 0
remainder - 0.00
Edit: Here I am giving %0.2lf for printing only two decimal characters
You are missing a & in your scanf.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'd like to take two integers from the user and print the results of this equation:
result = ((num1 + num2) * 3) – 10
When I declare num1 , num2 and result as integer variables the program didn't run, there was an error.
int num1 ,num2, result ;
printf("please enter the first number:");
scanf("%d",&num1);
printf("please enter the second number:");
scanf("%d",&num2);
result = (((num1 + num2) * 3) – 10 );
printf("the result is %d",result);
But when I defined a new variable y to be equal the sum of num1 and num2 the program works successfully:
int num1 ,num2, result,y ;
printf("please enter the first number:");
scanf("%d",&num1);
printf("please enter the second number:");
scanf("%d",&num2);
y=num1+num2;
result = ((y * 3) – 10 );
printf("the result is %d",result);
Why does this happen?
It does run.
In the line:
result = (((num1 + num2) * 3) - 10 );
your - is an horizontal bar character, delete it and replace with the propper minus.
Look here https://repl.it/#anastaciu/BlondSuperbTrigger
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
The program runs fine but keeps giving me an answer of 0.00. I have floated the numbers and answer and it asks for the first and second number but I cannot see where I have gone wrong.
#include <stdio.h>
#include <math.h>
int main()
{
float sub;
float num1 = 18.73;
float num2 = 20.00;
printf("Please enter the total of the meal: \n");
scanf("%f", &num1);
printf("Please enter the amount of money you have: \n");
scanf("%f", &num2);
sub = num2 - num1;
printf("\nYour change is: %.2f\n", &sub);
return 0;
}
You're printing the address of sub:
Do this:
printf("\nYour change is: %.2f\n", sub);
instead of:
printf("\nYour change is: %.2f\n", &sub);
In your code I have found 2 mistakes
Don't assign greater than 0 numbers to variables when you use scanf
float num1 = 0; //use this
float num2 = 0;
When you output a number don't use & in printf. That is the reason you were given 0.
printf("\nYour change is: %.2f\n", sub);
Finally, you don't need to use #include <math.h> in these king of programmes
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
team1sum / team2sum is supposed to get the sum of each playerWeight1 / playerWeight2 and print it out.
My team1sum and team2sum are printing out wrong numbers.
The right output can be seen here.
#include <stdio.h>
int main() {
int i, howManyPlayers, playerWeight1, playerWeight2,
team1sum = 0, team2sum = 0;
scanf("%d", &howManyPlayers);
for (i = 0; i < howManyPlayers; i++){
scanf("%d", &playerWeight1);
scanf("%d", &playerWeight2);
team1sum = team1sum + playerWeight1;
team2sum = team2sum + playerWeight2;
}
if (team1sum > team2sum){
printf("Team 1 has an advantage\n");
printf("Total weight for team 1: %d\n", &team1sum);
printf("Total weight for team 2: %d", &team2sum);
}
else {
printf("Team 2 has an advantage\n");
printf("Total weight for team 2: %d\n", &team2sum);
printf("Total weight for team 1: ", &team1sum);
}
return 0;
}
Your printf needs the int, not the address of the int, so remove the & operator.
printf("Total weight for team 2: %d\n", team2sum);
printf("Total weight for team 1: %d", team1sum);
That should do the trick.
I ran your code and it seems that the actual sum is being calculated correctly.
By removing the & from the print statements, you should obtain the expected output.
Remember that when you print using printf, the format does matter, and printing a & to an int is not the same thing as printing an int.