C programming program only printing the first value in my variable - c

Program only displaying the 32 for when I have it print "NewTemp"
NewTemp = 32 + input * 180/100; this part seems like the main problem
#include <stdio.h>
float celsius(float input) {
float NewTemp;
**NewTemp = 32 + input * 180/100;
printf("Please enter the temperature value to convert to fahrenheit\n");
scanf("%f", &input);
printf("The temperature in celsius is: %f\n", NewTemp);
return NewTemp;
}
int main(void){
float CelToFahren, input;
CelToFahren = celsius(input);
}

You do the math before you read the input. You need to do it the other way around.
Also, there's no reason to pass a meaningless and uninitialized value to the celsius function.
Lastly, 180/100 is 1 remainder 80 because when you divide two integers, you get integer division. You can use 180.0/100.0.
Basically, you need to learn C.

Related

Trouble going from Celsius to Fahrenheit

I just started learning C today so this might seem like a simple question but I'm having trouble running my program. I'm trying to implement a program that converts from Celsius to Fahrenheit. The formula for converting from Celsius to Fahrenheit is T(F)=T(C)*9/5+32. The degrees In Celsius is given as 30.
int main()
{
int DegreesCelsius;
printf("30 Degrees Celsius:");
scanf("%d",&30 );
printf("The temperature in F is %d\n", 30 * 9/5 + 32);
return 0;
}
My work is shown above. The problem is on the line containing scanf where I'm told "1value required as unary & operand"
Am I using the ampersand incorrectly?
Thanks
You need to scan the entered value into DegreesCelsius, and then use that variable to perform the conversion.
int DegreesCelsius;
printf("Degrees Celsius: ");
scanf("%d", &DegreesCelsius);
printf("The temperature in F is %d\n", DegreesCelsius * 9/5 + 32);
&30 is invalid syntax, as 30 is an integer literal and cannot be addressed by &; this is why you have DegreesCelsius, to hold the value the user enters.

getting different output from different compilers

The question was:
Define a function getint(), which would receive a numeric string from keyboard, convert it to an integer number and return the integer to the calling function.
My code is:
#include<stdio.h>
#include<math.h>
#include<string.h>
int getint();
int main()
{
int a;
a = getint();
printf("you entered %d",a);
return 0;
}
int getint()
{
char str[10];
printf("Enter number: ");
gets(str);
int d=0,len = strlen(str),r = len-1;
for(int i=0;str[i] != '\0';i++,r--)
d += (str[i]-48)*pow(10,r);
return d;
}
while I run this program from sublime text or code block the output was coming wrong
output(from sublime and codeblocks):
Enter number: 123
you entered 122
But when I used onlinegdb.com/online_c_compiler the output was coming correct
So how can output differ from compiler to compiler for the same program
The pow function works with floating point numbers. As such, the result may not be exactly equal to what you expect the numerical result to be. For example, pow(10,2) could output a number slightly larger or slightly smaller than 100. If the result is just below 100, the fractional part gets truncated and you're left with 99.
Rather than using pow for an integer power, just multiply the current value of d by 10 before adding the value for the next digit.
d = 10 * d + (str[i]-'0')

5/9 is not taking by the C compiler to Calculate fc [duplicate]

This question already has answers here:
Division result is always zero [duplicate]
(4 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 5 years ago.
I'm stuck in a Code where I have to convert a temperature given by the user (in Fahrenheit) to degree Celsius. But unfortunately it's formula is not working with C Compiler.
#include<stdio.h>
#include<stdlib.h>
void c_f();
//void f_c();
float c,f,fc,fc1;
int main()
{
c_f();
// f_c();
return 0;
getch();
}
void c_f()
{
printf("\n Enter the temperature (in *F) to covert it into Celsius: ");
scanf("%d",&f);
fc=((5/9)*(f-32));
printf("\n %f*C",fc);
}
fc=((5/9)*(f-32));
Because 5 and 9 are integers, the arithmetic calculation is done at integer level, thus 5/9 == 0.
You should change it to floating point:
fc = ((5.0f / 9.0f) * (f - 32.0f));
Besides, format specifier %d is used for integers. If you want to read a floating point number, use %f:
scanf("%f", &f);
The result should be right now.
You have to use the correct format specifier which will be scanf("%f",&f) in this case.
Also fc=((5.0/9.0)*(f-32)), otherwise integer division yields 0.(Integer division truncates).5/9.0 will also work.
It is useless to put getch() after return statement. It will never reach upto that line.
What happens in your version?
Actually when two integers are divided the result is the quotient. If there any fractional part it is discarded. If you divide 5 by 9 the result will be 0.555.... When fractional part discarded it will be 0. So you always get 0.
Whenever you need an outcome of a division not t truncate you must make one of them floating point. That ensures that it(result) will not truncate.
So the program would be
#include<stdio.h>
#include<stdlib.h>
void celciusToFahreinheit();
float fahr_temp,celc_temp;
int main()
{
celciusToFahreinheit();
return EXIT_SUCCESS;
}
void celciusToFahreinheit()
{
printf("\n Enter the temperature (in *F) to covert it into Celsius: ");
if( scanf("%f",&fahr_temp) != 1 ){
fprintf(stderr,"Error in input\n");
exit(EXIT_FAILURE);
}
celc_temp=((5.0/9.0)*(fahr_temp-32));
printf("\n %f*C",celc_temp);
}
Apart from the problem few things would be - Using readable function name and checking the return value of scanf.

can't do multiplication operation on 'double' numbers

I am trying to write some functions for Complex type numbers, but I couldn't make this work.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct{
double a; /* Real*/
double b; /* Imaginary*/
}Complex;
Complex Nmbr1,Nmbr2;
int main()
{
Complex NmbrMulti;
printf("Type the value of the real part of the first number\n");
scanf("%d",&Nmbr1.a);
printf("\nType the value of the Imaginary part of the first number\n");
scanf("%d",&Nmbr1.b);
printf("\nType the value of the real part of the second number\n");
scanf("%d",&Nmbr2.a);
printf("\nType the value of the Imaginary part of the second number\n");
scanf("%d",&Nmbr2.b);
NmbrMulti.a = Nmbr1.a * Nmbr2.a - Nmbr1.b * Nmbr2.b ;
NmbrMulti.b = Nmbr1.a * Nmbr2.b + Nmbr2.a * Nmbr1.b ;
printf("\nThe Multiplication : %d+i", NmbrMulti.a);
printf("%d\n", NmbrMulti.b);
return 0;
}
but I get the result 0+i0 , it seems that the problem is with the operations, is there another way to do multiplication for double numbers that I'm not aware of ?
When scanf sees %d, the corresponding argument must be a pointer to int. If the argument is a pointer to double, the code should be %lf.
Reference
http://www.cplusplus.com/reference/cstdio/scanf/
Formatting for double in C is lf rather than d. The latter implies and int.

Determining if a float has a fractional part?

Here is the problem: The game Totals can be played by any number of people. It starts with a total of 100 and each player in turn makes an integer displacement between -20 and 20 to that total. The winner is the player whose adjustment makes the total equal to 5. Using only the three variables given:
total
adjustment
counter
Here is what I have so far:
#include <stdio.h>
int main (void)
{
int counter=0;
float adj;
int ttl=100;
printf("You all know the rules now lets begin!!!\n\n\nWe start with 100. What is\n");
while (ttl!=5)
{
printf("YOUR ADJUSTMENT?");
scanf("%f",&adj);
counter++;
if (adj<=20 && adj>=-20)
{
ttl=ttl+adj;
printf("The total is %d\n",ttl);
}
else
{
printf ("I'm sorry. Do you not know the rules?\n");
}
}
printf("The game is won in %d steps!",counter);
}
What I need:
When a decimal number is entered it goes to the else. How do I determine if a float has a fractional part.
You can cast the float to an int and then compare it to your original variable. If they are the same there was no fractional part.
By using this method, there is no need for a temporary variable or a function call.
float adj;
....
if (adj == (int)adj)
printf ("no fractional!\n");
else
printf ("fractional!\n");
Explanation
Since an int cannot handle fractions the value of your float will be truncated into an int (as an example (float)14.3 will be truncated into (int)14).
When comparing 14 to 14.3 it's obvious that they are not the same value, and therefore "fractional!" will be printed.
#include <stdio.h>
#include <math.h>
int main ()
{
float param, fractpart, intpart;
param = 3.14159265;
fractpart = modff (param , &intpart);
return 0;
}
http://www.cplusplus.com/reference/clibrary/cmath/modf/
modff finds the fractional part, so I guess testing whether it's equal to 0 or null will answer your question.
if you want to know whether a real number x has no fractional part, try x==floor(x).
I am only learning C so tell me if I am wrong, please.
But if instead of using
scanf("%f",&adj);
if you use:
scanf("%d%d", &adj, &IsUndef);
Therefore if the user typed anything other than a whole integer &IsUndef would not equal NULL and must have a fractional part sending the user to else.
maybe.
Using scanf() is problematic. If the user typed -5 +10 -15 -15 on the first line of input, then hit return, you'd process the 4 numbers in turn with scanf(). This is likely not what you wanted. Also, of course, if the user types +3 or more, then the first conversion stops once the space is read, and all subsequent conversions fail on the o or or, and the code goes into a loop. You must check the return value from scanf() to know whether it was able to convert anything.
The read-ahead problems are sufficiently severe that I'd go for the quasi-standard alternative of using fgets() to read a line of data, and then using sscanf() (that extra s is all important) to parse a number.
To determine whether a floating point number has a fractional part as well as an integer part, you could use the modf() or modff() function - the latter since your adj is a float:
#include <math.h>
double modf(double x, double *iptr);
float modff(float value, float *iptr);
The return value is the signed fractional part of x; the value in iptr is the integer part. Note that modff() may not be available in compilers (runtime libraries) that do not support C99. In that case, you may have to use double and modf(). However, it is probably as simple to restrict the user to entering integers with %d format and an integer type for adj; that's what I'd have done from the start.
Another point of detail: do you really want to count invalid numbers in the total number of attempts?
#include <stdio.h>
#include <math.h>
int main(void)
{
int counter=0;
int ttl=100;
printf("You all know the rules now lets begin!!!\n"
"\n\nWe start with 100. What is\n");
while (ttl != 5)
{
char buffer[4096];
float a_int;
float adj;
printf("YOUR ADJUSTMENT?");
if (fgets(buffer, sizeof(buffer), stdin) == 0)
break;
if (sscanf("%f", &adj) != 1)
break;
if (adj<=20 && adj>=-20 && modff(adj, &a_int) == 0.0)
{
counter++; // Not counting invalid numbers
ttl += adj;
printf("The total is %d\n", ttl);
}
else
{
printf ("I'm sorry. Do you not know the rules?\n");
}
}
if (ttl == 5)
printf("The game is won in %d steps!\n", counter);
else
printf("No-one wins; the total is not 5\n");
return(0);
}
Clearly, I'm studiously ignoring the possibility that someone might type in more than 4095 characters before typing return.

Resources