Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Why this dies
Index -> Programming, C++ -> C++ Help
Goto page Previous  1, 2, 3, 4  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tubs




PostPosted: Tue Jan 24, 2006 10:56 pm   Post subject: (No subject)

I have no idea what code that is.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Jan 24, 2006 11:12 pm   Post subject: (No subject)

It's Common Lisp. It's my way of just posting the answer. Wink
Tubs




PostPosted: Tue Jan 24, 2006 11:22 pm   Post subject: (No subject)

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.

code:
#include <stdio.h>

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




PostPosted: Tue Jan 24, 2006 11:29 pm   Post subject: (No subject)

code:
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.

code:
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




PostPosted: Tue Jan 24, 2006 11:37 pm   Post subject: (No subject)

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 Very Happy
Martin




PostPosted: Wed Jan 25, 2006 2:57 am   Post subject: (No subject)

As I have recently discovered

code:
if (1 == n)
is far superior to
code:
if (n == 1)


Which of the following will crash?

code:
if (n == 1)

code:
if (n = 1)

code:
if (1 == n)

code:
if (1 = n)
wtd




PostPosted: Wed Jan 25, 2006 3:07 am   Post subject: (No subject)

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




PostPosted: Wed Jan 25, 2006 9:40 pm   Post subject: (No subject)

Well the easy solution is to start thinking in Japanese. Wink
Sponsor
Sponsor
Sponsor
sponsor
Hikaru79




PostPosted: Wed Jan 25, 2006 10:47 pm   Post subject: (No subject)

Martin wrote:
Well the easy solution is to start thinking in Japanese. Wink


That'd be closer to "n 1 ==" Wink
Tubs




PostPosted: Thu Jan 26, 2006 9:33 pm   Post subject: (No subject)

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)

code:
#include "coaster_node_t.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

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




PostPosted: Thu Jan 26, 2006 9:54 pm   Post subject: (No subject)

And without seeing the definition of these data structures, I'm supposed to tell you what about this code?
Tubs




PostPosted: Thu Jan 26, 2006 9:58 pm   Post subject: (No subject)

Just wondering if there was any obvious misuse of pointer syntax. Here is the whole file:


Q3.zip
 Description:

Download
 Filename:  Q3.zip
 Filesize:  6.7 KB
 Downloaded:  116 Time(s)

Tubs




PostPosted: Thu Jan 26, 2006 11:21 pm   Post subject: (No subject)

Misuse of pointer syntax such as not putting brackets around *list Confused
Fixed linker problem with having extraneous functions, now it dumps core after running, is'nt that great.
wtd




PostPosted: Thu Jan 26, 2006 11:35 pm   Post subject: (No subject)

I've gotten spoiled. That level of pointer indirection is making my head hurt.
wtd




PostPosted: Thu Jan 26, 2006 11:37 pm   Post subject: (No subject)

You have:

code:
*list->next;


In the function you pasted here.

This is in void context and does nothing. Why do you have it there?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 4  [ 52 Posts ]
Goto page Previous  1, 2, 3, 4  Next
Jump to:   


Style:  
Search: