#include<stdio.h>
#include<conio.h>
main()
{
int f,c;
printf("enter the value of celsius in integer (the value of f will be shown in integer neglecting the float value)");
scanf("%d,&c");
f=((9*c)/5)+32;
printf("f=%d,&f");
getch();
}
When i am going to compile and run this program in my window 7 then in compiler it is showing the string to enter the digit but when i am entering a digit to finding out its f then it is giving an error "celcius.exe has stopped working" and after that it is showing "A problem caused the program to stop working correctly. Window will close the program and notify you when a solution will available." How it will be handle in dev c++. Please help me to sorting it out.I am new with c. Thank you.
Change
scanf("%d,&c");
to
scanf("%d",&c);
and
printf("f=%d,&f);
to
printf("f=%d",f);
Side note:
Never use main() instead use int main() and better to use int main(void) and do not forget to add return 0 before closing braces of main.
In Dev C++ no need to use getchar(). It will cause to double press Enter to exit the console.
scanf("%d",&c);
f=((9*c)/5)+32;
printf("f=%d",f);
Change the code as per following
scanf("%d,&c"); to scanf("%d",&c);
printf("f=%d,&f); to printf("f=%d",f);
However your program will give wrong results most of the time. You should declare f and c as float so that f=((9*c)/5)+32; will be evaluated as float division. Now with your code it will be evaluated as integer division. integer division 10/3 will be evaluated to 3 not 3.33.
Rewriting your code
#include<stdio.h>
#include<conio.h>
main()
{
float f,c;
printf("\nEnter Celcius -");
scanf("\n%f",&c);
f=((9*c)/5)+32;
printf("\nf=%f",f);
getch();
return 0;
}
Related
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
Hi i have a project for tommorrow which i want to finish but im stuck. Im pretty new at this so don't be harsh.Basicly i want my program to ask how many numbers did the user play. How much money, after it asks for the lottery numbers and puts then in a border,then it asks for the users numbers,puts in on a second border and then i want to compare the 2 of them and if they have a same number it will add to 'sum'.
#include <stdlib.h>
int main()
{
int k[20],i;
int k2[12],f;
int numbers,sum,n,l,num;
float money,winnings;
l=0;
sum=0;
printf("How many numbers from 1 to 12?\n");
scanf("%d",&num);
printf("How much money?\n");
scanf("%f",&money);
for (i=0;i<19;i++)
{printf("Give lottery numbers\n");
scanf("%d",&k[i]);}
while (l<num){
printf("Give your numbers\n");
scanf("%d", k2 + f); !!fixed!!
l++;}
for (f=0;f<num;f++){
for (i=0;i>19;i++){ !!fixed!!
if ((k[i])==(k2[f])) !!! and here i think its a mistake.
{
sum=sum+1;
}
}
}
printf("You got %d numbers out of %d",sum,num);
if ((sum=1) && (num=1));
{winnings=(money*2,5);
printf("Won %f",winnings);}
if ((sum=1) && (num=2));
{winnings=(money*1);
printf("won %f",winnings);}
if ((sum=2) && (num=2));
{winnings=(money*5);
printf("Won %f",winnings);}
system("pause");}
It doesn't appear that f has ever been initialized to anything. Therefore,
scanf("%d",k2[f]);
Will result in undefined behavior, and is the likely cause of the crash.
Additionally, you need to fix your indentation. Furthermore, your loop is off by one. You initialize l to 1, then execute the following loop, whose apparent purpose is reading num numbers:
while (l<num){
So, if, for example, "1" was entered, in order to read only one number, the body of the loop will never execute, since the comparison "1<1" will be false.
It's likely there are other problems with this code, hard to analyze it due to bad indentation.
This loop
for (i=0;i=19;i++){
will never end since i=19 will evaluate to always true.
I think that the intention was:
for (i=0;i<19;i++){
Instead of l, what you probably intended, you are using f, which, as Sam Varshavchik spotted already, is not initialized.
Additionally, you are not passing a pointer to scanf: scanf("%d", k2[f]);. You need scanf("%d", k2 + l); instead, or scanf("%d", &k2[l]), if you prefer.
Before anything, here's my code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
float Exp, Act, Px, Facto_Act;
printf("\n This is Poisson Distribution Calculator!");
printf("\n Enter the expected value of success in a time period:");
scanf("%f",& Exp);
printf("\n Enter the actual or calculated value of success in a time period:");
scanf("%f",& Act);
Px=pow(M_E,-Exp)*pow(Exp,Act)/Facto_Act;
printf("\n Poisson probability is:%f", Px);
getch();
return 0;
}
Facto_Act(float Act)
{
float c;
float result=1;
for(c=1;c<=Act;c++)
result=result*c;
return result;
}
Further explanation:
Poisson equation looks like this:
P(x)= (e^-Lambda)(Lambda^x)/(x!)
Exp: Expected number of events in a given time(Lambda)
Act: Actual number of events in a given time(x)
Px: Probability of an event occuring in a given time( P(x) )
Facto_Act: Factorial of Actual number of events in a given time(x!)
When I figured out how to do factorials for integer in C, I will try to add factorials for positive decimals too. But #1.INF00 is not a value I expect.
When I compile the code, there are no more coding errors shown. But when I enter the expected value of successes in a period, then the actual value of succesess in a period, I always end up with #1.INF00. I am very noobful of C, and while this site has helped me improved my programs by a bit, I can't understand the '#1.INF00' means.
I decided not to make Facto_Act a function
I decided to circumvent the entire Facto_Act function problem by not making Facto_Act a function, then trying to call it. It seems that factorials can be performed without making a new function for it. Thus Facto_Act is now a variable. This is my new code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
double Exp, Px;
int c, Act, Act2, Facto_Act=1;
printf("\n This is Poisson Distribution Calculator!");
printf("\n Enter the expected value of success in a time period:");
scanf("%lf",& Exp);
printf("\n Enter the actual or calculated value of success
\n in a time period(must be an integer!):");
scanf("%d",& Act);
/*My factorial starts here*/
for (c=1;c<=Act;c++)
Facto_Act=Facto_Act*c;
/*My factorial ends here*/
Px=(pow(M_E,-Exp))*(pow(Exp,Act))/Facto_Act;
printf("\n Poisson probability is:%lf", Px);
getch();
return 0;
}
I thank you all for helping me out.
You declared a variable named FactoAct of type float. Since it is an extern variable with no initialisation, it has a value of 0.
Later you define a function Facto_Act(float Act) with an implicit return type of "int".
Your division xxx / FactoAct divides xxx by the variable FactoAct, which is zero. That's where your INF result comes from.
When you had the function at the top, when the compiler saw xxx / FactoAct, FactoAct was not the result of a call to the function, it was the function itself. You can't divide a number by a function. It doesn't make sense. The only thing you can do with a function is take its address, or call it.
You probably want FactoAct (x) or something like that.
PS. Don't use float instead of double, unless you have a reason that you can put into clear words why in your specific case float is better than double.
I wrote this recursive function to calculate the terms of the sequence:
and to place them in a float array of maximum 1000 element, but this function is crashing as I run and input the float A and I don't see what's the problem in there.
#include<stdio.h>
#include<math.h>
void triple_root(float B[1000],int i,float A,float b,float c){
float x;
x = 0.333*((A/(b*b))+(1/c));
B[i] = x;
if(fabs(x-b)<=0.00001|| i==999)
puts(" ");
else triple_root(B,i+1,A,x,b);
}
int main(){
float A[1000],b;
int i;
scanf("%f",&b);
triple_root(A,0,b,1,1);
for(i=0;i<1000;i++){
printf("%f\n",A[i]);
}
getchar();
}
P.S.: The integer i initial value is 0, and the two floats b and c initial value is 1.
There is no explicit test to make sure i stays below 1000; your code assumes that the recursion will stop before that happens, but I see nothing to insure this.
Even with your latest edit, the code didn't compile for me. It's possible that you were running a broken binary. Not to worry, though! I managed to fix it.
Remove the <conio.h> include. You will rarely if ever need this non-standard header. Change getch to getchar.
Change void main to int main. main returns int, not void.
Viola! http://ideone.com/zeAuP1
#include<stdlib.h>
#include<stdio.h>
long double fact(unsigned long int n)
/*The factorial of a positive integer by recursion*/
{
if (n==0)
return 1;
else
return n*fact(n-1);
}
int main()
{
long double sum, n;
int i, m;
printf("\t/*Code to find the approximate value of e */");
check:
printf("\n\n\tPlease Enter the value of n := ");
scanf("%lf", &n);
sum=0;
for (i=0; i<=n; i++)
sum +=1/(fact(i));
printf("\n\n\tThe appriximate value of e := %.15lg\n\n\t", sum);
printf("Let's do this again? 1/ YES Any key/ NO := ");
scanf("%d", &m);
if (m==1)
goto check;
else (1);
return 0;
}
This code worked perfectly well with Visual C++ 2010 but not with DEV C++. It kept on returning zero for the value of e. Can someone please explain why! Thanks!
use this scanf("%Lf", &n); the Format specifier of long double is %Lf,%le,%lg so use this
and also consider these point
Dev-C++ It uses GCC (MinGW). No ammount of Dev-C++ updates will fix this issue.
Dev-C++ has not been updated for 5 years; don't hold your breath.
The derivative wxDev-C++ is maintained.
If you want a compiler update, go to www.mingw.org, and hope that the latest version still works with the ancient Dev-C++. Or use a different IDE or compiler. I'd recommend VC++ 2010 Express Edition (Free, and a far better debugger), your code works as is in VC++ 2008, but that does not make it safe.
How did you determine the value was "0"? It is likely that it was in fact something like 0.000001, and your method of interrogation rounded it. You should view the value in the debugger rather than a print statement. Unfortunately Dev-C++'s debugger sucks.
The ISO C standard library defines functions only for double precision. If you want versions defined for float, you need to compile as C++ and include . C does not support function overloading, so it is impossible to do in C without differently named functions.
scanf("%lf", &n);
%lf conversion specification is used to read a double.
You have to use %Lf to read a long double.
did you check wheter scanf(...) really reads the number you want to read? Try to print the value you read. And also the line
sum +=1/(fact(i));
looks suspicious. 1 as an integer divided by something bigger than 1 should always yield 0, in the optimal case on every compiler... Maybe you could try this instead:
sum +=1.0/(fact(i));
EDIT: Sorry was wrong here. Typeconversions make it possible to devide integer by long double.