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
Related
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!!!
;)
I am still learning to code and wrote this code to get the total after the weight and distance are calculated. Can someone tell my why the math is not working?
Example: When I enter 5 for weight and 1500 miles instead of getting 8.20 back I get 3.50.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
double weight,mileCost;
int miles, segment, remainder, stop = 1;
while(stop == 1){
printf("Charge by weight:(We don't tale packages over 10lbs\n");
printf("\n 1-2 lbs: $1.50\n 3-6 lbs: $3.70\n 7-10 lbs: $5.25\n ");
printf("Enter your package's weight:\n");
scanf("%f", &weight);
printf("Charge by mile: \n");
printf("$1.50 for every 500 miles\n");
printf("Enter the total miles for your package:\n");
scanf("%d", &miles);
if(miles == 0 || weight == 0 || weight > 10){
printf("Invalid entry! Try Again.");
}
segment= ceil((double) miles / 500);
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
else if(weight > 2 && weight <= 6){
mileCost = 3.70 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight <= 10){
mileCost = 5.25 * (segment + remainder);
printf("The shipping charge for your package is: %f\n", mileCost);
}
system("pause");
printf("Would you like to ship another package?\n Enter 1 to continue or 0 to stop. \n");
scanf("%d", &stop);
if(stop == 0){
printf("Thank you for your business! \n");
}
}
return 0;
}
Changing double weight, mileCost; to float weight, mileCost; will make it work since you are reading the variable from stdin using scanf("%f", &weight) (for floats) and not scanf("%lf", &weight) (for double).
I am still puzzled by why you do this:
if(remainder > 0)
remainder = 1;
Wouldn't ceil() already handle that in:
segment = ceil((double) miles / 500);
Also, based on the formula you provided, inputting weight=5 and miles=1500 should output 11.1.
You must use "lf" for a reading of a double variable:
scanf("%lf", &weight);
I am doing the following program out of a book and don't understand where I am going wrong with it. Can someone please point out to me some mistake in logic that I am missing?
Develop a program that will input the miles driven and gallons used for each tankful.
The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls.
#include <stdio.h>
int main(void) {
int total = 0, count = 0;
float gallons_used, mpg, miles;
while(gallons_used != -1) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used);
printf("Enter the miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons_used;
printf("Miles / gallon for this tank was %f\n", mpg);
total += mpg;
count++;
}
total /= count;
printf("Average miles to the gallon was: %d\n", total);
return 0;
}
Now, It appears that I have the loop just right, up until the point I exit it with the value of -1 because it still asks for the mileage of that tank, and obviously inputting it completely throws off the total at the end.
You can use an infinite loop and break it just in case gallons_used = -1
for(;;) { // <-- infinite loop
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used);
if (gallons_used == -1)
break; // <-- exit the loop
printf("Enter the miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons_used;
printf("Miles / gallon for this tank was %f\n", mpg);
total += mpg;
count++;
}
while(true) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used);
printf("Enter the miles driven: ");
scanf("%f", &miles);
if(gallons_used== -1 )break;
mpg = miles / gallons_used;
printf("Miles / gallon for this tank was %f\n", mpg);
total += mpg;
count++;
}
#include <stdio.h>
int main(void) {
int total = 0, count = 0;
float gallons_used, mpg, miles;
while(gallons_used != -1) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used);
if (gallons_used < 0) // check gallons_used
break;
printf("Enter the miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons_used;
printf("Miles / gallon for this tank was %f\n", mpg);
total += mpg;
count++;
}
total /= count;
printf("Average miles to the gallon was: %d\n", total);
return 0;
}
You are using gallons_used uninitialized. Using uninitialized variables invokes undefined behavior. You need to initialize it first before comparing it in while's conditional expression. You can do this as
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used); // Reading value for gallons_used
while(gallons_used != -1) {
printf("Enter the miles driven: ");
scanf("%f", &miles);
mpg = miles / gallons_used;
printf("Miles / gallon for this tank was %f\n", mpg);
total += mpg;
count++;
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons_used);
}
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;
I'm trying out programming in C for the first time, and applying it to some concrete stuff...
The program I'm creating with the problem deals with a while loop. The goal of the program is to calculate the average miles per gallon for a set of trucks. I want it to terminate as soon as -1 is inputted as the number of gallons consumed, but instead I have to input it twice, once for the number of gallons, and once for the number of miles. I have found this input to in fact be used as part of the calculation of the result. Here is the code:
#include <stdio.h>
int main()
{
int tanks, miles;
float gallons = 0, average = 0, miles_per_gallon = 0;
tanks = 0;
while (gallons != -1) {
tanks += 1;
miles_per_gallon = (float)miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3f\n",
miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
average /= tanks;
printf("The overall average miles/gallon was %.3f", average);
return 0;
}
Here is some sample output:
C:\>gallons
Enter the gallons used (-1 to end): 12.3
Enter the miles driven: 700
The miles / gallon for this tank was 56.911
Enter the gallons used (-1 to end): 13.4
Enter the miles driven: 666
The miles / gallon for this tank was 49.701
Enter the gallons used (-1 to end): 17.3
Enter the miles driven: 644
The miles / gallon for this tank was 37.225
Enter the gallons used (-1 to end): 15.5
Enter the miles driven: 777
The miles / gallon for this tank was 50.129
Enter the gallons used (-1 to end): -1
Enter the miles driven: -1
The miles / gallon for this tank was 1.000
The overall average miles/gallon was 38.993
Thanks for any help.
The problem is that the sequence of statements in your code is such that the check for the loop's exit condition is not reached until after the second input is requested. You could add a check for -1 as soon as it's entered, and break out from the loop. Alternatively, you could ask for the miles to be entered ahead of the gallons.
for (;;) { /* This is a "forwver" loop; we break out from the middle */
tanks += 1;
miles_per_gallon = (float)miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3f\n",
miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
/* This is where you break from the loop: */
if (gallons == -1) return 0;
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
Well you should be able to work it out by yourself, it is easy if you just change your loop or put a if statement after gallons input
while (gallons != -1) {
tanks += 1;
miles_per_gallon = ( float ) miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3f\n", miles_per_gallon);
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons==-1){
printf("Program terminated");
return 0;
}
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
Check for the exit condition after reading gallons. I've made a couple of changes to you code --- because you are only breaking after reading gallons, i've changes the while condition to true. second I changed your test to <=0 as if 0 is entered you divide by 0 which will break your math, and anything less than 0 simply makes no sense. Thirdly I changed the calculation and reporting to after reading the values, so you don't divide by zero
while (1) {
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if(gallons <= 0) break;
printf("Enter the miles driven: ");
scanf("%d", &miles);
tanks += 1;
miles_per_gallon = ( float ) miles / gallons;
average = average + miles_per_gallon;
printf("The miles / gallon for this tank was %.3f\n", miles_per_gallon);
}
while (gallons != -1) {
/* snip */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
Note that you only evaluate the exit criteria after asking both questions. If you want to avoid asking for the miles, you have to contort your loop a little further. It'll look something like this:
while (gallons != -1) {
/* snip */
printf("Enter the gallons used (-1 to end): ");
scanf("%f", &gallons);
if (gallons == -1)
break; /* exit the while loop */
printf("Enter the miles driven: ");
scanf("%d", &miles);
}
Of course, exiting a loop in the middle like this is a bit rough around the edges, but I don't immediately see a more convenient way to terminate the loop without asking the second question.