Squared Equation calculator not working in C - c

Hello guys I started learning C few weeks ago and I am trying to make my first useful program, I am trying to create a squared equation calculator but no matter what I type in the input it always outputs "no solution2" can anyone take a look and help me ?
examples for input :
0 0 0=
1 4 1=
#include <stdio.h>
#include <math.h>
int main()
{
printf("enter a\n");
double a; scanf("%1f", &a);
printf("\nenter b\n");
double b; scanf("%1f", &b);
printf("\nenter c\n");
double c; scanf("%1f", &c);
if (a==0)
{
if (b==0)
{
if (c==0)
printf("x can be every number\n");
else
printf("no solution1\n");
}
else
{
printf("x equals %.2f\n", ((-1)*c) / b);
}
}
else
{
double delta = (b*b)-(4*(a*c));
if (delta>0)
{
double sqrtDlt = sqrt(delta);
printf("x1 = %4.2f\n", (((-1)*b) + sqrtDlt) / 2 * a);
printf("x2 = %4.2f\n", (((-1)*b) - sqrtDlt) / 2 * a);
}
else
printf("no solution2\n");
}
return 0;
}

For double use specifier %l(ell)f not %1(one)f in scanf.
Note that this is one place that printf format strings differ substantially from scanf (and fscanf, etc.) format strings. For output, you're passing a value, which will be promoted from float to double when passed as a variadic parameter. For input you're passing a pointer, which is not promoted, so you have to tell scanf whether you want to read a float or a double, so for scanf, %f means you want to read a float and %lf means you want to read a double.

Related

C language pointer

Write a program that uses two pointer variables to read two double numbers and display the absolute value of their sum?
this is my code and I don not know where it gets wrong:
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%f %f", p1,p2);
if(*p1+*p2 >= 0)
printf("%f\n", *p1+*p2);
else
printf("%f\n", -(*p1+*p2));
return 0;
}
if you have to scan doubles you use "lf" and to print them as well. It was your only mistake. "f" it is just for floats.
int main(void)
{
double *p1,*p2, val1,val2;
p1 = &val1;
p2 = &val2,
printf("Enter two number: ");
scanf("%lf %lf", p1,p2);
if(*p1+*p2 >= 0)
printf("%lf\n", *p1+*p2);
else
printf("%lf\n", -(*p1+*p2));
return 0;
}
http://www.cplusplus.com/reference/cstdio/scanf/
Please consult this website or a similar one for errors or warnings.
%f is used for float values, and c compilers often issue warnings or stop compilation when type conversions occur without specifying them.
%lf is used for doubles.

C not reading values from input correctly

I am very new to C, and while working on a project which requires pulling an indeterminate amount of values from the console, I am finding that it is not pulling the correct values. It seems like addresses, which I believe means it is a pointer issue, but I can't seem to find it.
int getVals(int degree){
double sum;
double x;
double coefs[degree];
for(int counter = 0; counter<=degree; counter = counter+1){
double nxt;
scanf(" %d", &nxt);
coefs[counter] = nxt;
printf("coefs[%d] = %d\n", counter, coefs[counter]);
}
printf(" x ? ");
scanf(" %d", &x);
printf("degree %d x %d\n", degree, x);
sum = poly(x, degree, coefs);
printf ("polynomial evaluate to: %lf\n", sum);
int newDegree;
scanf(" %d", &newDegree);
degree = newDegree;
if(degree>-1){
getVals(degree);
}
else
return degree;
}
Note: poly returns a double result of the evaluated polynomial
I am getting the following infinite loop after entering a degree of 1 and a coefficient of 1.5. It does not allow me to enter an x.
Infinite loop
In scanf(" %d", &newDegree); you should use the "%lf" format specifier (since your values is a double, not an int). Change the format specifier in all your calls to scanf() and "%f" in calls to printf().
Please refer to the documentation at this links printf(3), scanf(3).

Returns a value as an output parameter

I have a question in C where I need to insert coefficients of a quadratic equation into a function and return the number of solutions and result.
Write a program that accepts a series of 3 real numbers, which are the
coefficients of a quadratic equation, and the program will print out
some solutions to the equation and the solutions themselves.
Guidelines:
Functions must be worked with one of the functions that
returns the number of solutions as a returned value, and returns the
solutions themselves through output parameters.
3 numbers must be
received each time. The input will be from a file (will end in EOF)
In the meantime I built the function without reading from a file just to see that it works for me, I built the function that returns the number of solutions but I got entangled in how to return the result as output parameter
here is my code for now:
int main ()
{
double a, b, c, root1,root2,rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=(rootnumber(a,b,c);
printf("the number of roots for this equation is %d ",rootnum);
}
int rootnumber (double a,double b, double c)
{
formula=b*b - 4*a*c;
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}
In C, providing an "output parameter" usually amounts to providing an argument that is a pointer. The function dereferences that pointer and writes the result. For example;
int some_func(double x, double *y)
{
*y = 2*x;
return 1;
}
The caller must generally provide an address (e.g. of a variable) that will receive the result. For example;
int main()
{
double result;
if (some_func(2.0, &result) == 1)
printf("%lf\n", result);
else
printf("Uh oh!\n");
return 0;
}
I've deliberately provided an example that illustrates what an "output parameter" is, but has not relationship to the code you actually need to write. For your problem, you will need to provide two (i.e. a total of five arguments, three that you are providing already, and another two pointers that are used to return values to the caller).
Since this is a homework exercise, I won't explain WHAT values your function needs to return via output parameters. After all, that is part of the exercise, and the purpose is for you to learn by working that out.
Apart from a wayward parenthesis in the call and some other syntax errors, what you have so far looks fine. To print out the number of roots, you need to put a format specifier and an argument in your printf statement:
printf("the number of roots for this equation is %d\n", rootNum);
The %d is the format specifier for an int.
Here is your working code:
#include <stdio.h>
int rootnumber (double a,double b, double c)
{
double formula = (b*b) - (4*(a)*(c));
if (formula > 0) {
return 2;
}
else if (formula < 0) {
return 0;
}
else {
return 1;
}
}
int main (void)
{
double a, b, c;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
printf("The number of roots for this equation is %d ", rootnumber(a,b,c));
return 0;
}
It just need some sanity checking, its working now:
#include<stdio.h>
int rootnumber(double a, double b, double c);
int main ()
{
double a, b, c, root1,root2;
int rootnum;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf",&a, &b, &c);
rootnum=rootnumber(a,b,c);
printf("the number of roots for this equation is %d", rootnum);
return 0;
}
int rootnumber(double a, double b, double c)
{
int formula= (b*b) - (4*a*c);
if (formula<0)
return 0;
if (formula==0)
return 1;
else
return 2;
}

User enter 5 characters and averge sums out the 5 numbers in C

I been having issues having my C code work. I have 1 warning, which states Warning: too many arguments for format. I am a beginner in C so I haven't encountered this issue yet. Any ideas on how to fix it and I cannot use conditions as I am in the beginning segment of my course learning from the start. I just need to know what I did wrong so I can fix the issue. Here's the code below:
#include <stdio.h>
int main() {
float firstNumber, secondNumber, thirdNumber;
float fourthNumber, fifthNumber;
float sumAverage1 = (firstNumber+secondNumber+thirdNumber);
float sumAverage2 = (fourthNumber+fifthNumber);
long a = 1000000000;
long b = 1250000000;
long c = 1500000000;
long d = 1750000000;
long e = 2000000000;
printf("A is %li\n", a);
printf("B is %li\n", b);
printf("C is %li\n", c);
printf("D is %li\n", d);
printf("E is %li\n", e);
printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
printf("You cannot enter a decimal integer and enter numbers below 100.\n");
scanf("%f", &firstNumber);
scanf("%f",&secondNumber);
scanf("%f",&thirdNumber);
scanf("%f",&fourthNumber);
scanf("%f",&fifthNumber);
printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);
system("pause");
return 0;
}
The line:
printf("Your numbers average out to:\n", sumAverage1+sumAverage2/5);
Has an argument but no format specifier. Also, that expression is unparenthesized; the division has higher precedence than the addition, so what you're calculating is sumAverage1+(sumAverage2/5), which is integer division, which is probably not what you want.
What you probably want is:
printf("Your numbers average out to: %f\n", (double)(sumAverage1+sumAverage2)/5.0);
Here this will solve all your problem.
#include <stdio.h>
#include <stdlib.h>
int main() {
float firstNumber, secondNumber, thirdNumber,sumAverage1;
float fourthNumber, fifthNumber,sumAverage2;
long a = 1000000000;
long b = 1250000000;
long c = 1500000000;
long d = 1750000000;
long e = 2000000000;
printf("Enter 5 Random numbers and guess what the total will be summed up when program runs.\n");
printf("You cannot enter a decimal integer and enter numbers below 100.\n");
scanf("%f",&firstNumber);
scanf("%f",&secondNumber);
scanf("%f",&thirdNumber);
scanf("%f",&fourthNumber);
scanf("%f",&fifthNumber);
sumAverage1 = (firstNumber+secondNumber+thirdNumber);
sumAverage2 = (fourthNumber+fifthNumber);
printf("A is %li\n", a);
printf("B is %li\n", b);
printf("C is %li\n", c);
printf("D is %li\n", d);
printf("E is %li\n", e);
printf("Your numbers average out to:%f\n", (sumAverage1+sumAverage2)/5);
system("pause");
return 0;
}
I ran this on my Visual Studio and it works just fine. Hope it Solves your Problem.
You need to change the printf format specifier, but your scanf does not capture the extra dangling newline. You need to either clear the buffer, fflush(stdin) after each scanf() or you need an extra scanf("%c") to get rid of the newline character.
See scanf() leaves the new line char in buffer?

in C, trying to use a program that converts Fahrenheit to kelvin [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
But the calculation doesn't work or change when I run it and plug in any random number...need some guidance. I'm a novice to C and programming in general so please include easy to understand help.
#include <stdio.h>
double const change_celcius = 32.0;
double const change_kelvin = 273.15;
void temperatures(double n);
int main(void)
{
int q = 'q';
double user_number;
printf("Enter the fahrenheit: \n");
scanf("%f", &user_number);
while (user_number != q)
{
temperatures(user_number);
printf("\n");
printf("Enter the fahrenheit: \n");
scanf("%f", &user_number);
}
}
void temperatures(double n)
{
double celsius, kelvin;
celsius = 5.0 / 9.0 * (n - change_celcius);
kelvin = 5.0 / 9.0 * (n - change_celcius) + change_kelvin;
printf("fahrenheit: %.2f - celsius is: %.2f - kelvin is: %.2f",
n, celsius, kelvin);
}
I don't believe the all the use %lf instead of %f comments, by themselves, fix your program. The handling of q (for "quit") is also problematic so let's fix that too. First, we'll use POSIX function getline() to read it into a string and test if it's "q". If not, we'll sscanf it into a double and use it as our temperature:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
double const change_celsius = 32.0;
double const change_kelvin = 273.15;
void temperatures(double n)
{
double celsius = 5.0 / 9.0 * (n - change_celsius);
double kelvin = 5.0 / 9.0 * (n - change_celsius) + change_kelvin;
printf("fahrenheit: %.2f - celsius is: %.2f - kelvin is: %.2f\n", n, celsius, kelvin);
}
int main(void)
{
char *user_string = NULL;
ssize_t user_string_length;
size_t user_string_capacity = 0;
while (1)
{
printf("Enter the fahrenheit: ");
if ((user_string_length = getline(&user_string, &user_string_capacity, stdin)) < 1)
break;
if (strncmp(user_string, "q\n", (size_t) user_string_length) == 0)
break;
double user_number;
if (sscanf(user_string, "%lf", &user_number) == 1)
temperatures(user_number);
}
if (user_string != NULL)
free(user_string); // free memory allocated by getline()
if (user_string_length == -1)
putchar('\n'); // output courtesy newline if user used ^D to exit
return(0);
}
We check the return value of sscanf so that bad input won't cause the program to recalculate using the last good input. Instead, it will just prompt again for input.
You need to use "%lf" in scanf() and print() to read and write the value of type double.
Note that the printf() will work with "%f" too.
For more details please refer : Why does scanf() need "%lf" for doubles, when printf() is okay with just "%f"?
There are several issues that can be addressed in your code. First, always (Always, in case it wasn't clear) check the return of scanf. That is the only way you know whether the expected number of conversions took place -- and whether you have an actual value to work with in your code.
The return also holds the key to exiting the loop when the user enters 'q' (or anything that causes the conversion to double to fail). By simply checking
if (scanf(" %lf", &user_number) == 1)
You can determine whether to process the value as a temperature, or tell the user has indicated exit.
Another tip, never (Never) write:
printf ("\n");
Why would you want to call a variadic function simply to output a single char? That is what putchar (or fputc) is for, e.g.:
putchar ('\n');
Putting those pieces together, and noting that %lf is used as the format specifier for double, you can rewrite your code, and format the output in quite a bit fewer lines, e.g.
#include <stdio.h>
double const change_celcius = 32.0;
double const change_kelvin = 273.15;
void temperatures (double n);
int main(void)
{
double user_number;
while (printf ("\nEnter temp in degrees fahrenheit: ") &&
scanf(" %lf", &user_number) == 1)
temperatures(user_number);
return 0; /* main() is type 'int' and returns a value to the shell */
}
void temperatures (double n)
{
double celsius, kelvin;
celsius = 5.0 / 9.0 * (n - change_celcius);
kelvin = 5.0 / 9.0 * (n - change_celcius) + change_kelvin;
printf(" fahrenheit: % 7.2lf\n celsius is: % 7.2lf\n kelvin is : % 7.2lf\n",
n, celsius, kelvin);
}
Example Use/Output
$ ./bin/temps
Enter temp in degrees fahrenheit: 212
fahrenheit: 212.00
celsius is: 100.00
kelvin is : 373.15
Enter temp in degrees fahrenheit: 0
fahrenheit: 0.00
celsius is: -17.78
kelvin is : 255.37
Enter temp in degrees fahrenheit: 68
fahrenheit: 68.00
celsius is: 20.00
kelvin is : 293.15
Enter temp in degrees fahrenheit: q
Always compile your code with at minimum -Wall -Wextra warnings enabled (and if you really want to drill down, add -pedantic). Read the warnings and fix them. All of your code should compile without warning before you consider your code reliable at this stage of the game.
Look all answers over, and let me know if you have any questions.
To read Double use %lf instead of %f.
for double printf() will work with %f also.
The calculation seems to be okay; however the you are not reading in the words properly
scanf("%f", &user_number);
You are stating you are reading in a float, yet you are declaring user_name as a double. If you wanted to use a float, you would need to change the user_name declaration from double to float. If you wanted to use a double use "%f".

Resources