abhityagi85
BAN USERI think critical section would be required for lockedDescendentsInt
- abhityagi85 August 11, 2015can variable 'lockedDescendentsInt' be > 0? If any of decendent is locked, the node cant be locked :)
- abhityagi85 August 11, 2015#define MY_MAX_INT(x) ((~(x & 0)) & ~(((x & 0)|1) << (sizeof(x)*8-1)))
int x = MY_MAX_INT(x);
long long y = MY_MAX_INT(y)
looks like you have used all the programming you know. Cheers
- abhityagi85 June 09, 2014eugene, I was happy to see pay-as-you-go used by you. Thats the whole point started from your post and now you say equally fast. C++ is based on a design pattern of Object Orient ism. Now thats the whole point of getting slower code. If you say just take a .c file and rename it to .cpp and it will run equally fast. Your ideology of c++ itself is wrong then. C++ means object oriented approach and reusability. as soon as reusability comes into picture, virtual usually start coming with it and ultimately little bit slower code.
You cant just write a "HelloWorld" program and say that -
hey both C and C++ are equally fast. I am not going to reply any more posts as its simple and not worth explaining.
The above is a simplest case.
An advanced case may be to make the destructor private. As constructors may have different signatures while destructor's signature is unique, it will always be hepful.
2 weighings needed
ceil(log3(8)))
It will be ceil( log3(n) ); n=number of objects
where ceil (1.1) = 2;
log3(n) => logrithm of n with base 3.
the cube root logic doesnt hold i believe
- abhityagi85 January 11, 2012implement a 2 bit full synchronous adder; put A=B=0, Cin = Data(bit stream)
Cout S0 S1 will give desired result
bit_pos=0;
while(a && tot_size>bit_pos){
b|=a&(1<<bit_pos++);
}
would this alone wouldn't work??
enum STACK_DIRECTION {UP, DOWN};
STACK_DIRECTION
getStackDiection(
int hi
)
{
int hello;
if((&hi-&hello)>0)
return DOWN;
else
return UP;
}
BFS, a general approach to traverse tree without recursion
- abhityagi85 January 06, 20121,3 but not 2
you do not need to do explicit deallocation in garbage collected environment and so it will not let u do so.
if you allow oops, Put the above methods by Dinesh, chinmoy or sem inside a constructor, that will make a=b sweet rather than enabling and disabling interrupts everytime you do an assignement. Further RAII will take care of enabling the interrupts if something goes wrong
- abhityagi85 January 06, 2012use divide and conquor approach
divide the 32 bit data into 8 bits(or less) and make a LookUpTable with 256 entries corresponding to each 8bit combination. For example
for an 8 bit num, we have 0-255 i.e 256 values;
now let us say that we have an array whose index contains number of 1s for that index(unsigned 8bit)
NumberOf1sAtIndex[]={0,1,1,2,1,2,......., 8};//
so we have 32 bit number right;
we can get number of 1s in a 32 bit number (create a copy of it and call it as num) by following :
for(i=0; i<32/8; i++) {
result+=NumberOf1sAtIndex[(unsigned) (num&0x00FF)];
num>>0x00FF;
}
Thus you can optimize the code depending upon:
(1) extract 4 bits and look up array NumberOf1sAtIndex will be reduce to size 16 rather than 256
(2) if your num will usually contain many zeros compared to 1s, then determine minimum how many zeros are there. Let us say that in each 8 bits there will be 4 or more zeros, then we will have max number 0xF0, then in that case, your NumberOf1sAtIndex size will become 256-2^4. Slight space compaction but significant sometimes
Why double pointer is needed ?
Answer: lets consider two prototypes:
(1) void myAllocate1(void* p, size_t sz);
Here you can pass NULL for p which is syntactically correct but actually incorrect. even if you do p=(void*) malloc(sz);, then this function call is like a call by value; *p=somevalue will be a call by pointer but p=somepointer is call by value;
(2) void myAllocate2(void** p, size_t sz);
Here you have both benefits; p actually is visible outside when function returns and you can not send NULL for p
traverse the lists from head to tail and put the node pointers of each lists in stacks
ListA -> StackA
ListB -> StackB
(1) if they intersect, then the top element of both stacks will be same
(2) if (1) is true, keep popping up till the elements of stack have same value (node addresses) the last popped will be the intersection point
Eg- 100 is the last matched and popped node in the following
2--3--4--5--6--7--
\--- 100--200--300
20--30--40--50-- /
stackA 300--200--100--7--6-- ...--2
stackB 300--200--100--50--40--30--20
not as simple as you have said ;)
- abhityagi85 January 05, 2012harleen, its ok, solution logic is ok
he simply needs to extend same logic in case if rows (M) is odd
if(M%2){
R_ID=(M+1)/2
for(col=0 to NCOL/2)
swap(a[R_ID][col], a[R_ID][NCOL-col])
simple
workable but bad approach
- abhityagi85 January 05, 2012@Haojian Jin
Agree. Just tried to improve the above code even i did smell bad in the very begining
This will fail if input is {1,2,4,5,6,2,2,4,3,5,7,2,2} ie 1,2,1 case is still printed while it is not possible. BUG! You should rather compare the number of occurences of the numbers. I have tried to correct it somewhat.
Also, you always use a[][1] so i guess you dont need a 2d array!
Further, you dont need to decrement the a[].
I believe it should be:
int num[] = {1,2,4,5,6,2,2,4,3,5,7,2,2};
int a[3]={0,0,0};
int i;
for(i=0; i<13; i++) {
if(num[i]<4 && num[i]>0)
a[num[i]-1]=a[num[i]-1]+1;
if (a[2] >0 && a[0] >0) /* To print {3,1}*/
printf("\n3,1");
if (a[1] >0 && a[0] >1) /* To print {1,2,1}*/
printf("\n1,2,1");
if (a [1]>1 ) /* To print {2,2}*/
printf("\n2,2");
if (a[0] >3 ) /* To print {1,1,1,1}*/
printf("\n1,1,1,1");
You can run it online at ideone
www ideone.com/4vpwl
@suma:
yes A && B && C is supposed to be executed from L->R so && was needed. I have corrected it now. Thanks
Also, regarding and string Vs numeric case, I will say (1) We can always convert number to string. (2) more importantly, If we dont convert to string and treat it as integer, think how many times we will have to do / and %.
I believe if S1,S2,S3,S4 are 4 strings that needs be concatenated,
S1="Hello"
S2="How"
S3="Are"
S4="You"
then we can have a string S5 of length = sum of lengths of s1, s2, s3, s4
and then we can construct S5 by simply concatinating s1..s4
Am I missing something here?
@prabha094
fixed it. didn't consider equalities. gr8 that you found it
Wayne, i think conversion doesnt mean to print as hex, but to give a logic to actually convert :)
- abhityagi85 December 22, 2011Can be easily done by comparing the numbers rather than their integer value as suggested in other posts downvoted above.
The following code doesnt take care of exceptions that may occur due to invalid input and other things.
------------------------------------------------------------------------
#include <iostream>
#include <string.h>
using namespace std;
int indexoflargest4digitnumber(char *number);
int main(){
char number[32];
cout<<"Enter number:";
cin>>number;
int maxIndex = indexoflargest4digitnumber(number);
cout.write(number+maxIndex, 4);
return 0;
}
int indexoflargest4digitnumber(char *number) {
if(number == 0)
return -1;
int result = 0;
int length = strlen(number);
int count = 0;
while ( count <= length-4 ) { //9164916500
if( number[result] < number[count] )
result = count;
else if ( number[result] == number[count]){
if( (number[result+1] <= number[count+1]) &&
(number[result+2] <= number[count+2]) &&
(number[result+3] < number[count+3]))
result = count;
}
count++;
}
return result;
}
mystry, how does it matter?
algorithmically both are same. here 9111 is more obvious but 9641 can also be answer which we can find out same way as we can find 9111
downvoted!
uses a lot of heavy complexity arithmetic expressions
Pay-A-You-Go What does it means?
Do you want to say I will not use c++ features so my code will run equally fast. If you dont use c++ features, what do you compare your C code with. There is no point of comparison of C and C++ till you use the c++ features.
There is no point of comparing c and c++ on basic of compiling same code in '.c' and '.cpp' files as mentioned by you as 'functions'.
If you '.c' and '.cpp' extension is not responsible for performance, It is senseless and illogical!!
I liked that "pay-as-you-go."
But it breaks my heart when you said 'ANY' ... C++ has to be slower than C, though negligibly for the reasons stated.
use a link list with each node having an array of type int of some predefined size SIZE. create more and more nodes in link list as the data is read from the file. the last node can have the array partially filled so you will have to count either the total elements read from the file or total valid elements in the array contained in last node.
something like:
#define SIZE 1<<4
typedef struct node_ {
struct node* next;
int elements[SIZE];
}node;
class DataContainer { //LinkList
node * m_pHead;
int m_nTotalElementsContained;
public:
void InsertElement(int nValue);
void getValueAtIndex(int nIndex);
};
Here we have used m_nTotalElementsContained which tells total number of elements. We could also have used m_nTotalElementsContainedInLastArray, which will hold the number of elements in the last node's Array. The total elements in that case could have been calculated by
SIZE*(number of nodes -1) + m_nTotalElementsContainedInLastArray
both having their advantages. Compared to a link list node having a single value rather than array, it saves (TotalElements - (TotalElements / SIZE))*sizeof(int*) number of bytes of memory.
Let us say TotalElements = 1000, SIZE=10, thus it becomes (1000 - 1000/10) * (4) for 32 bit OS
900*4=3600. Thus 3600 bytes are saved for 1000 elements compared to a linklist having 1 value rather than array.
Its simple.
C :Static Polymorphism or Overloading;
C++ :Dynamic Polymorphism.
Implementing dynamic polymorphism in C is like reinventing the wheel (C++ features). If there is a concept, both C, C++ are powerful enough to implement it.
But a simple anwer would be: "No, dynamic polymorphism is not there in C, thats why the world moved to C++."
A C program should be faster than C++ program.
For Example, let us take an example of polymorphism. The C++ has dynamic or runtime polymorphism. So the function call on an object will result in v-table v-function etc etc which will certainly take time. However in C, we have static polymorphism or function overloading in which the flow control is decided at the compile time thus saving the additional steps at runtime.
C++ however makes possible to write better code, reusable but this comes at a little price.
make the constructors private. This will restrict the creation of an object on the stack. The heap object however can be created by the new which can call the constructor.
- abhityagi85 October 18, 2011
When locking a node, why cant we update its parents property that one of the child is locked. That way for locking a node availability for being locked, we can check following 2 things for a node and it's parent:
- abhityagi85 August 11, 2015(1) current Node's parent is locked, which is a flag in the parent node
(2) current Node's parent's decendent is locked. this is again a flag in the parent which can be set by any child (need critical section)