While printing a number, I am trying to print its sign before the number. Is there a way to do it without the actually if...else case mentioned in the comment section of the code below.
I have tried getting the sign of the number. But I don't know how to print just the sign.
#include<stdio.h>
#include<complex.h>
void main(){
double complex s = 3.14 + 5.14*I;
printf("\ns is: %f + %f i", creal(s), cimag(s));
double complex S = conj(s);
printf("\nConjugate of s is: %f + %f i", creal(S), cimag(S));
}
/*
printf("\nConjugate of s is: %f ", creal(S))
if cimag(S) > 0
printf("+ %f i", cimag(S))
else
printf("- %f i", abs(cimag(S)))
*/
If S = 3.14 - 5.14*I, without the if...else condition, I'm expecting to get an output something like this:
3.14 - 5.14 i
You can just use the printf sign flag. +
#include <stdio.h>
int main()
{
float f = 1.0;
printf("%f%+f",f,f);
return 0;
}
Output
1.000000+1.000000
Change to -1:
-1.000000-1.000000
If you really need the spaces, you're going to have to do something like you described:
#include <stdio.h>
#include <math.h>
#include <complex.h>
void complexToString(double complex num, char * buffer){
double imag = cimag(num);
char sign = (imag<0) ? '-':'+';
sprintf(buffer,"%f %c %f i",creal(num),sign,fabs(imag));
}
int main()
{
double complex s = 3.14 + 5.14*I;
char buffer[50];
complexToString(s,buffer);
printf("%s",buffer);
return 0;
}
output:
3.140000 + 5.142000 i
First get the sign character:
double x = ...;
char c = signbit(x) ? '-' : '+';
Then use it however you want:
printf ("%c %f", c, fabs(x));
With the help of answers from #Antoine and #yhyrcanus, the simplest way to code for space is:
double complex s = 3.14 - 5.14*I;
printf("\ns is: %f %c %f i", creal(s), signbit(cimag(s)) ? '-' : '+',cabs(cimag(s)));
Related
I've tried to build a simple calculator for physics force experiments.
//
// main.c
#include <stdio.h>
int main() {
char name[20];
int month,date;
int difference_percentage1, difference_percentage2, difference_percentage3;
double force1, force2, force3;
scanf("%s", name);
scanf("%d %d", &month, &date);
scanf("%lf %lf %lf", &force1, &force2, &force3);
double Favg=(force1 + force2 + force3)/3.000;
puts(name);
difference_percentage1=100*(force1-Favg)/Favg;
difference_percentage2=100*(force2-Favg)/Favg;
difference_percentage3=100*(force3-Favg)/Favg;
printf("%d %d %d\n", difference_percentage1, difference_percentage2, difference_percentage3);
getchar();
return 0;
}
The calculate doesn't match what I've typed for scanf().
The o's of the % mark for the %f look a bit larger to me than for the %d, so my guess is that you used the wrong % for the %f format specifier which isn't recognized by the compiler; it interprets the double value as int.
Edit: one more reason to post text instead of a screenshot of the code! (o;
Edit2: Yup, your % mark in %f is actually a "EF BC 85" in hex but should be "25"
I have input the values of d = 10 s = 5 and h = 4 and I got a different output than expected.
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",& d,&s,&h);
double E= sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T=sqrt((2*s)/d*h);
printf ("T=%lf\n",T);
return 0;
}
Expected output
E = 5
T = 0.5
What I get
E = 5
T = 2
double T=sqrt((2*s)/d*h);
'/' and '*' has same precedence so the order of evaluation is left to
right.
so first (2xs) = 10 next 10/10 = 1; at last 1x2 = 2;
Hence T is 2.
For more info - https://www.geeksforgeeks.org/operator-precedence-and-associativity-in-c/#:~:text=Operator%20precedence%20determines%20which%20operator,one%20operators%20with%20different%20precedence.&text=Operators%20Associativity%20is%20used%20when,Right%20or%20Right%20to%20Left.
Try this one I am getting E=5 and T=0.5
#include <stdio.h>
#include <math.h>
int main()
{
float d,s,h;
printf ("enter values:");
scanf("%f %f %f",&d, &s, &h);
double E = sqrt((2*d*s)/h);
printf ("E=%lf\n",E);
double T = sqrt((2*s)/(d*h));
printf ("T=%lf\n",T);
}
This is the output I got
E=5.000000
T=0.500000
I am trying to pass a number an found its root
I tried running this code to an online compiler and I get random numbers that I didn't entered with scanf. I tried it on an online compiler that can be found here https://www.onlinegdb.com/online_c_compiler#.
For some reason whatever I put I get 8. I also tried it in DEV-C++ which can found here https://sourceforge.net/projects/orwelldevcpp/ in that complier I always get 0 instead of my input number. There is also printf for confirmation to scanf and seems to me that the value actually becomes my input but when I call the function everything changes any ideas (thanks for ur time)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define g(x,num) 1.0/3.0*(2.0 * x + (num / x)) //f'(x,num)
#define var 1E-10//10^-10
#define f(x,num) x*x - num
double x;
int i;
double num;
void sqrtNR(double num){// **actual bug**
printf ("\nnum: %d\n", num);
x = num;
printf ("x: %d\n", x);
for(i;var < fabs(f(x,num)) ; i++) { **infinite loop**
x = x - ( f(x,num)/g(x,num) );
// printf ("x: %d\n", x);
}
printf ("metrhths %d\n", i);
return x;
}
int main(int argc, char** argv) {
printf("dwse ari9mo\n");
scanf("%lf", & num);
printf("%lf", num);
sqrtNR(num);
//printf("\nsrtNR : %lf", sqrtNR(num));
}
youknow actually get the for at least working cuz now its on infinite loop
Your printf formats are wrong and cause the UB.
Abstracting from the math logic of your function.
void sqrtNR(double num)
{
printf ("\nnum: %f\n", num);
x = num;
printf ("x: %f\n", x);
while(var < fabs(f(x,num))
{
x = x - ( f(x,num)/g(x,num) );
}
return x;
}
you should also put your macro parameters in the parenthesizes
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.
I have customer data such as customer no, the coordinates of location etc. And there are 25 customers in the text file.
Here's my code. This gives me an output of zeros when I print it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define customerCount 25
struct customerData
{
int customerNo;
double xCoordinate;
double yCoordinate;
double demand;
double readyTime;
double dueTime;
double serviceTime;
};
int main()
{
int i;
struct customerData allSubscriber[customerCount];
FILE *dosya;
dosya = fopen("c:\\solomon_c101.txt", "r");
for(i=1; i<=customerCount; i++)
{
fscanf(dosya, "%d %f %f %f %f %f %f", &allSubscriber[i].customerNo, &allSubscriber[i].xCoordinate, &allSubscriber[i].yCoordinate, &allSubscriber[i].demand, &allSubscriber[i].readyTime, &allSubscriber[i].dueTime, &allSubscriber[i].serviceTime);
}
fclose(dosya);
for(i=1; i<=customerCount; i++)
{
printf("%f\n", &allSubscriber[i].xCoordinate);
}
getch();
return 0;
}
Use this:
printf("%f\n", allSubscriber[i].xCoordinate);
instead of
printf("%f\n", &allSubscriber[i].xCoordinate);
As another user pointed out,use %lf as the format specifier for double in fscanf().In printf() it gets promoted to double.
Don't use conio.h and getch().Those are not standard C.
use the %lf to read a double in scanf
printf("%f\n", &allSubscriber[i].xCoordinate); //remove &