
-----------------------------------
Tubs
Thu Jan 19, 2006 9:16 pm

Why this dies
-----------------------------------
#include 
#define PATH "/usr/tmp"

char *full_name(const char name[]);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system ("PAUSE");
  return (0);
}

char *full_name(const char name[])
{
  static char file_name[100];
  strcpy(file_name, PATH);
  strcat(file_name, '/');
  strcat(file_name, name);
  return(file_name);
}

Why does this program die a horribly gruesome death?

-----------------------------------
wtd
Thu Jan 19, 2006 9:29 pm


-----------------------------------
#include 
#define PATH "/usr/tmp"

char *full_name(const char* name);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system("PAUSE");
  return 0;
}

char *full_name(const char* name)
{
  char* file_name = malloc(sizeof(char) * 100);
  strcpy(file_name, PATH);
  strcat(file_name, "/");
  strcat(file_name, name);
  return file_name;
}


What did I do differently?  :)

-----------------------------------
Tubs
Thu Jan 19, 2006 9:46 pm


-----------------------------------
Changed the parameter to a pointer and dynamically allocated memory for the variable?

-----------------------------------
Tubs
Thu Jan 19, 2006 9:47 pm


-----------------------------------
that code is giving me errors!!! line 19 makes a pointer from integer without a cast

-----------------------------------
wtd
Thu Jan 19, 2006 9:55 pm


-----------------------------------
Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC.  Show me the exact code you were using.

-----------------------------------
Tubs
Thu Jan 19, 2006 9:58 pm


-----------------------------------
Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC.  Show me the exact code you were using.

The one you posted.

-----------------------------------
wtd
Thu Jan 19, 2006 10:04 pm


-----------------------------------
Then I cannot help.  The code I posted compiles flawlessly with GCC 3.4.2 (MinGW).

-----------------------------------
Tubs
Fri Jan 20, 2006 1:01 am


-----------------------------------
Ok, I got the code working. IDEs are foolish.

Another question: how would I go about making a function that finds a certain item in a linked list, given the number it is in order and a pointer to the beginning of the list? This is what I have so far:

#include 
#include "coaster_info_t.h"

coaster_info_t find_nth_coaster (int *N, int index)
{

  coaster_node_t *current = *N;
  int i = 0;

  if (current != NULL && i != index)
    current = current->next;

  else if (current != NULL && i == index)
    return( current );

  else
    return ( NULL );
}

-----------------------------------
wtd
Fri Jan 20, 2006 1:13 am


-----------------------------------
Use a loop to follow the links?

-----------------------------------
Tubs
Tue Jan 24, 2006 7:29 pm


-----------------------------------
Yeah, thanks. New question if I may:

A sequence is defined by the following recursive relationship:

A(n) = A(n/2) + A(n/2) ; A(1) = 3

It is defined only for powers of 2 and you need only consider powers of two.

I don't think I know what they are asking here, this is my code so far:

#include 

int A (int n);

int main(int argc, char *argv[])
{
  int n, x;

  printf ("n = ");
  scanf ("%d", &n);
  printf ("\n");

  x = A (n);
  printf ("A (n) = %.2f\n", x);
  system ("pause");

  return 0;
}

double A (double n)
{
  if (n == 1)
    return n;
  else
    n = A (n / 2) + A (n / 2);
}

Help greatly appreciated :)

-----------------------------------
wtd
Tue Jan 24, 2006 7:34 pm


-----------------------------------
Look at that code really closely.  

Now... remove all of the code inside the functions, leaving just the headers?  See the problem?

-----------------------------------
Tubs
Tue Jan 24, 2006 8:03 pm


-----------------------------------
Look at that code really closely.  

Now... remove all of the code inside the functions, leaving just the headers?  See the problem?

Yeah, I was fooling around with the types because the question seemed to imply that that ints were ok, but there is division by 2 thrown in there. When I change it to double type it outputs 0, when it is an integer, 1. I'm still lost.  :cry:

-----------------------------------
wtd
Tue Jan 24, 2006 9:58 pm


-----------------------------------
What they're saying is... when n is 1, A returns 3.  For any other value of n, well... the question explains that.  So, let's look at a breakdown of A(8).

A(8)
A(4) + A(4)
(A(2) + A(2)) + (A(2) + A(2))
((A(1) + A(1)) + (A(1) + A(1))) + ((A(1) + A(1)) + (A(1) + A(1)))
A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1)
3 + 3 + 3 + 3 + 3 + 3 + 3 + 3
24

-----------------------------------
Tubs
Tue Jan 24, 2006 10:25 pm


-----------------------------------
Thanks a lot wtd. You rock.

-----------------------------------
wtd
Tue Jan 24, 2006 10:52 pm


-----------------------------------
Glad I could help.

(defun a (&optional (n 1))
  (cond ((= n 1) 3)
        ((> n 1) 
         (let ((v (a (/ n 2))))
           (+ v v)))
        (t 
         (let ((v (a (* n 2))))
           (+ v v)))))

;)

-----------------------------------
Tubs
Tue Jan 24, 2006 10:56 pm


-----------------------------------
I have no idea what code that is.

-----------------------------------
wtd
Tue Jan 24, 2006 11:12 pm


-----------------------------------
It's Common Lisp.  It's my way of just posting the answer.  ;)

-----------------------------------
Tubs
Tue Jan 24, 2006 11:22 pm


-----------------------------------
Weird.

This function is still eluding me. (Recursion is not my fort'e) Any tips at all? Currently if a power of 2 is inserted it returns n.

#include 

int A (int n);

int main(int argc, char *argv[])
{
  int n, x;

  printf ("n = ");
  scanf ("%d", &n);
  printf ("\n");

  x = A (n);
  printf ("A (n) = %d\n", x);
  system ("pause");

  return 0;
}

int A (int n)
{
  int ans;
  if (n == 1)
    ans = 1;
  else
    ans = A(n / 2) + A(n / 2);

  return ans;
}

-----------------------------------
wtd
Tue Jan 24, 2006 11:29 pm


-----------------------------------
int A (int n)
{
  int ans;
  if (n == 1)
    ans = 1;
  else
    ans = A(n / 2) + A(n / 2);

  return ans;
}

Your use of a variable here is not a good habit to get into.

int A (int n)
{
  if (n == 1)
    return 1;
  else
    return A(n / 2) + A(n / 2);
}

You're returning 1 here if n equals 1.  Is that what the problem says should happen?

-----------------------------------
Tubs
Tue Jan 24, 2006 11:37 pm


-----------------------------------
Yeah, I thought the A(1) = 3 was just an example to show that you were on the right track. Also, I followed the format provided in my text book on how to do a recursive fibonacci sequency and it used an 'ans' variable.

Thanks again :D

-----------------------------------
Martin
Wed Jan 25, 2006 2:57 am


-----------------------------------
As I have recently discovered

if (1 == n) is far superior to if (n == 1)

Which of the following will crash?

if (n == 1)
if (n = 1)
if (1 == n)
if (1 = n)

-----------------------------------
wtd
Wed Jan 25, 2006 3:07 am


-----------------------------------
Unfortunately this is sacrificing expressivity of the code to compensate for a [potential ambiguity in C++'s syntax.

It is vastly more natural to think "n == 1".  Here "n" is emphasized because it is the subject.  We're not interested in whether 1 equals "n".  We're interested in whether "n" equals 1.  Technically speaking, they're entirely equivalent.  However, there is a linguistic factor in writing good code.

-----------------------------------
Martin
Wed Jan 25, 2006 9:40 pm


-----------------------------------
Well the easy solution is to start thinking in Japanese. ;)

-----------------------------------
Hikaru79
Wed Jan 25, 2006 10:47 pm


-----------------------------------
Well the easy solution is to start thinking in Japanese. ;)

That'd be closer to "n 1 ==" ;)

-----------------------------------
Tubs
Thu Jan 26, 2006 9:33 pm


-----------------------------------
This code is telling me that data.name and next are not part of a structure or union, which is almost definately because I have some incorrect pointer usage somewhere, but I get all confuzzeled when it comes to double pointers. Any tips? (I would post the program in its entirety but it is a heirchy of many functions)

#include "coaster_node_t.h"
#include 
#include 
#include 

coaster_node_t *remove_duplicate_coasters( coaster_node_t **list, char namedelete[] )
{

  if (*list != NULL)
  {
    while (*list != NULL)
    {
      if (strcmp(*list->data.name, namedelete) != 0)
      {
        coaster_list_delete (&*list, namedelete);
      }
      
      *list->next;
    }
    return (*list);
  }
  else
  {
    printf ("List is empty.\n");
    return (*list);
  }
}

-----------------------------------
wtd
Thu Jan 26, 2006 9:54 pm


-----------------------------------
And without seeing the definition of these data structures, I'm supposed to tell you what about this code?

-----------------------------------
Tubs
Thu Jan 26, 2006 9:58 pm


-----------------------------------
Just wondering if there was any obvious misuse of pointer syntax. Here is the whole file:

-----------------------------------
Tubs
Thu Jan 26, 2006 11:21 pm


-----------------------------------
Misuse of pointer syntax such as not putting brackets around *list  :? 
Fixed linker problem with having extraneous functions, now it dumps core after running, is'nt that great.

-----------------------------------
wtd
Thu Jan 26, 2006 11:35 pm


-----------------------------------
I've gotten spoiled.  That level of pointer indirection is making my head hurt.

-----------------------------------
wtd
Thu Jan 26, 2006 11:37 pm


-----------------------------------
You have:

*list->next;

In the function you pasted here.  

This is in void context and does nothing.  Why do you have it there?

-----------------------------------
Tubs
Thu Jan 26, 2006 11:52 pm


-----------------------------------
I thought it would select the next coaster for the delete function to work with. I've been doing this assignment for about 6 hours now so my head hurts too :(

functions still to go:
- duplicate one node to another node
- duplicate a list to another list
- reverse a list

-----------------------------------
Tubs
Fri Jan 27, 2006 12:00 am


-----------------------------------
This may be my tiredness / burntoutedness setting in, but I really really appreciate the help wtd. You're the man.

-----------------------------------
wtd
Fri Jan 27, 2006 3:12 pm


-----------------------------------
Glad I can help even if only a little bit.  

My suggestion... write one function at a time.  Make sure that function works.  Then move on.

-----------------------------------
Tubs
Thu Feb 02, 2006 12:53 pm


-----------------------------------
When using linked lists, how would you make a loop so that the current node is changed by a power of 2 in every iteration? (Creating a 'galloping search')

ex. currentnode->next                // changed by one
      currentnode->next->next      // changed by two

-----------------------------------
wtd
Thu Feb 02, 2006 2:10 pm


-----------------------------------
Generalize psuedocode for dealing with linked list iteration.

set "current" to first node in list

while current node's not null do
   access current node's stored value,
   and ...

   set "current" to the current node's pointer
   to the next node
end while loop

-----------------------------------
Tubs
Thu Feb 02, 2006 3:11 pm


-----------------------------------
This is a sorted list so I dont want a linear search, i want to increase the current node by a power of 2 in each iteration til the value of that node is greater than the target, in which case i run a linear search from the previous power of 2 to the current power of 2 until the node  with the target is found.

-----------------------------------
Andy
Thu Feb 02, 2006 3:32 pm


-----------------------------------
have a for loop that increaes exponentially each time, then inside, have another for loop that goes from 1 to the first counter, and inside the second for loop, do your getNext stuff

-----------------------------------
Tubs
Thu Feb 02, 2006 9:47 pm


-----------------------------------
I'm getting there, but the list that I am supposed to use seems not to finish declaring itself. can anyone tell me whats wrong?

#include 
#ifndef INT_TYPES
#define INT_TYPES

#include 
#include 

typedef struct
{
  int value;
}
int_data_t;

typedef struct int_node
{
  int_data_t data;
  struct int_node *next;
}
int_node_t;

// Prototypes.
void create_int_list( int_node_t **, int );
int_node_t *insert_int_list( int_node_t **, int_data_t * );
void print_int_list( int_node_t * );

#endif

int main(int argc, char *argv[])
{

  int_node_t *list;
  int_data_t something;
  something.value = 10;

  *insert_int_list ( &list, &something);
  create_int_list ( &list, something.value );
  print_int_list ( list );
  system("pause");
  return 0;
}

void create_int_list( int_node_t **list, int n )
{
  int i;
  int_data_t temp;

  for( i = n ; i >= 0 ; i-- )
  {
    // Insert the numbers in reverse order into the front of the list.
    temp.value = i;
    insert_int_list( list, &temp );
  }
}

int_node_t *insert_int_list( int_node_t **list, int_data_t *data )
{
  int_node_t *temp = NULL;

  temp = (int_node_t *)malloc( sizeof( int_node_t ) );

  if( temp != NULL )
  {
    temp->data = *data;
    temp->next = *list;
    *list = temp;
  }
  return temp;
}

void print_int_list( int_node_t *list )
{
  int_node_t *current = list;

  while( current != NULL )
  {
    printf( "%4d", current->data.value );
    current = current->next;
  }
  printf( "\n" );
}

-----------------------------------
wtd
Thu Feb 02, 2006 10:24 pm


-----------------------------------
typedef struct int_node 
 { 
   int_data_t data; 
   struct int_node *next; 
 } 
 int_node_t; 

This is one of the more inane things I've ever seen.

int main(int argc, char *argv[]) 
 { 
 
   int_node_t *list; 
   int_data_t something; 
   something.value = 10; 
 
   *insert_int_list ( &list, &something); 
   create_int_list ( &list, something.value ); 
   print_int_list ( list ); 
   system("pause"); 
   return 0; 
 } 

Why are you dereferencing the return value of insert_int_list in a void context?

-----------------------------------
wtd
Thu Feb 02, 2006 10:25 pm


-----------------------------------
typedef struct int_node 
 { 
   int_data_t data; 
   struct int_node *next; 
 } 
 int_node_t; 

This is one of the more inane things I've ever seen.

Errrr... I meant:

typedef struct 
 { 
   int value; 
 } 
 int_data_t; 

-----------------------------------
Tubs
Thu Feb 02, 2006 10:26 pm


-----------------------------------
Yeah i was wondering about that as well. i guess its just to get us used to declaring the structure and stuff.

-----------------------------------
Tubs
Thu Feb 02, 2006 10:29 pm


-----------------------------------
As for the dereferencing, I get the same error when i change it.

-----------------------------------
wtd
Thu Feb 02, 2006 10:30 pm


-----------------------------------
An additional note is that you should not, by any means, be casting the return value of malloc.

-----------------------------------
wtd
Thu Feb 02, 2006 10:32 pm


-----------------------------------
As for the dereferencing, I get the same error when i change it.

Yes, it's likely not the source of the error, but it is worth pointing out.

-----------------------------------
Tubs
Thu Feb 02, 2006 10:38 pm


-----------------------------------
i only wrote the main of this stuff, the rest was supplied by my prof for this assignment. its kinda hard to do this when the supplied material doesnt work.

-----------------------------------
wtd
Thu Feb 02, 2006 10:48 pm


-----------------------------------
Indeed.

-----------------------------------
Tubs
Thu Feb 02, 2006 10:55 pm


-----------------------------------
So no idea why its not declaring the end of the list properly?

-----------------------------------
wtd
Thu Feb 02, 2006 11:00 pm


-----------------------------------
I have no idea why anything in that library is written the way it is.

-----------------------------------
Tubs
Thu Feb 02, 2006 11:01 pm


-----------------------------------
Yeah. Fuck the library i'm making my own damn list. Excuse my french.

-----------------------------------
Tubs
Sun Feb 12, 2006 8:48 pm


-----------------------------------
I cannot figure out why this program dies (no error messages on my IDE either :() Any help is greatly appreciated as always :)

The program is supposed to take an integer, use the division by 2 method to determine the binary equivalent  using stacks.

-----------------------------------
Tubs
Sun Feb 12, 2006 10:26 pm


-----------------------------------
Scratch that last post. Re-wrote program. Linked lists suck.

-----------------------------------
Tubs
Thu Mar 30, 2006 9:55 pm


-----------------------------------

void median_qsort( int a[], int p, int q, int piv )  /*to sort the subarray */
{                          /* a[p:q] of array A into ascending order */
  int i, j;

  if( p < q ) 
  {
	 /* Initially i and j point to the first and last items */
     i = p;
     j = q;
	 partition( a, &i, &j, piv ); /* partitions a[p:q] into a[p:j] and a[i:q] */
	 median_qsort( a, p, j, piv );
	 median_qsort( a, i, q, piv );
  }
}


where piv is a value between 1 and 3 determining the location of the pivot (1 being the beginning, 2 being the middle and 3 being the end of the array)


void partition( int a[], int *i, int *j, int piv )
{
  int pivot, temp;

  pivot = median (a[*i], a[*j - 1], a[SIZE / 2]);

  if ( piv == 1 )
  {
    if ( pivot == a[*j-1] )
    {
      temp = a[0];
      a[0] = pivot;
      a[*j-1] = temp;
    }
    else if ( pivot == a[SIZE/2] )
    {
      temp = a[0];
      a[0] = pivot;
      a[SIZE/2] = temp;
    }
  }
  else if ( piv == 2 )
  {
    if ( pivot == a[*j-1] )
    {
      temp = a[SIZE/2];
      a[SIZE/2] = pivot;
      a[*j-1] = temp;
    }
    else if ( pivot == a[0] )
    {
      temp = a[SIZE/2];
      a[SIZE/2] = pivot;
      a[0] = temp;
    }
  }
  else if ( piv == 3 )
  {
    if ( pivot == a[0] )
    {
      temp = a[*j-1];
      a[*j-1] = pivot;
      a[0] = temp;
    }
    else if ( pivot == a[SIZE/2] )
    {
      temp = a[*j-1];
      a[*j-1] = pivot;
      a[SIZE/2] = temp;
    }
  }

  print_list ( a, SIZE );

  do {
/* Find leftmost i such that a[i] >= Pivot.*/
	while( &a[*i] 