subtraction

Removing elements from an array in C

混江龙づ霸主 提交于 2019-11-27 13:35:35
I just have a simple question about arrays in C What's the best way to remove elements from an array and in the process make the array smaller. i.e the array is n size, then I take elements out of the array and then the array grows smaller by the amount that I removed it from. basically I'm treating the array like a deck of cards and once I take a card off the top of the deck it shouldn't be there anymore. EDIT: I'm going to drive myself crazy before the end of the day, thanks for all the help I'm trying the value swapping thing but it's not working right. #include <stdio.h> #include <string.h

Why is subtraction faster than addition in Python?

我怕爱的太早我们不能终老 提交于 2019-11-27 11:31:57
问题 I was optimising some Python code, and tried the following experiment: import time start = time.clock() x = 0 for i in range(10000000): x += 1 end = time.clock() print '+=',end-start start = time.clock() x = 0 for i in range(10000000): x -= -1 end = time.clock() print '-=',end-start The second loop is reliably faster, anywhere from a whisker to 10%, depending on the system I run it on. I've tried varying the order of the loops, number of executions etc, and it still seems to work. Stranger,

Algorithm to add or subtract days from a date?

拜拜、爱过 提交于 2019-11-27 08:52:35
I'm trying to write a Date class in an attempt to learn C++. I'm trying to find an algorithm to add or subtract days to a date, where Day starts from 1 and Month starts from 1. It's proving to be very complex, and google doesn't turn up much, Does anyone know of an algorithm which does this? The easiest way is to actually write two functions, one which converts the day to a number of days from a given start date, then another which converts back to a date. Once the date is expressed as a number of days, it's trivial to add or subtract to it. You can find the algorithms here: http://alcor

Subtract time in PHP

微笑、不失礼 提交于 2019-11-27 05:10:32
I have been looking for an answer for a few hours now, but I can't find one. I'm writing a simple script . The user sets their work start and end time. So, for example, somebody is working from 8:00 to 16:00. How can I subtract this time to see how long the person has been working? I was experimenting with strtotime(); but without success... A bit nicer is the following: $a = new DateTime('08:00'); $b = new DateTime('16:00'); $interval = $a->diff($b); echo $interval->format("%H"); That will give you the difference in hours. If you get valid date strings, you can use this: $workingHours =

How to subtract a vector from each row of a matrix? [duplicate]

元气小坏坏 提交于 2019-11-27 04:58:38
Possible Duplicate: How can I divide each row of a matrix by a fixed row? I'm looking for an elegant way to subtract the same vector from each row of a matrix. Here is a non elegant way of doing it. a = [1 2 3]; b = rand(7,3); c(:,1) = b(:,1) - a(1); c(:,2) = b(:,2) - a(2); c(:,3) = b(:,3) - a(3); Also, the elegant way can't be slower than this method. I've tried c = b-repmat(a,size(b,1),1); and it seems slower. EDIT: The winner is this method. c(:,1) = b(:,1) - a(1); c(:,2) = b(:,2) - a(2); c(:,3) = b(:,3) - a(3); EDIT: More methods, and tic toc results: n = 1e6; m = 3; iter = 100; a = rand(1

Subtracting 1 month to 2015-12-31 gives 2015-12-01

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 04:52:03
问题 I'm trying to subtract one month from 2015-12-31 but it gives me 2015-12-01 instead of 2015-11-30 . Why ? Code: var date1 = new Date('2015-12-31'); var date2 = new Date(date1); date2.setMonth(date1.getMonth() - 1); console.log(date1); console.log(date2); Output: Thu Dec 31 2015 01:00:00 GMT+0100 (CET) Tue Dec 01 2015 01:00:00 GMT+0100 (CET) Any workaround? 回答1: Try this var date1 = new Date('2015-12-31'); var date2 = new Date(date1); date2.setDate(date2.getDate()-date1.getDate()); alert(date2

Problems with a shunting yard algorithm

白昼怎懂夜的黑 提交于 2019-11-27 03:34:02
问题 I have successfully implemented a shunting yard algorithm in java. The algorithm itself was simple however I am having trouble with the tokenizer. Currently the algorithm works with everything I want excluding one thing. How can I tell the difference between subtraction(-) and negative (-) such as 4-3 is subtraction but -4+3 is negative I now know how to find out when it should be a negative and when it should be a minus, but where in the algorithm should it be placed because if you use it

Subtraction between two sql queries

 ̄綄美尐妖づ 提交于 2019-11-27 03:08:05
问题 I have 2 queries in MS SQL that return a number of results using the COUNT function. I can run the the first query and get the first result and then run the other one to get the other result, subtract them and find the results; however is there a way to combine all 3 functions and get 1 overall result As in: run sql1 run sql2 run SQL3 (sql1-sql2)?.... I tried them with xxxx as a function but no luck. 回答1: You should be able to use subqueries for that: SELECT (SELECT COUNT(*) FROM ... WHERE ..

Subtracting one row of data from another in SQL

旧巷老猫 提交于 2019-11-27 02:43:45
问题 I've been stumped with some SQL where I've got several rows of data, and I want to subtract a row from the previous row and have it repeat all the way down. So here is the table: CREATE TABLE foo ( id, length ) INSERT INTO foo (id,length) VALUES(1,1090) INSERT INTO foo (id,length) VALUES(2,888) INSERT INTO foo (id,length) VALUES(3,545) INSERT INTO foo (id,length) VALUES(4,434) INSERT INTO foo (id,length) VALUES(5,45) I want the results to show a third column called difference which is one row

How does the CPU do subtraction?

折月煮酒 提交于 2019-11-26 20:29:01
问题 I have some basic doubts, but every time I sit to try my hands at interview questions, these questions and my doubts pop up. Say A = 5, B = -2. Assuming that A and B are 4-bytes, how does the CPU do the A + B addition? I understand that A will have sign bit (MSB) as 0 to signify a positive value and B will have sign bit as 1 to signify a negative integer. Now when in C++ program, I want to print A + B , does the addition module of the ALU (Arithmetic Logic Unit) first check for sign bit and