Complex numbers through scanf in C - c

I have problem with following fragment of code:
int main()
{
int n = 3;
complex_t t[3];
for(int i = 0; i < n; i++)
{
double x, y;
printf("Enter complex number: ");
scanf("%f %f", &x, &y);
t[i].re = x;
t[i].im = y;
}
}
When I am trying to pass x, and y to the program, it doesn't change value, and is 0.00000.
Can you help me?

You've declared x and y as doubles, but then went on to use the format specifier for floats (%f). So the end result is reading them in as if they were regular floats, then jamming the result into a double leaving you with unexpected values when trying to actually use them.
You need to use the format specifier specifically for doubles (%lf) here.
scanf("%lf %lf", &x, &y);
See format string specifications for more information about different format specifiers.

Related

where I made the mistake and how to fix it?

#include <stdio.h>
float result(int x, int y);
float result1(int x, int y);
int main()
{
int x,y;
char z;
printf("enter x:\n");
scanf("%d",&x);
printf("enter y:\n");
scanf("%d",&y);
printf("enter z:\n");
scanf("%s",&z);
if (z=='*')
{printf("the result is %.2f",result(x,y));}
else if (z=='/')
{printf("the result is %.2f",result1(x,y));}
else
{printf("there is an error");}
return 0;
}
float result(int x, int y)
{
float r=x*y;
return r;
}
float result1(int x, int y)
{
float r1=x/y;
return r1;
}
```so this is my code. my out put is ---
enter x:
4
enter y:
5
enter z:
*
the result is 0.00
Question was -
take two integer number from user x and y and a character z. the result should be in float.
if z is * then it should be x*y
if z is / then it should be x/y
if z is none of the above then it will return 0
you need to use function .
so it was the question, I know it can be done by switch case but I wanted to try if else.
The problem is this:
scanf("%s",&z);
The format specifier %s is used to read null-terminated strings. The variable z is a single character, it can only hold the empty string (which is only the null-terminator and nothing else).
Any other input will write somewhere in memory and lead to undefined behavior.
If you want to read a single character use the format %c. But be careful, the newline that the Enter key added from previous input will also be read with %c, and you need to ask scanf to skip and ignore it. This is done by adding a leading space to the format string.
So the call should be:
scanf(" %c",&z);

getDouble function in C

I have created a custom function to prompt the user with whatever you want and they need to input a double. The problem is, if I were to enter a number like 1.22 it would print '1' so it doesn't print anything behind the decimal, no rounding
int main(void) {
double f = getDouble("Enter a double: ");
printf("%d\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%d", &x);
return x;
}
In your code, you are using printf("%d\n", var); to print a double and you are also using scanf("%d", &x); to get a double. You should use "%lf" instead as the format specifier of in both printf in scanf. Your code obviously doesn't work because scanf is looking for an integer input and printf is trying to print and integer instead of a double.
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
double getDouble(char* y){
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
This should work. You can find format specifiers for every basic type: https://en.wikipedia.org/wiki/C_data_types#Main_types
Use the correct format specifier for the correct type otherwise, the function will try to do things assuming the wrong datatype which will lead to problems.
For reading double you need to use "%lf", "%d" is for integer.
double getDouble(const char* y) {
double x;
printf("%s", y);
scanf_s("%lf", &x);
return x;
}
int main()
{
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
return 0;
}
In scanf/printf use %lf instead of %d
#include <stdio.h>
double getDouble(char *y) {
double x;
printf("%s", y);
scanf("%lf", &x);
return x;
}
int main(void) {
double f = getDouble("Enter a double: ");
printf("%lf\n", f);
}
As others have already pointed out, you are simply using the wrong specifier in scanf. They can be confusing without practice.
Here's a reference.

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).

Squared Equation calculator not working in 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.

Resources