I have written a small c code to find the sqrt of a number . And i want the program to even find the sqrt of a negative number . I came across complex.h and couple of functions to perform the complex arithmetic .I have made use of them and i'm getting a couple of warning messages while compiling.The code does not calculate the square root of positive and negative numbers correctly .
1)What is the correct format specifier for printf and scanf for printing and taking in complex numbers ?
Please let me know where exactly i'm going wrong .I'm using a gcc compiler and OS is Ubuntu 12.04 LTS.I have attached the code and the outputs.
#include <stdio.h>
#include <math.h>
#include <complex.h>
int main()
{
float complex var,op;
printf(" Enter the number : " );
scanf("%lf",&var);
op=csqrt(var);
printf("%lf \n",op);
}
o/p Enter the number : 4
0.011604
Another o/p Enter the number : -4
-0.011604
From C99:
6.2.5/13 Types
Each complex type has the same representation and alignment
requirements as an array type containing exactly two elements of the
corresponding real type; the first element is equal to the real part,
and the second element to the imaginary part, of the complex number.
So as one possibility, you can use:
scanf("%f %f",&var[0], &var[1]);
To read in a float complex type, with whitespace separating the real and imaginary parts.
To find the square root of a negative number you can use the following code,
printf(" Enter the number : " );
scanf("%lf",&var);
if(var < 0)
{
op=csqrt(-var);
printf("The square root is %lfi \n",op);
}
else
{
op=csqrt(var);
printf("The square root is %lf \n",op);
}
printf and scanf don't have native support for complex numbers, but that's OK; you can write the format easily enough:
scanf( "%f %fi", &re, &im );
now re and im contain the real and imaginary parts (although it won't accept spaces after the + or -, so 3 + 2i will be rejected, but 3 +2i and 3+2i are fine.
The same goes for printing;
printf( "%f%+fi", re, im );
will print the variables in 3+2i form.
Finally, though it doesn't seem to be directly part of your question, the square root of a negative number is simply the square root of the positive value multiplied by i.
Related
This is the code I wrote. Please tell me the mistakes and knowledge gap I may have
#include <stdio.h>
int main()
{
int i,n,c=1;
printf("enter the number 1:");
scanf("%d",&i);
printf("enter the second number:");
scanf("%d",&n);
while (i!=n)
{
c++;
i=i*c;
n=n*c;
}
printf("the lcm is %d",i);
return 0;
}
Input I put: 2 & 3
The output I get: The lcm is 0
Output expected: The lcm is 6
Your algorithm is simply wrong.
Take a basic example i=3 and n=4.
The LCM is 12 and to get 12 you multiply both numbers by a different number. Whereas you're assuming that you need to multiply both numbers by the same factor c.
You may have found the solution yourself by doing a very easy debugging step by step. You can even do that online for a basic code like yours.
There are other issues in your code, like the fact your are using signed integer (you probably need unsigned integer instead) and the fact you are nottaking care about integer overflow.
Im new to C and I really don't know know what I'm doing wrong.
The issue that I am having is I'm supposed to ask 3 questions of the user using scanf. I'm supposed to ask the user for an integer, a positive real number and a non negative number and then calculate the numbers into XX.XX using %.2f.
//pre-processor directives
#include <stdio.h>
#include <math.h>
//main function
int main()
{
//declare variables
int smp1,smp2, smp3,total;
printf("sample 1?\n"); // positive integer
scanf("%d", &smp1);
printf("sample 2?\n"); //positive real number
scanf("%f",&smp2);
printf("sample 3?\n"); // non negative number
scanf("%u", &smp3);
total = (smp1 + smp2 / smp3);
printf("The final result is %.2f",total);
//end of main
return 0;
}
No matter what I put in there my result ends up being 0.00. It won't even do simple addition and I don't know enough to know why.
Your main issue is that you declare all your variables as ints, but smp2 and total must hold floating point values.
Change your declarations to
int smp1;
double smp2, total;
unsigned int smp3;
This way, the types of the variables match up with the conversion specifiers used in the printf and scanf calls.
Types matter in C, and it's up to you that the types of the arguments in each printf and scanf call match up with the conversion specifiers.
Check your compiler documentation on how to enable warnings (even better, to treat all warnings as errors). Most compilers should warn about type mismatches like this, but sometimes you have to set a flag in order for those warnings to appear.
This question already has answers here:
How do I restrict a float value to only two places after the decimal point in C?
(17 answers)
Closed 6 years ago.
I've just started a class in C Programming, and while I have some background knowledge in JAVA, I'm trying to transition to this programming language. I have a project where I have to round user's input from something like 1.3333 to only two decimal places.
What I have so far is this:
#include <stdio.h>
int main (void)
{
//v is my variable for the value which the user will input
//Declaring variable as floating
float v;
printf("Enter your value: \n");
scanf("%.2f", &v);
v = 0;
printf("The rounded version is: %.2f");
return 0;
}
This is what I have so far based off of what I've read in my book and this link: Rounding Number to 2 Decimal Places in C which my question is different from because it involves user input. My professor does say that I can't use a library function and need to use simple type casts to calculate it. This makes me feel that what I have might be wrong. Would the #include <stdio.h> be considered a library function? Given this information, is my thought process on the right track? If not, then would I do something like divide by variable by 100? Maybe %e for scientific notation?
Thanks ahead of time! Only asking for specific information, not coding or anything. Really want to understand the "hows" and "whys".
First of all #include is a command that you need in order to include and use function that c provides for example for scanf you need to include library.
To round the number in two decimals without using %.2f in scanf you could write:
int x= (v*1000);
if(x%10>6) x=x/10+1 ;
else x= x/10;
printf("%d.%d",x/100,x%100);
I think your professor aims not so much in user input but rather in understanding what happens when converting basic datatypes. Rounding, or at least cutting off digits, without library functions could look as follows:
int main (void)
{
//v is my variable for the value which the user will input
//Declaring variable as floating
float v;
printf("Enter your value: \n");
scanf("%f", &v);
v = float((int)(v*100))/100;
printf("The rounded version is: %f", v);
return 0;
}
Input/Output:
Enter your value:
1.3333333
The rounded version is: 1.330000
Here is a working example that rounds properly without using any library calls other than stdio.
#include <stdio.h>
int main (void)
{
float v;
printf("Enter your value: \n");
scanf("%f", &v);
v = (float)((int)(v * 100 + .5) / 100.0);
printf("The rounded version is: %f\n",v);
return 0;
}
Output:
jnorton#mint18 ~ $ ./a.out
Enter your value:
3.456
The rounded version is: 3.460000
I'm trying to write a code that asks from the user to give 5 coefficients for a 5th-degree polynomial, and it also asks to give a range (two values) that the programs checks if there is a solution in it or not (I'm asked to find only one), and the solution must be an integer, while the coefficients can be floats.
I'm thinking of writing a code that runs over every integer in the range and substitute it in a description of a polynomial than I define, and check if its equal to zero, but I got stuck at deciding how to make the loops.
And another thing, if there are more than one root in the interval that the user enters, then we must print the minimal value of the roots (but I have no direction how to do that either).
I will show you what I wrote so far, and any kind of help would be appreciated:
#include <stdio.h>
#define zero 0.00001
int main()
{
double a, b, c , d , e , f , y , beginning_of_range, end_of_range;
int x;
printf("please enter the coefficients of the polynomial:\n");
scanf("%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e);
printf("please enter two values to indicate the beginning and end of range:\n");
scanf("%lf%lf", &beginning_of_range, &end_of_range);
while (beginning_of_range > end_of_range)
{
printf("ERROR: the range you have entered isn't valid, please try again:");
scanf("%lf%lf", &beginning_of_range, &end_of_range);
}
while (beginning_of_range < end_of_range)
{
x = beginning_of_range;
y = a + b*x + c*x*x + d*x*x*x + e*x*x*x*x + f*x*x*x*x*x;
if (y == zero)
{
printf("the root is:%d", x);
}
else
{
x = x+1;
}
break;
}
return 0;
}
Your task as written is very questionable, so much so that there is perhaps a mis-interpretation or mis-communication.
A randomly given polynomial with floating point coefficients will have integer roots with probability so close to zero that it is practically impossible.
Even with integer coefficients it requires carefully reverse-engineered coefficients to get an integer root. Just change one coefficient by one and in most cases all roots will be irrational.
What you can do in your framework is to find intervals with a sign change so that there is at least one root inside the interval that can be found using one of bisection, regula falsi, Illinois, secant or Mullers methods. All of them derivative free.
Without identifying all roots, also the complex ones, it is rather hard to guarantee that all real roots have been found. Thus it is only approximately possible to find the smallest real root inside the given interval. There might be an integer interval with two real roots inside so that at the boundaries the sign is the same. You would have to analyse the signs of all derivatives at the integer points to make more reliable guesses in this case, see Descartes rule and Budan-Fourier theorem.
Your first while loop should probably be an if statement instead of a loop.
Your second (main) while loop never increments the beginning of range. This is probably causing an endless loop for you.
i have made a program to compute roots of quauation but it does not simplify the roots.can anyone help me to simplify them
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int a,b,c;
float d,d2;
printf(" Enter a,b and c:");
scanf("%d %d %d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
printf("(%d+i%d)/%d\n",-b,sqrt(-d),2*a) ;
printf("(%d-i%d)/%d\n",-b,sqrt(-d),2*a);
}
else
{
printf("(%d+%d)/%d\n",-b,sqrt(d),2*a);
printf("(%d-%d)/%d\n",-b,sqrt(d),2*a);
}
getch();
}
You can't compute the square root of a negative number. d is negative and you're trying to find its square root. The whole point of complex solutions and the imaginary unit i is to write -1 as i^2, and then when d < 0 you have:
sqrt(d) = sqrt(i^2 * (-d)) = i*sqrt(-d)
So change to this:
if(d<0)
{
printf("(%d+i%lf)/%d",-b,sqrt(-d),2*a);
printf("(%d-i%lf)/%d",-b,sqrt(-d),2*a);
}
I don't know why you had parantheses around your printf arguments, I removed those.
The second %d should also be changed to %lf since sqrt returns a double.
If you want to compute square roots tof negative numbers, find a C99 compiler (basically, anything besides MSVC will do), include <complex.h> header, use complex data type and csqrt function.
http://en.wikipedia.org/wiki/Complex.h