Output isn't true - c

Here is my code:
#include <stdio.h>
int main()
{
int inches = 0;
int yards = 0;
int feet = 0;
const int inches per foot = 12;
const int feet_per yard = 3;
printf("Enter a distance in inches: ");
scanf ("%d", &inches);
feet = inches/inches_per_foot;
yards = feet/feet_per_yard; feet %= feet_per_yard;
inches %= inches_per_foot;
printf("That is equivalent to %d yards %d feet and %d inches.\n",
printf("or %d centimeters\n", inches*2.54);
}
Input:
Enter a distance in inches: 10
Output:
That is equivalent to 0 yards 0 feet and 10 inches.
or 1717986918 centimeters.
So, what is 1717986918?

So, what is 1717986918?
It's what happens when you try to print a double as if it were an int.
printf("or %d centimeters. \n", inches *2.54);
Because inches *2.54 involves a multiplication by a floating point number, inches is implicitly cast to double and the result is a double. Use %f to print a double
printf("or %f centimeters. \n", inches *2.54);

Related

Simple C Coding Problem: Skipping Over Printf and Skipping User Input

https://onlinegdb.com/-9jZX-uU0 [link]
I want to have the second line of code to have the user input the Fahrenheit not the Celsius twice. The int main() code is correct and the output number is correct but the user does not input the Fahrenheit. How do I fix this? enter image description here
What is expected is that the user inputs the Fahrenheit and the code continues. I do not want the output to skip over lines 12-14. I don't know how to fix this. I expect that once the user inputs Fahrenheit the code will continue to run as it currently does
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius()
{
double temp_celcius, temp;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_celcius();
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f
ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis
equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f
ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit
is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
You used used return twice in the function so that it won't go ahead for asking the fahrenhiet question. Whenever return is encountered while compiling it terminates the function there and there only.
Now what you can do is that just make another function for fahrenhiet and copy paste the fahrenhit part in it.
#include <stdio.h>
#include <math.h>
double ask_for_air_celcius(){
double temp_celcius;
printf("Enter the current air temperature(in Celcius): ");
scanf(" %lf", &temp_celcius);
return(temp_celcius);
}
double ask_for_air_Fahrenhiet(){
double temp;
printf("Enter the current air temperature(in Fahrenheit): ");
scanf(" %lf", &temp);
return (temp);
}
int main()
{
double temp_celcius, spsound_1,spsound_convert_1,temp,
spsound,spsound_convert;
{
temp_celcius = ask_for_air_celcius();
spsound_1 = 1086 * sqrt(((5 * (temp_celcius*1.8+32)) + 297)
/ 247);
spsound_convert_1=spsound_1*1.09728;
temp = ask_for_air_Fahrenhiet();//changes here
spsound = 1086 * sqrt(((5 * temp) + 297) / 247);
spsound_convert=spsound*1.09728;
printf("\nThe speed of sound at %.2f degrees Celcius is %.2f ft/s.\n", temp_celcius, spsound_1);
printf ("The speed of sound at this temperature in Celciusis equivalent to %.2f km/h.\n\n", spsound_convert_1);
printf("The speed of sound at %.2f degrees Fahrenheit is %.2f ft/sec.\n", temp, spsound);
printf ("The speed of sound at this temperature in Fahrenheit is equivalent to %.2f km/h.\n", spsound_convert);
}
return(0);
}
Hope it helps!!!
;)

uninitialized local variable used in C language

so i have been at this for a bit and i am a little stumped i am getting "uninitialized local variable gallons used". in my take_Input function on conversion(gallons);
i am aware that that means that the value for gallons is not being recognized.
why is the gallons to liters function not placing a value for gallons so that the conversion function has that value. any help appreciated thanks...
code:
double take_Input(void)
{
double a, b, c, d;
double gallons;
printf("please enter how many liters of A: ");
scanf("%lf", &a);
printf("please enter how many gallons of B: ");
scanf("%lf", &b);
printf("please enter how many liters of C: ");
scanf("%lf", &c);
printf("please enter how many gallons of D: ");
scanf("%lf", &d);
gallons_To_Liters(a,b,c,d);
conversions(gallons);
return(0);
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return(0);
}
double conversions(double gallons)
{
double totalGallons = gallons;
double quarts = totalGallons * 4;
double pints = totalGallons * 8;
double cups = totalGallons * 16;
double fluid_ounces = totalGallons * 128;
double tablespoons = totalGallons * 256;
double teaspoons = totalGallons * 768;
// output statements.
printf("the amount of gallons is: %.2f \n", totalGallons);
printf("the amount of quarts is: %.2f \n", quarts);
printf("the amount of pints is: %.2f \n", pints);
printf("the amount of cups is: %.2f \n", cups);
printf("the amount of fluid ounces is: %.2f \n", fluid_ounces);
printf("the amount of tablespoons is: %.2f \n", tablespoons);
printf("the amount of teaspoons is: %.2f \n", teaspoons);
return (0);
}
Your gallons_To_Liters function sets the local variable gallons, but does nothing with it. You need to return this value from the function.
Then in the calling function, you need to assign the return value of gallons_To_Liters to the gallons variable in that function.
double take_Input(void)
{
....
gallons = gallons_To_Liters(a,b,c,d);
....
}
double gallons_To_Liters(double a, double b, double c,double d)
{
double gallons, liters;
liters = a + c;
gallons = b + d;
gallons = (liters * 3.79) + gallons;
return gallons;
}
You need to keep in mind that variables in different functions are distinct from each other, even if their names are the same.
Also, for the take_Input and conversion functions, their return values are not used for anything, so change the functions to have a return type of void and remove the return statements from those functions.

Loop with rolling total in c

I am trying to get a c program to ask me how many items i am buying than to ask the price for each item while keeping a rolling total and than ask again in a loop. I have the loop working fine but am having problems getting it to give me proper output.
here is the output i am getting when i run it.
http://i119.photobucket.com/albums/o134/halodude808/assignment2_zpsd46e84b8.jpg
#include <stdio.h>
#include <math.h>
int main(void)
{
//variables used
float penny = .01;
float nickel = .05;
float dime = .1;
float quarter = .25;
int items = 0;
float paid= 0.0;
float total = 0.0;
float price =0.0;
int counter =0.0;
for (;;)
{
printf("Please enter the number of grocery items:");
scanf("%d", &items);
for (counter = 1; counter <= items; counter++)
{
printf("Please enter the price for item #%d:", counter);
scanf("%f", &price);
total += price;
}
printf("items = %d total = %f \n", &items, &total);
getchar();
getchar();
}
}
Change
printf("items = %f total = %f \n", &items, &total);
to
printf("items = %i total = %f \n", items, total);
Also, you might want to consider checking for invalid values (zeroes, negative, characters, etc).

Conversion is not correct in C program?

I have created this program, it converts miles into km but answer is wrong when i compare it with my Phone's result. But in program everything is fine.
int main(void) {
char i;
float km, miles;
do {
printf("Enter Distance in Miles: "); scanf("%f", &miles);
km = miles * 1.906;
printf("Distance in KM is: %.2f \n", km);
printf("Enter Y/y to continue or any other key to stop."); scanf(" %c", &i);
}
while( i == 'y' || i == 'Y' );
}
Your conversion factor is wrong: there are 1.609 km in 1 mile.
More precisely, 1 foot is 0.3048 m (by definition), so 5280 ft (1 mile) is (5280 * 0.3048 / 1000) = 1.609344 km.
Your formula is wrong, the correct one is:
km = miles * 1.609

Calculate average miles per gallon

Are there any C language programmers on here that can help me figure this out?
I am having trouble getting the calculation for the average miles per gallon to work and my head is spinning. I would really appreciate if anyone have a solution ^_^
int x, number_of_tanks = 3;
double total_num1, total_num2;
double total_miles_per_gallon;
float division, avg;
float num1, num2;
for (x = 1; x <= 3; x++)
{
printf("Enter the number of gallons used for tank #%i: ",x);
scanf("%f", &num1);
fflush(stdin); /* clear input buffer */
printf("Enter the number of miles driven: ");
scanf("%f", &num2);
fflush(stdin); /* clear input buffer */
/*--------------------------------------------------------------*/
/* calculate and output the miles per gallon from user input. */
/* ------------------------------------------------------------ */
division = num2 / (float) num1;
printf("The miles per gallon for this tank %.1f divided by %.1f is %.1f", \
num2, num1, division);
total_num2 = total_num2 + num2;
printf("The total of miles is %f\n", total_num2);
total_num1 = total_num1 + num1;
printf("The total of gallons is %f\n", total_num1);
}
avg = (double) total_num2 / total_num1;
printf("Overall average miles per gallon for three tanks: %.1f", avg);
You don't initialise your totals, so they are undefined. When you start adding to them, you get undefined results. I bet that's what you mean by it not working.
Do this:
double total_num1 = 0;
double total_num2 = 0;

Resources