juan.jose.martin.marcos.gomez
BAN USER
and how are you supposed to change the pointer itself? You definitely need to pass a pointer to what you are going to change inside the function body, the "char *", so you'll need a "char **".
- juan.jose.martin.marcos.gomez August 25, 2012I don't get to understand why you guys use a vector or an array, when we only need to count how many times the function is called and when was the first one inside the last "t" seconds. Something like this (pseudocode):
define x, t
bool foo()
{
static count = 1
static begin = now
currentTime=now
if (currentTime-begin > t)
{
count = 1
begin = now
}
else
{
++count
}
if (count >= x)
return true
else
return false
}
This is O(1), isn't it?
- juan.jose.martin.marcos.gomez August 24, 2012Nice one for unsigned integer.
I'd only add error management, in case a not-an-integer were part of the string. In that case, you'd still return an integer.
And check for the sign in first place, as the atoi function does it.
What about this variation (in C++) of Kadane's algorithm, usefull for positive or negative numbers?
#include <iostream>
int calculateSum(int* a, size_t len)
{
int sum =a[0]+a[1], aux=a[0];
for (int i=1;i<len;++i)
{
aux=aux+a[i];
if (aux>sum)
sum=aux;
aux=a[i];
}
return sum;
}
int main()
{
int a[] = {-10, -12, -7, -5, -4, -13};
std::cout<<"Sum = "<<calculateSum(a, sizeof(a)/sizeof(a[0]))<<std::endl;
}
P.S.: I understood consecutive as adjacent, so I guess it isn't valid.
My implementation in C++ of the Kadane's algorithm:
#include <iostream>
#include <limits>
long calculateSum(int* a, size_t len)
{
long maxSum(std::numeric_limits<long>::min()), currentMaxSum(0);
int maxStartIndex(0), maxEndIndex(0), currentStartIndex(0);
for (int currentEndIndex=0; currentEndIndex<len; ++currentEndIndex)
{
currentMaxSum = currentMaxSum + a[currentEndIndex];
if (currentMaxSum > maxSum)
{
maxSum = currentMaxSum;
maxStartIndex = currentStartIndex;
maxEndIndex = currentEndIndex;
}
if (currentMaxSum < 0)
{
currentMaxSum = 0;
currentStartIndex = currentEndIndex + 1;
}
}
return(maxSum);
}
int main()
{
int a[] = {-2, 4, 2, 3, -12, 5, 9, -3, 4};
std::cout<<"Sum = "<<calculateSum(a, sizeof(a)/sizeof(a[0]))<<std::endl;
}
With those two lines it will work only when the power is negative even, not for odd.
What about this code to fix it?
#include <iostream>
double power(int a,int b)
{
std::cout<<a<<"^"<<b<<";";
double temp;
bool signedExp(b<0?true:false);
b=abs(b);
if(b==0)
{
return 1;
}
temp=power(a,b/2);
if (!signedExp)
{
if(b%2==0)
{
return(temp*temp);
}
else
{
return(temp*temp*a);
}
}
else
{
if(b%2==0)
{
return(1/(temp*temp));
}
else
{
return (1/(temp*temp*a));
}
}
}
int main()
{
int a(5),b(-5);
std::cout<<a<<"^"<<b<<"="<<power(a,b)<<std::endl;
}
Repaliciajdew, Integration Software Engineer at Absolute Softech Ltd
I have a strong base in marketing and I believe in focused strategies that bring brands to their relevant audiences ...
Now I get it.
- juan.jose.martin.marcos.gomez August 30, 2012Thanks, Vick.