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);
Related
I am composing a code that gives the user the total of their package after weight and mileage is calculated. It was working before but after I made minor changes anything that weighs over 2lbs is not printing out anything. Why is this?
#include <stdio.h>
#include <stdlib.h>
int main(){
int weight;
int miles;
double mileCost;
int segment;
int remainder;
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("%d", &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= miles / 500;
remainder = miles % 500;
if(remainder > 0)
remainder = 1;
if(weight <= 2){
mileCost = 1.50 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 2 && weight >= 6){
mileCost = 3.70 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
if(weight > 6 && weight >= 10){
mileCost = 5.25 * (segment + remainder);
printf("The charge for your package is: %f\n", mileCost);
}
return 0;
}
You have your upper bounds written with greater than or equal to >= instead of less than or equal to <=:
if(weight > 2 && weight >= 6){
if(weight > 6 && weight >= 10){
This should have been
if(weight > 2 && weight <= 6){
if(weight > 6 && weight <= 10){
regarding:
if(weight > 2 && weight >= 6){
1) this should be preceeded by else so it is not even looked at if weight is <= 2
2) the statement should be:
else if(weight > 2 && weight <= 6){
notice the <= 6
3) similar considerations exist for:
if(weight > 6 && weight >= 10){
Here I have created a compounding interest calculator. The user inputs principal, interest% and duration (in quarters). I have used a for loop for the initial calculation. But, I don't know how to get the total to rollover to the next quarter's principal.
Say the user inputs 1000, 5% and 2 quarters. The output should look like, Q1 Principal=$1000, Interest=0.05, Total=$1012.50, Q2 =$1012.50 =0.05 =$1025.16
Also my last Do while is giving me some issues. The ouput is spitting out a couple extra lines before letting the user start over.
Any advice would be greatly appreciated.
Thank you
#include <stdio.h>
int main (void)
{
int a = 0, b=0;
double interest, prin, total=0;
char check = ' ';
do{
do{
do{
printf (" Please enter principal:\n");
scanf ("%lf", &prin);
}while(prin <=0);
do{
printf ("Please enter desired interest greater
than 0 less than 20 :\n");
scanf ("%lf", &interest);
}while(interest <=0 || interest >20);
interest = interest/100;
do{
printf ("For how many quarters would you like
to deposit: (more than 0, less than 40) \n");
scanf ("%d", &b);
}while(b <=0 || b >40);
printf ("Is this information correct? Press X
to continue" );
scanf ("\n%c", &check);
}while(check != 'x' && check != 'X');
total = prin * (1+(interest *.25));
printf ("Quarter Principal Interest
Total\n");
for(a=1; ;++a){
printf ("%2d $%.2f %.2lf
$%.2lf\n", a, prin, interest, total);
if(a == b)
break;
}
printf ("Do you want to start over (Y/N)?");
scanf ("%c\n", &check);
}while(check != 'y' || check != 'Y');
return 0;
}
The are some problem with indentation and logic in your code. You need to updade principle in for loop statement. Then print it out .Here is my solution
#include <stdio.h>
int main(void)
{
int a = 0, b = 0;
double interest, prin, total = 0;
char check = ' ';
do {
do {
do {
printf(" Please enter principal:\n");
scanf("%lf", &prin);
} while (prin <= 0);
do {
printf("Please enter desired interest greater than 0 less than 20 :\n");
scanf("%lf", &interest);
} while (interest <= 0 || interest > 20);
interest = interest / 100;
do {
printf("For how many quarters would you like to deposit : (more than 0, less than 40) \n");
scanf("%d", &b);
} while (b <= 0 || b > 40);
printf("Is this information correct? Press X to continue" );
scanf("\n%c", &check);
} while (check != 'x' && check != 'X');
printf("Quarter Principal Interest Total\n");
for (a = 1; a<=b; ++a) {
total = prin * (1 + (interest *.25));
printf("%2d $%.2f %.2lf $%.2lf\n", a, prin, interest, total);
prin = total;
}
printf("Do you want to start over (Y/N)?");
scanf("%c\n", &check);
} while (check != 'y' || check != 'Y');
return 0;
}
This is my code so far for my simple calculator. Im working on sine right now(case 6) with a degree range of 0-360.Here is the output.
$ ./a.exe ProblemSolving.c
Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)
Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)
Exponent : x^y(11) 2^x(12) 10^x(13)
Enter the choice of operation:6
The choice of operation is:6
Enter degree range from 0 to 360
Enter degrees:400
After I enter the desired degrees nothing else happens and the program ends. I believe there is something wrong with my if statement or the sine function.
#include <stdio.h>
#include <math.h>
int main() {
double Add1, Add2, Sub1, Sub2, Mult1, Mult2;
int Choice, Div1, Div2, Mod1, Mod2, Base1, Power1, Deg1;
printf("Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)\n");
printf("Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)\n");
printf("Exponent : x^y(11) 2^x(12) 10^x(13)\n");
printf("Enter the choice of operation:");
scanf("%d", &Choice);
printf("The choice of operation is:%d\n", Choice);
switch(Choice) {
case 0:
printf("Enter number one:");
scanf("%lf", &Add1);
printf("Enter number two:");
scanf("%lf", &Add2);
printf("%2.2lf + %2.2lf = %2.2lf", Add1, Add2, Add1+Add2);
break;
case 1:
printf("Enter number one:");
scanf("%lf", &Sub1);
printf("Enter number two:");
scanf("%lf", &Sub2);
printf("%2.2lf - %2.2lf = %2.2lf", Sub1, Sub2, Sub1-Sub2);
break;
case 2:
printf("Enter number one:");
scanf("%lf", &Mult1);
printf("Enter number two:");
scanf("%lf", &Mult2);
printf("%2.2lf * %2.2lf = %2.2lf", Mult1, Mult2, Mult1*Mult2);
break;
case 4:
printf("Enter number one:");
scanf("%d", &Div1);
printf("Enter number two:");
scanf("%d", &Div2);
if (Div2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d / %d = %d", Div1, Div2, Div1/Div2);
break;
case 5:
printf("Enter number one:");
scanf("%d", Mod1);
printf("Enter number two:");
scanf("%d", Mod2);
if (Mod2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d % %d = %d", Mod1, Mod2, Mod1%Mod2);
break;
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", Deg1);
if (0 > Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %d", Deg1, sin(Deg1));
break;
default:
printf("Error! operator is not correct");
break;
}
return 0;
}
There are several problems in this code:
Change scanf("%d", Deg1); to scanf("%d", &Deg1);, because scanf() call for addresses. Also, I think it might be better to declare Deg1 as a double.
0 > Deg1 > 360 is wrong in C. You have to write Deg1 < 0 || Deg1 > 360. Operator || stands for "logical Or".
In math.h, sin() works in radians. So use sin(Deg1 * 3.14159265 / 180). Or, to improve readability and maintenance, #define PI 3.14159265 and sin(Deg1 * PI / 180). Note that you cannot write Deg1 / 180 * 3.14159265, because integer literals in C is ints, and int/int = int. For example, 3 / 2 == 1, rather than 1.5. To get the exact value, write 3.0 / 2.0.
In math.h, sin() returns double, so write printf("sin(%d) = %g", Deg1, sin(...));.
Fixed code here:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
// ...many lines of code...
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", &Deg1);
if (0 > Deg1 || Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %g", Deg1, sin(Deg1 * PI / 180));
break;
The sine function (and the rest of the trig functions) in C works in radians, not degrees. You'll have to convert from degrees from radians before passing the value to sine.
Right now you also have a problem with the format in your printf, since you're passing a double, but telling printf to expect an int. You need to use %f instead of %d.
In addition, your if statement doesn't currently make much sense, and almost certainly doesn't mean what you think. What you apparently want is if (Deg1 < 0.0 || Deg1 > 360.0)
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);
}
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.