I'm trying to create a program which will calculate the same date in three different ways. I'm currently stuck on calculating the day of the week, as I need this to calculate the ISO week day. I've got an algorithm that I can use, and it is the one which I've got in my code, with the only difference being that the % sign in my code is replaced by the word "mod" in the algorithm.
When I run this, I get an error saying "Expected expression before % token". I've looked this up but didn't find any results. I've also tried to look at other ways of doing it, and found the Sakomoto Algorithm, but I don't exactly understand how that works. For a possible solution, I was thinking that I maybe need to create a function called mod, but I'm not entirely sure what I would need to put in there.
int day_of_the_week(int year)
{
int week_day;
week_day = %(1+5 * %(year - 1, 4) + 4 * %(year - 1, 100) + 6 * %(year-1,
400), 7);
printf("The day of the week is %d\n", week_day);
return 0;
}
Gauss'
R(1 + 5R(A - 1, 4) + 4R(A - 1, 100) + 6R(A - 1, 400), 7)
should be equivalent to
int week_day = (1 + 5 * (year - 1) % 4) + 4 * ((year - 1) % 100) + 6 * ((year - 1) % 400) % 7;
Related
I am trying to have an SSIS derived column that will transform HH:MM:SS from a flat file to just seconds in the database.
The issue I am having and not seeming to find out how to online is when the HH is over 24.
For instance one of the csv file has 178:29:00.
I was using (DT_NUMERIC,10,2)(((DT_I4)SUBSTRING([SPEED OF ANSWER],1,2) * 3600) + ((DT_I4)SUBSTRING([SPEED OF ANSWER],4,2) * 60) + ((DT_NUMERIC,4,2)RIGHT([SPEED OF ANSWER],2))) when the hours was under 24.
But that does not work if it is over 24.
here is a script component.
string time = row.YourTimeRowAsString;
var t = time.Split(':');
int secs = 0;
switch(t.length)
{
case 0:
break;
case 1:
secs = int.Parse(t[0]);
break;
case 2:
secs = 60* int.Parse(t[0]) + int.Parse(t[1]);
break;
case 3:
secs = 60*60*int.Parse(t[0]) + 60*int.Parse(t[1]) + int.Parse(t[2]);
break;
}
one way to do it is to split the varchar into different values, and then just sum them.
What you did wrong in your code is that you did not take the number of digits for the hour part in account.
Here is an example on how you could do that, the example is in SQL because I don't know much of ssis.
Hopefully it can help you
declare #s varchar(20) = '178:29:00'
select convert(int, left(#s, charindex(':', #s) - 1)) * 3600 +
convert(int, substring(#s, charindex(':', #s) + 1, 2)) * 60 +
convert(int, right(#s, 2))
This is without checking if there are invalid values, you still have to do that.
I also assume that the minute and the second part are always 2 digits long
I suspect my question has a very simple answer, and yet I cannot find an answer that I understand in searching all over the site.
Ticker1 = an array of stock prices (1 to 2542)
PctChg1 = an array which I hope will ultimately hold the results of
(Price_last / Price_prev) - 1
i = counter
Code:
For i = 2 To UBound(Ticker1)
PctChg1(i, 1) = Ticker1(i, 1) / Ticker1(i - 1, 1) - 1
Next i
Something about the Ticker1(i - 1, 1) part it does not like - I have fooled around with ranges as well and cannot make sense of this. I don't know why it thinks I am dividing by a number that isn't there - if i am starting at point 2 of the array, why wouldn't I be able to reference point 1 ?
For i = 2 To UBound(Ticker1)
If Val(Ticker1(i - 1, 1)) <> 0 Then PctChg1(i, 1) = Ticker1(i, 1) / Ticker1(i - 1, 1) - 1
Next i
This works, but I still have not established why i-1 generates errors when I start the calculation on data point i=2.
I have two functions written that have simple assignment statements with very simple expressions. The expressions are the same for both functions, however, they involve different variable types: One function uses an array of structs, the other just uses a typedef'd struct.
When running the functions, the second function fails to divide by 256, and I get very high values that are not "normalized". I have to uncomment the second line in the second function (valueB = valueB / 256) to get it to work.
The first function, however, works perfectly.
Heres the statement in Function One:
value = ((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area) / 256);
Heres the statement in Function Two:
valueB = ((dataPoints.p0B * p2Area)+(dataPoints.p1B * p3Area)+(dataPoints.p2B * p0Area)+(dataPoints.p3B * p1Area) / 256);
//valueB = valueB / 256;
Why would this happen?
Also, I pass the functions the same numbers and it doesn't seem to help.
This is on MacOSX 10.6.8, inside Xcode 3.2.6
Are you absolutely sure the first one works properly? You have
value = ((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area) / 256);
I think you want:
value = (((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area)) / 256);
Similar thing with the second. I think it should be:
value = (((p[0].value * p2Area)+(p[1].value * p3Area)+(p[2].value * p0Area)+(p[3].value * p1Area)) / 256);
In both cases I think you want to divide the sum of the products by 256. Not just the last one. My change only involves placing an extra set of parentheses around the sum of the product subexpressions and dividing the entire thing by 256
In all languages there is an order by which mathematical (and all other operators are completed). It just so happens that * and / are higher in precedence than + and - in C/C++ You may refer to this link for more details.
To simplify what happened to you, I will create this simple equation:
2 + 4 + 6 + 4 / 2
Since division occurs first (and there are no parentheses to alter the order) it gets computed as:
2 + 4 + 6 + (4 / 2) = 14
Not:
(2 + 4 + 6 + 4) / 2 = 8
So my change to your code was the same as putting parentheses around 2 + 4 + 6 + 4 / 2 giving (2 + 4 + 6 + 4) / 2 and forcing the division to be done last after all the additions are completed.
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++;
http://www.freebsd.org/cgi/cvsweb.cgi/~checkout~/src/usr.bin/tar/Attic/getdate.y?rev=1.9.12.1;content-type=text%2Fplain;hideattic=0
I am trying to understand how yyTimezone is calculated in code below:
| bare_time '+' tUNUMBER {
/* "7:14+0700" */
yyDSTmode = DSToff;
yyTimezone = - ($3 % 100 + ($3 / 100) * 60);
}
| bare_time '-' tUNUMBER {
/* "19:14:12-0530" */
yyDSTmode = DSToff;
yyTimezone = + ($3 % 100 + ($3 / 100) * 60);
}
How I understand is, lets say the timestamp is 2011-01-02T10:15:20-04:00; this means its 0400 hours behind UTC. So to convert it into UTC, you add 0400 hours to it and it becomes 2011-01-02T14:15:20. Is my understanding correct?
How is that achieved in the codeblock I pasted above?
The input would encode the offset like -0400. The 0400 part of that would be returned as the tUNUMBER token (presumably holding an unsigned value). This token is matched by the grammar rules, and can be used as $3.
To get the actual offset in minutes from the value 400, you first have to split it up in two halves. The hours part can be obtained with $3 / 100 (ie. 4), and the minutes part with $3 % 100 (ie. 0). Since there are 60 minutes in an hour, you multiply the hours by 60, and add the minutes to that ($3 % 100 + ($3 / 100) * 60), which would give the value 240. Then all that's left, is to add the sign, and store it in yyTimezone.
After all that, yyTimezone will contain the timezone offset in minutes.