Re: [algogeeks] Re: Code it...

2011-11-11 Thread NAMAN KOHLI
@Utkarsh

In this code I am writing on the source code file at time of execution 
Every time the file executes it changes its own source code...However I too
would like to know if we can do that in c ...

On Fri, Nov 11, 2011 at 12:38 PM, UTKARSH SRIVASTAV usrivastav...@gmail.com
 wrote:

 @NAMAN i don't know python but i think u r writing in a new file .
 what i want to say a c code that could write on a.out without the use of
 another file


 On Fri, Nov 11, 2011 at 12:32 PM, NAMAN KOHLI naman09...@iiitd.ac.inwrote:

 I think @Don meant this

 For a *New Text Document.py* in my folder I am running this script in
 python

 File = open(New Text Document.py,'r')
 data = File.readlines()
 print 1
 value = int(data[2].split( )[1])
 print value
 File.close()
 File = open(New Text Document.py,'w')
  value += 1
 data[2]=print +str(value)+'\n'
 for i in data:
 File.writelines(i)
 File.close()

 This prints a new value every time the program runs... The only catch is
 that you have to shut the program after running it once and then again
 restart it.. Please tell if there is any error

 On Fri, Nov 11, 2011 at 1:48 AM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 @don can u give a code for your logic

 On Wed, Oct 19, 2011 at 2:45 PM, Azhar Hussain azhar...@gmail.comwrote:

 I am not sure about the program to do it. But, 'strace' on linux would
 give the details of a program(parameters passed, retrun values, signals
 recieved etc). If you are looking for something similar then you can loot
 at strace source.

 mean while a.out does not not have information of stack pointer etc
 which will be built only at run time and it just tells what data goes to
 text, stack etc. it is dumb it does not do anything, the interpreter
 recollects all the info from a.out.


 -
 Azhar.


 On Wed, Oct 19, 2011 at 2:29 PM, Rahul raikra...@gmail.com wrote:

 http://www.dgp.toronto.edu/~ajr/209/notes/memoryos.html
 Rahul


 On Wed, Oct 19, 2011 at 2:29 PM, Rahul raikra...@gmail.com wrote:

 http://valgrind.org/

 I am not sure but this may help on linux.
 I have a question that how to determine the number of system calls a
 program has made ?
 That too of a particular type
  Rahul


 On Wed, Oct 19, 2011 at 11:07 AM, kumar raja 
 rajkumar.cs...@gmail.com wrote:

 Can someone give me an idea about how to see the range of segments
 like data ,heap,stack and text segments of an executable file.(a.out)

 Is there anyway to access the those segments from the program itself
 (while in execution), like using stack pointer for stack segment ??




 On 18 October 2011 19:54, sravanreddy001 
 sravanreddy...@gmail.comwrote:

 @Don, Gene:
 very good insights,
 didn't even thought of the changing the executable, but it indeed
 is one way to do.
 :)

 @Don: agree with scripts and interpreted code.. :)
 [coming out of the same language helps answers some questions
 easily]

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/ONw7a6q9VRMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 Regards
 Kumar Raja
 M.Tech(SIT)
 IIT Kharagpur,
 10it60...@iitkgp.ac.in


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 3rd Year
 @MNNIT ALLAHABAD*


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 Naman Kohli
 Roll No. 2009027

 --
 You received this message because you are 

Re: [algogeeks] BST question

2011-11-11 Thread UTKARSH SRIVASTAV
correct me if I am wrong

#includestdio.h

struct node
{
int data;
struct node *left;
struct node * right;
}*root;

int sum(int s,struct node *p,int ar[],int l)
{
if(p == NULL )
{
return 0;
}
if(p-left == NULL  p-right == NULL)
{
if( s - p-data == 0)
{
ar[l++] = p-data;
int i;
for( i = 0 ;i  l ;i++)
{
printf(%d ,ar[i]);
}
printf(\n);
}

}
ar[l++] = p-data;
sum(s - p-data, p-left , ar , l);
sum(s - p-data, p-right , ar, l);
return 0;
}

struct node * getnode(int k)
{
struct node *temp = malloc(sizeof(struct node));
temp-data = k;
temp-left= NULL;
temp-right = NULL;
return temp;
}

main()
{
int ar[50],value;
root = getnode(5);
root-left= getnode(2);
root-right = getnode(2);
root-left-left = getnode(7);
root-left-right = getnode(8);
root-right-left = getnode(3);
root-right-right = getnode(7);
value = 14;
sum(value,root,ar,0);
return 0;
}

On Fri, Nov 11, 2011 at 12:38 PM, aniket chatterjee aniket...@gmail.comwrote:

 Write a recursive function that will store each root to leaf path in an
 array. Now for each root to leaf path find the subarray which sums up to X.

 On Thu, Nov 10, 2011 at 11:53 PM, AMAN AGARWAL mnnit.a...@gmail.comwrote:

 Hi All,

 Please give me the solution of this problem.

 A binary tree and a number X is given. Find all the paths(not necessarily
 starting from root) such that the sum equals X.


 Regards,
 Aman.

 --
 AMAN AGARWAL
 Success is not final, Failure is not fatal: It is the courage to
 continue that counts!

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
*UTKARSH SRIVASTAV
CSE-3
B-Tech 3rd Year
@MNNIT ALLAHABAD*

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Weird Behaviour of Fork()

2011-11-11 Thread UTKARSH SRIVASTAV
well rahul is absolutely correct and here I will want to say that one
should also see behaviour of _exit and exit .. they will also
give different output

On Fri, Nov 11, 2011 at 1:09 PM, rahul vatsa vatsa.ra...@gmail.com wrote:

 wen u write something in printf, its 1st stored in the stdout buffer,  and
 then wen buffers gets filled up, it goes to std output, u can forcly flush
 it by using a fflush(stdout) api aftr ur printf st, or by putting a new
 ln..

 On Fri, Nov 11, 2011 at 12:49 PM, rachel asret...@gmail.com wrote:

 oh, so what you want to say is that when '\n' is not there in print
 command and then it is stored in stdout buffer unless it receives a '\n'
 and executes only in the end if '\n' is not received.

 thanks rahul :)


 On Fri, Nov 11, 2011 at 12:31 PM, rahul vatsa vatsa.ra...@gmail.comwrote:

 u have a new line in 2nd prog bt not in the st, that makes the
 difference.
 when u do fork in 1st code, the algo is nt printed on the terminal,
 rather it is in the stdout buffer oly, which gets duplicated in the forked
 pr also. nd wen the prog terminates, whtver is ther in the buffer gets
 printed on terminal.
 bt in the 2nd code, due to new line, it gets flushed off before fork, nd
 so the forkd pr doesn't print this.

   On Fri, Nov 11, 2011 at 12:22 PM, rachel asret...@gmail.com wrote:

   main()
 {
 printf(algo );
 fork();
 // do anything
 }
 in this program, it is printing algo two times.


 main()
  {
 printf(algo \n);
 fork();
 // do anything
 }
 in this program, it is printing algo only once.

 when a fork() is done, processes gets doubled and the execution starts
 from the part of the program below the fork() command, am i correct ?

 Thanks in advance.

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
*UTKARSH SRIVASTAV
CSE-3
B-Tech 3rd Year
@MNNIT ALLAHABAD*

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] BST question

2011-11-11 Thread aniket chatterjee
As far as I understand, your solution will always contain the path that
essentially start from root. But the actual problem states that the path
may not necessarily start from root.

On Fri, Nov 11, 2011 at 1:21 PM, UTKARSH SRIVASTAV
usrivastav...@gmail.comwrote:


 correct me if I am wrong

 #includestdio.h

 struct node
 {
 int data;
 struct node *left;
 struct node * right;
 }*root;

 int sum(int s,struct node *p,int ar[],int l)
 {
 if(p == NULL )
 {
 return 0;
 }
 if(p-left == NULL  p-right == NULL)
 {
 if( s - p-data == 0)
 {
 ar[l++] = p-data;
 int i;
 for( i = 0 ;i  l ;i++)
 {
 printf(%d ,ar[i]);
 }
 printf(\n);
 }

 }
 ar[l++] = p-data;
 sum(s - p-data, p-left , ar , l);
 sum(s - p-data, p-right , ar, l);
 return 0;
 }

 struct node * getnode(int k)
 {
 struct node *temp = malloc(sizeof(struct node));
 temp-data = k;
 temp-left= NULL;
 temp-right = NULL;
 return temp;
 }

 main()
 {
 int ar[50],value;
 root = getnode(5);
 root-left= getnode(2);
 root-right = getnode(2);
 root-left-left = getnode(7);
 root-left-right = getnode(8);
 root-right-left = getnode(3);
 root-right-right = getnode(7);
 value = 14;
 sum(value,root,ar,0);
 return 0;

 }

 On Fri, Nov 11, 2011 at 12:38 PM, aniket chatterjee 
 aniket...@gmail.comwrote:

 Write a recursive function that will store each root to leaf path in an
 array. Now for each root to leaf path find the subarray which sums up to X.

 On Thu, Nov 10, 2011 at 11:53 PM, AMAN AGARWAL mnnit.a...@gmail.comwrote:

 Hi All,

 Please give me the solution of this problem.

 A binary tree and a number X is given. Find all the paths(not
 necessarily starting from root) such that the sum equals X.


 Regards,
 Aman.

 --
 AMAN AGARWAL
 Success is not final, Failure is not fatal: It is the courage to
 continue that counts!

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 3rd Year
 @MNNIT ALLAHABAD*


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] BST question

2011-11-11 Thread aniket chatterjee
And also your solution prints the root to leaf path that sums up to X. But
the path may not contain root as well as leaf also. May be some
intermediate 4 nodes (from root to leaf path)sums up to X. Your code doesnt
provide the solution for that scenario.

On Fri, Nov 11, 2011 at 2:53 PM, aniket chatterjee aniket...@gmail.comwrote:

 As far as I understand, your solution will always contain the path that
 essentially start from root. But the actual problem states that the path
 may not necessarily start from root.

 On Fri, Nov 11, 2011 at 1:21 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:


 correct me if I am wrong

 #includestdio.h

 struct node
 {
 int data;
 struct node *left;
 struct node * right;
 }*root;

 int sum(int s,struct node *p,int ar[],int l)
 {
 if(p == NULL )
 {
 return 0;
 }
 if(p-left == NULL  p-right == NULL)
 {
 if( s - p-data == 0)
 {
 ar[l++] = p-data;
 int i;
 for( i = 0 ;i  l ;i++)
 {
 printf(%d ,ar[i]);
 }
 printf(\n);
 }

 }
 ar[l++] = p-data;
 sum(s - p-data, p-left , ar , l);
 sum(s - p-data, p-right , ar, l);
 return 0;
 }

 struct node * getnode(int k)
 {
 struct node *temp = malloc(sizeof(struct node));
 temp-data = k;
 temp-left= NULL;
 temp-right = NULL;
 return temp;
 }

 main()
 {
 int ar[50],value;
 root = getnode(5);
 root-left= getnode(2);
 root-right = getnode(2);
 root-left-left = getnode(7);
 root-left-right = getnode(8);
 root-right-left = getnode(3);
 root-right-right = getnode(7);
 value = 14;
 sum(value,root,ar,0);
 return 0;

 }

 On Fri, Nov 11, 2011 at 12:38 PM, aniket chatterjee 
 aniket...@gmail.comwrote:

 Write a recursive function that will store each root to leaf path in an
 array. Now for each root to leaf path find the subarray which sums up to X.

 On Thu, Nov 10, 2011 at 11:53 PM, AMAN AGARWAL mnnit.a...@gmail.comwrote:

 Hi All,

 Please give me the solution of this problem.

 A binary tree and a number X is given. Find all the paths(not
 necessarily starting from root) such that the sum equals X.


 Regards,
 Aman.

 --
 AMAN AGARWAL
 Success is not final, Failure is not fatal: It is the courage to
 continue that counts!

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 3rd Year
 @MNNIT ALLAHABAD*


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] free() function

2011-11-11 Thread shady
typedef struct n{
int num;
struct n *next;
}node;

node is the structure to create the linked list.

node *list1;

I have created a linked list ( list1 )like this 1 - 2 - 3 - 4

so i free it like this 

free(list1 - next - next -next);
free(list1 - next - next);
free(list1 - next);
free(list1);

when i am printing the list after each free, it is always printing a
list of length 4, isn't the values free'd when we do free() ?

actual printing gives
1 2 3 0
1 2 garbage 0
1 garbage garbage 0
garbage garbage garbage 0

why is the linked list still connected ?


actual print function -
void print(node *l)
{
 while(l != NULL)
{
printf(%d\t,l-num);
l = l-next;
}
printf(\n);
}

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Code it...

2011-11-11 Thread saurabh singh
for an executable its not possible.
open will return with ETXTBSY error.

OS:ubuntu.

On Fri, Nov 11, 2011 at 1:50 PM, NAMAN KOHLI naman09...@iiitd.ac.in wrote:

 @Utkarsh

 In this code I am writing on the source code file at time of execution
  Every time the file executes it changes its own source code...However
 I too would like to know if we can do that in c ...


 On Fri, Nov 11, 2011 at 12:38 PM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 @NAMAN i don't know python but i think u r writing in a new file
 . what i want to say a c code that could write on a.out without the
 use of another file


 On Fri, Nov 11, 2011 at 12:32 PM, NAMAN KOHLI naman09...@iiitd.ac.inwrote:

 I think @Don meant this

 For a *New Text Document.py* in my folder I am running this script in
 python

 File = open(New Text Document.py,'r')
 data = File.readlines()
 print 1
 value = int(data[2].split( )[1])
 print value
 File.close()
 File = open(New Text Document.py,'w')
  value += 1
 data[2]=print +str(value)+'\n'
 for i in data:
 File.writelines(i)
 File.close()

 This prints a new value every time the program runs... The only catch is
 that you have to shut the program after running it once and then again
 restart it.. Please tell if there is any error

 On Fri, Nov 11, 2011 at 1:48 AM, UTKARSH SRIVASTAV 
 usrivastav...@gmail.com wrote:

 @don can u give a code for your logic

 On Wed, Oct 19, 2011 at 2:45 PM, Azhar Hussain azhar...@gmail.comwrote:

 I am not sure about the program to do it. But, 'strace' on linux would
 give the details of a program(parameters passed, retrun values, signals
 recieved etc). If you are looking for something similar then you can loot
 at strace source.

 mean while a.out does not not have information of stack pointer etc
 which will be built only at run time and it just tells what data goes to
 text, stack etc. it is dumb it does not do anything, the interpreter
 recollects all the info from a.out.


 -
 Azhar.


 On Wed, Oct 19, 2011 at 2:29 PM, Rahul raikra...@gmail.com wrote:

 http://www.dgp.toronto.edu/~ajr/209/notes/memoryos.html
 Rahul


 On Wed, Oct 19, 2011 at 2:29 PM, Rahul raikra...@gmail.com wrote:

 http://valgrind.org/

 I am not sure but this may help on linux.
 I have a question that how to determine the number of system calls a
 program has made ?
 That too of a particular type
  Rahul


 On Wed, Oct 19, 2011 at 11:07 AM, kumar raja 
 rajkumar.cs...@gmail.com wrote:

 Can someone give me an idea about how to see the range of segments
 like data ,heap,stack and text segments of an executable file.(a.out)

 Is there anyway to access the those segments from the program
 itself (while in execution), like using stack pointer for stack 
 segment ??




 On 18 October 2011 19:54, sravanreddy001 
 sravanreddy...@gmail.comwrote:

 @Don, Gene:
 very good insights,
 didn't even thought of the changing the executable, but it indeed
 is one way to do.
 :)

 @Don: agree with scripts and interpreted code.. :)
 [coming out of the same language helps answers some questions
 easily]

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/ONw7a6q9VRMJ.

 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 Regards
 Kumar Raja
 M.Tech(SIT)
 IIT Kharagpur,
 10it60...@iitkgp.ac.in


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 *UTKARSH SRIVASTAV
 CSE-3
 B-Tech 3rd Year
 @MNNIT ALLAHABAD*


  --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more 

[algogeeks] Re: free() function

2011-11-11 Thread vikas
nopes , they are not connected, it is just a chance you are getting
the same values and nothing is overwritten there: basically these are
DANGLING POINTERS . Now you should keep practising something like this

#define FREE(N) { free(N); N=NULL;}

to avoid such mistakes

On Nov 11, 3:41 pm, shady sinv...@gmail.com wrote:
 typedef struct n{
         int num;
         struct n *next;

 }node;

 node is the structure to create the linked list.

 node *list1;

 I have created a linked list ( list1 )like this 1 - 2 - 3 - 4

 so i free it like this 

 free(list1 - next - next -next);
 free(list1 - next - next);
 free(list1 - next);
 free(list1);

 when i am printing the list after each free, it is always printing a
 list of length 4, isn't the values free'd when we do free() ?

 actual printing gives
 1 2 3 0
 1 2 garbage 0
 1 garbage garbage 0
 garbage garbage garbage 0

 why is the linked list still connected ?

 actual print function -
 void print(node *l)
 {
      while(l != NULL)
     {
         printf(%d\t,l-num);
         l = l-next;
     }
     printf(\n);

 }

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: free() function

2011-11-11 Thread shady
ok, thanks.

why do we need to free the memory ?
Suppose i have a linked list of 1000 nodes and i make the head of it =
NULL, thus losing the whole list. Then compiler can look at other variables
and if this list has not been referenced anywhere else then it is useless,
thus will free the memory.
Is the argument wrong ?



On Fri, Nov 11, 2011 at 8:20 PM, vikas vikas.rastogi2...@gmail.com wrote:

 nopes , they are not connected, it is just a chance you are getting
 the same values and nothing is overwritten there: basically these are
 DANGLING POINTERS . Now you should keep practising something like this

 #define FREE(N) { free(N); N=NULL;}

 to avoid such mistakes

 On Nov 11, 3:41 pm, shady sinv...@gmail.com wrote:
  typedef struct n{
  int num;
  struct n *next;
 
  }node;
 
  node is the structure to create the linked list.
 
  node *list1;
 
  I have created a linked list ( list1 )like this 1 - 2 - 3 - 4
 
  so i free it like this 
 
  free(list1 - next - next -next);
  free(list1 - next - next);
  free(list1 - next);
  free(list1);
 
  when i am printing the list after each free, it is always printing a
  list of length 4, isn't the values free'd when we do free() ?
 
  actual printing gives
  1 2 3 0
  1 2 garbage 0
  1 garbage garbage 0
  garbage garbage garbage 0
 
  why is the linked list still connected ?
 
  actual print function -
  void print(node *l)
  {
   while(l != NULL)
  {
  printf(%d\t,l-num);
  l = l-next;
  }
  printf(\n);
 
  }

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: free() function

2011-11-11 Thread saurabh singh
well that would be tough for the compiler to predict things that will
happen during run time.Its the job of garbage collector to do that.

On Fri, Nov 11, 2011 at 8:36 PM, shady sinv...@gmail.com wrote:

 ok, thanks.

 why do we need to free the memory ?
 Suppose i have a linked list of 1000 nodes and i make the head of it =
 NULL, thus losing the whole list. Then compiler can look at other variables
 and if this list has not been referenced anywhere else then it is useless,
 thus will free the memory.
 Is the argument wrong ?



 On Fri, Nov 11, 2011 at 8:20 PM, vikas vikas.rastogi2...@gmail.comwrote:

 nopes , they are not connected, it is just a chance you are getting
 the same values and nothing is overwritten there: basically these are
 DANGLING POINTERS . Now you should keep practising something like this

 #define FREE(N) { free(N); N=NULL;}

 to avoid such mistakes

 On Nov 11, 3:41 pm, shady sinv...@gmail.com wrote:
  typedef struct n{
  int num;
  struct n *next;
 
  }node;
 
  node is the structure to create the linked list.
 
  node *list1;
 
  I have created a linked list ( list1 )like this 1 - 2 - 3 - 4
 
  so i free it like this 
 
  free(list1 - next - next -next);
  free(list1 - next - next);
  free(list1 - next);
  free(list1);
 
  when i am printing the list after each free, it is always printing a
  list of length 4, isn't the values free'd when we do free() ?
 
  actual printing gives
  1 2 3 0
  1 2 garbage 0
  1 garbage garbage 0
  garbage garbage garbage 0
 
  why is the linked list still connected ?
 
  actual print function -
  void print(node *l)
  {
   while(l != NULL)
  {
  printf(%d\t,l-num);
  l = l-next;
  }
  printf(\n);
 
  }

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Saurabh Singh
B.Tech (Computer Science)
MNNIT ALLAHABAD

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: kth smallest element

2011-11-11 Thread Ankuj Gupta
I tried to understand the logic of it but could not :(

On Nov 11, 11:17 am, shady sinv...@gmail.com wrote:
 no, for eg.

 array1 = { 1, 2, 5, 6, 7, 7, 7, 23};
 array2 = { 1, 2, 2, 4, 8, 9, 12 };

 then for
 k = 2, answer = 1

 k = 3, answer = 2

 k = 4, answer = 2,

 k = 6, answer = 4.

 anyway to do it iteratively in logarithmic time

 On Fri, Nov 11, 2011 at 2:27 AM, sravanreddy001 
 sravanreddy...@gmail.comwrote:







  Is it (k)th smallest element (distict integers)
  or the element at position k, when both are merged?

  455566777
  -- Is 3rd smallest element '1' or '4'

  If four, I am not able to think of a log complexity. Can u post your
  recursive solution only if u meant '4' in above case.

  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To view this discussion on the web visit
 https://groups.google.com/d/msg/algogeeks/-/Aq8q9OwfcaEJ.

  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: kth smallest element

2011-11-11 Thread rachel
what you didn't understand, logic or question ?

On Fri, Nov 11, 2011 at 10:05 PM, Ankuj Gupta ankuj2...@gmail.com wrote:

 I tried to understand the logic of it but could not :(

 On Nov 11, 11:17 am, shady sinv...@gmail.com wrote:
  no, for eg.
 
  array1 = { 1, 2, 5, 6, 7, 7, 7, 23};
  array2 = { 1, 2, 2, 4, 8, 9, 12 };
 
  then for
  k = 2, answer = 1
 
  k = 3, answer = 2
 
  k = 4, answer = 2,
 
  k = 6, answer = 4.
 
  anyway to do it iteratively in logarithmic time
 
  On Fri, Nov 11, 2011 at 2:27 AM, sravanreddy001 
 sravanreddy...@gmail.comwrote:
 
 
 
 
 
 
 
   Is it (k)th smallest element (distict integers)
   or the element at position k, when both are merged?
 
  
 455566777
   -- Is 3rd smallest element '1' or '4'
 
   If four, I am not able to think of a log complexity. Can u post your
   recursive solution only if u meant '4' in above case.
 
   --
   You received this message because you are subscribed to the Google
 Groups
   Algorithm Geeks group.
   To view this discussion on the web visit
  https://groups.google.com/d/msg/algogeeks/-/Aq8q9OwfcaEJ.
 
   To post to this group, send email to algogeeks@googlegroups.com.
   To unsubscribe from this group, send email to
   algogeeks+unsubscr...@googlegroups.com.
   For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: kth smallest element

2011-11-11 Thread Freddy
FIND_PTH_SMALLEST(A, Astart, Aend, B, Bstart, Bend, p)  // Check for
the special case when A and/or B have size one. if Astart ==
Aend AND Bstart == Bend return p == 1 ? min(A[1], B[1]) : max(A[1],
B[1])if Astart == Aend  return max(min(A[1], B[Bend]),
B[Bend – 1])if Bstart == Bend   return max(min(A[Aend], 
B[1]),
A[Aend – 1])
// Get the correct indices to work with.i = floor((p+1)/
2)j = ceil((p+1)/2)if i  Aend j += i –
Aend i = Aendif j  Bend i += j –
Bend j = Bend
// Check the Sandwich Conditions.if A[i – 1] = B[j] AND B[j]
= A[i] return B[j]if B[j – 1] = A[i] AND A[i] = B[j] return
A[i]
// Recursively call the function and throw away parts of the array
you don’t want. if A[i]  B[j]  return FIND_PTH_SMALLEST(A, Astart, i,
B, j, Bend, p – j – 1)  elsereturn FIND_PTH_SMALLEST(A, i, Aend, B,
Bstart, j, p – i – 1)

On Nov 10, 11:07 am, shady sinv...@gmail.com wrote:
 Given two sorted arrays, how to find the kth smallest element in the
 union of the arrays in a logarithmic time algorithm ?
 i have already formed a recursive solution, can anyone give the
 iterative approach, only pseudo-code :)

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: kth smallest element

2011-11-11 Thread Brijesh Upadhyay
I guess this approach will work..

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/algogeeks/-/zeydVF1OqioJ.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.