In C , how do I make 1200 / 500 = 3.
I'm doing a homework assignment.
Shipping Calculator: Speedy Shipping company will ship your package based on how much it weighs and how far you are sending the package. They will only ship small packages up to 10 pounds. You need to have a program that will help you determine how much they will charge. The charges are based on each 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same charge as 900 miles.
Here is the table they gave you:
Package Weight--------------------------Rate per 500 miles shipped
2 pounds or less------------------------$1.50
More than 2 but not more than 6---------$3.70
More than 6 but not more than 10--------$5.25
Here is one test case.
Test Case Data:
Weight: 5.6 pounds
Miles: 1200 miles
Expected results:
Your shipping charge is $11.10
My answer keeps coming out to 7.40
Are you trying to round up? Before dividing, you could add 499 to the number that is being divided.
(0 + 499) / 500 -> 0
(1 + 499) / 500 -> 1
(1200 + 499) / 500 -> 3
This will round up.
Say you want to get a ceiling division a by b (in your example a = 1200 b = 500).
You can do it in integer arithmetic like this.
result = (a + b - 1) / b;
Or you could use floating point numbers and do it like this (probably a bad idea)
result = (int) ceil( (double) a / b );
The thing is that as this is a homework, you could just make it up in small steps:
if( a % b == 0 ) {
result = a / b;
} else {
result = a / b + 1;
}
Another advantage of this code is that it actually doesn't overflow for too big as, but this is not relevant in this case, I guess.
I'd suggest using the mod and truncate functions. If mod comes out zero, it's fine, otherwise truncate and add 1.
You have to use the ceiling of the division. This will round the quotient up to the next integer.
So when you are trying to find the number of 500-mile increments, you have to round the quotient up to the next integer.
Alternatively, (and inefficiently), you could increment the number of miles by 1, until it is divisible by 500...that is, while ( (q = x_miles++%500) != 0 ) {} . Then multipy q by the rate to get your answer (That is also assuming you will have an integer number of miles).
You could also use the stdlib div function. This might be nice if you only wanted integer math and specifically wanted to avoid floating point math.
http://www.cplusplus.com/reference/clibrary/cstdlib/div/
#include <stdlib.h>
int foo(void)
{
div_t result = div(1200, 500);
return result.quot + (0 < result.rem);
}
[EDIT1]
From your code you would implement this part as follows:
if ( weight <= 5.6 )
{
int multiplier = (int) miles / 500;
if( ((int)miles % 500) > 0)
multiplier++;
rate370 = (double)multiplier * 3.7;
printf("Your total cost : %.2lf\n", rate370);
}
[ORIGINAL]
In "integer land" 1200 / 3 should equal to 2.
for what it "seems" you want try this:
int multFiveHundreds = (int)totalWeight / 500;
if(multFiveHundreds % 500 > 0)
multFiveHundreds++;
Related
I have a rounding discrepancy in SQL that I could do with a hand resolving.
I have 2 SQL calculations, the first one equals 1.1 and the second 5.65 (see below)
round((sum((monthly_markup)+100) / 100) / sum(monthly_qty),2) as timesby, --equals 1.1
sum(monthly_buy)/sum(monthly_qty) as buy, -- equals 5.65
If I then take those calculations and do calc1 x calc2 it equals 6.21
cast (round(sum(monthly_buy)/sum(monthly_qty) * (sum((monthly_markup)+100) / 100) / sum(monthly_qty),2) as decimal (30,2)), -- equals 6.21 !!
But I am expecting 6.22, as per the below calculation
cast (round((5.65 * 1.1),2) as decimal (30,2)) -- equals 6.22
How can I get my calculation to return 6.22?
Thanks
In case it helps anyone else, I resolved by casting the sum of buy * qty to decimal first.
round((cast(sum(monthly_buy)/sum(monthly_qty) as decimal (5,3)) * round((sum((monthly_markup)+100) / 100) / sum(monthly_qty),2)),2)
I have the following problem in my simulation.
A is an array 24 x 2. I am going to split it and get 4 or 12 array. It means that I group 6 or 2 array. It will be ok, if I use even "split" coefficient. If it is odd, I can"t split A.[ I can't group 5 or 7, because of 24/5=4*5 + 4 ( or 5*5 -1) or 24/7=7*3+3.
That's why I going to do the following:
If I have 24 x 2 and need group every 5 together:
block 1 : A(1,:), A(2,:),A(3,:),A(4,:),A(5,:)
block 2 : A(6,:), A(7,:),A(8,:),A(9,:),A(10,:)
block 3 : A(11,:), A(12,:),A(13,:),A(14,:),A(15,:)
block 4 : A(16,:), A(17,:),A(18,:),A(19,:),A(20,:)
block 5 : A(21,:), A(22,:),A(23,:),A(24,:), ?
As you can see the 5th block is not full, Matlab gives me an error. My idea is to create A(25,:)=0. For my simulation it will be ok.
I am going to simulate it as function:
A=rand(m,n)
w- # number of a vector that i would like group together ( in ex., it is `5`)
if mod(w,2)==0
if mod(m,2)==0
% do....
else
% remainder = 0
end
else
if mod(m,2)==0
% remainder = 0
else
%do...
end
I was going to simulate like above, but then I have noticed that it doesn't work. Because 24/10 = 2*10+4. So I should write something else
I can find the reminder as r = rem(24,5). As an example above, MatLab gives me r=4. then I can find a difference c= w-r =1 and after that, I don't know how to do that.
Could you suggest to me how to simulate such a calculation?
Determine the number of blocks needed, calculate the virtual amount of rows needed to fill these blocks, and add as many zero rows to A as the difference between the virtual and actual amount of rows. Since you didn't mention, what the actual output should look like (array, cell array, ...), I chose a reshaped array.
Here's the code:
m = 24;
n = 2;
w = 5;
A = rand(m, n)
% Determine number of blocks
n_blocks = ceil(m / w);
% Add zero rows to A
A(m+1:w*n_blocks, :) = 0
% Reshape A into desired format
A = reshape(A.', size(A, 1) / n_blocks * n, n_blocks).'
The output (shortened):
A =
0.9164959 0.1373036
0.5588065 0.1303052
0.4913387 0.6540321
0.5711623 0.1937039
0.7231415 0.8142444
0.9348675 0.8623844
[...]
0.8372621 0.4571067
0.5531564 0.9138423
A =
0.91650 0.13730
0.55881 0.13031
0.49134 0.65403
0.57116 0.19370
0.72314 0.81424
0.93487 0.86238
[...]
0.83726 0.45711
0.55316 0.91384
0.00000 0.00000
A =
0.91650 0.13730 0.55881 0.13031 0.49134 0.65403 0.57116 0.19370 0.72314 0.81424
0.93487 0.86238 0.61128 0.15006 0.43861 0.07667 0.94387 0.85875 0.43247 0.03105
0.48887 0.67998 0.42381 0.77707 0.93337 0.96875 0.88552 0.43617 0.06198 0.80826
0.08087 0.48928 0.46514 0.69252 0.84122 0.77548 0.90480 0.16924 0.82599 0.82780
0.49048 0.00514 0.99615 0.42366 0.83726 0.45711 0.55316 0.91384 0.00000 0.00000
Hope that helps!
For some calculations, I need to get the closest number in tens, hundreds, thousands...etc of a given number. Examples:
1 becomes 10
3 becomes 10
15 becomes 20
36 becomes 40
105 becomes 110
1009 becomes 1010
... etc
Thanks in advance!
Simple javascript add reminder to number. by using mod of unit digit
Use roundOff function from below example pass digit it will return.
function roundOff(num){
return (10 - (num%10)) + num
}
console.log("round off of 1009",roundOff(1009))
console.log("round off of 105",roundOff(105))
console.log("round off of 36",roundOff(36))
You can do this.
let i = 105;
let result = (parseInt(i/10, 10)+1)*10;
console.log(result);
Revansiddh answer is really nice, only to 'fullier' answer the question:
function roundOff(num, precision = 10){
return (precision - (num%precision)) + num
}
console.log("round off of 1009",roundOff(1049))
console.log("round off of 105",roundOff(1049, 100))
console.log("round off of 36",roundOff(1049, 1000))
C Time Difference
scanf ("%2d %2d", &shours, &sminutes);
printf ("Enter End Time : ");
scanf ("%2d %2d", &ehours, &eminutes);
printf ("\nTIME DIFFERENCE\n");
tohours = ehours - shours;
printf("Hour(s) : %2d", tohours);
tominute = eminutes - sminutes;
printf("\nMinute(s): %2d ", tominute);
How can I make my output like this? When I try to run my code the minutes output is -59 instead of 1 and my hours is the one who got the output "1"
P.S. without using the if else statements
Use (some sort of) timestamps, by turning your hours and minutes variables into one, e.g:
stime = shours * 60 + sminutes;
etime = ehours * 60 + eminutes;
then calculate de difference of that
totime = etime - stime;
then convert that back into hours and minutes
tominutes = totime % 60;
tohours = (totime - tominutes) / 60;
(integer division will take care of rounding down)
Not the most elaborated solution, but I guess you're looking for an beginners-friendly solution
Edit
speaking of beginner-friendly: the % is the modulus operator that returns the remainder of a division. So when you divide 119 by 60 it returns 59. And yes, you could also just get the hours from dividing totime by 60 and let the integer division do the job, but it's nicer (read: clearer to read what's going on) when you divide (totime - tominutes) because it's like the missing part to the line with the modulus
Similar questions have been asked before, but none have addressed why the AVG function in MSSQL produces a value different from explicit SUMand-divide when no NULL values are present in the data.
I would like to calculate the score for the data in this (simplified) TestTable.
CorrectCount IncorrectCount
5.0 0.0
3.0 2.0
5.0 0.0
4.0 0.0
3.0 0.0
5.0 0.0
2.0 1.0
5.0 0.0
5.0 0.0
2.0 2.0
The score is calculated as score = sum(CorrectCount) / sum(CorrectCount+IncorrectCount)
The following query
SELECT
AVG(CorrectCount / (CorrectCount+IncorrectCount)) as ScoreAverage,
SUM(CorrectCount) / SUM(CorrectCount+IncorrectCount) as ScoreSumDivide
FROM TestTable
produces this output:
ScoreAverage ScoreSumDivide
0.876666666666667 0.886363636363636
Where does this difference come from? What does AVG do different than the SUMand-divide? I am looking for an explanation why the result of the AVG is different from the explicit SUM(CorrectCount) / SUM(CorrectCount+IncorrectCount). I expect it is due to precision or rounding internal to the AVG function.
Probably my comment was not understood, so I am expanding it here. Assume you have Correct/Incorrect counts as:
5/2
3/1
Averaging Correct/(Correct+Incorrect) means (5/7 + 3/4)/2 = 41/56
However Sum( Correct) /sum(Correct+Incorrect) means ( 5+3 )/(7+4) = 8/11
41/56 != 8/11
If you did : Sum(Correct /(Correct+Incorrect))/Count it would be: (5/7 + 3/4)/2 = 41/56 which is equal to avg.
It is simply how the math works out. Your score average takes an average of the individual percentages. If C1, C2 ā¦ Cn is your correct scores and I1, I2 ā¦ In is your incorrect scores and āNā is the number of records then the math will look as follow:
C1/(C1+ I1) + C2 /(C2 + I2) + ... + Cn /(Cn + In)
-------------------------------------------------
N
Your sum average first sum all of your correct scores and sum all your total scores and then calculate the percentage ratio. The math of this look as follow:
C1 + C2 + ... + Cn
----------------------------------------------
(C1+ I1) + (C2 + I2) + ... + (Cn + In)
Both numbers are meaningful but the second will better reflect what the percentage of correct counts where for the entire data set.
I think you are simply calculating different stuff there. The aquivalent for the AVG should be SUM(CorrectCount / (CorrectCount + IncorrectCount)) / COUNT(*).
SELECT
AVG(CorrectCount / (CorrectCount+IncorrectCount)) as ScoreAverage,
SUM(CorrectCount / (CorrectCount + IncorrectCount)) / COUNT(*) ScoreSumDivide
Your average calculation wants modification;
,AVG(CorrectCount) / (AVG(CorrectCount)+AVG(IncorrectCount)) as ScoreAverage
This returns the correct value of 0.886363 (39 / 44) rather than what looks like a rounding issue without it.