| Author |
Message |
Tubs

|
Posted: Tue Jan 24, 2006 10:56 pm Post subject: (No subject) |
|
|
| I have no idea what code that is.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Tue Jan 24, 2006 11:12 pm Post subject: (No subject) |
|
|
It's Common Lisp. It's my way of just posting the answer.
|
|
|
|
|
 |
Tubs

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

|
Posted: 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
|
|
|
|
|
 |
Martin

|
Posted: Wed Jan 25, 2006 2:57 am Post subject: (No subject) |
|
|
As I have recently discovered
is far superior to
Which of the following will crash?
|
|
|
|
|
 |
wtd
|
Posted: 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

|
Posted: Wed Jan 25, 2006 9:40 pm Post subject: (No subject) |
|
|
Well the easy solution is to start thinking in Japanese.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Hikaru79
|
Posted: Wed Jan 25, 2006 10:47 pm Post subject: (No subject) |
|
|
Martin wrote: Well the easy solution is to start thinking in Japanese. 
That'd be closer to "n 1 =="
|
|
|
|
|
 |
Tubs

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

|
Posted: 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:
| Description: |
|
 Download |
| Filename: |
Q3.zip |
| Filesize: |
6.7 KB |
| Downloaded: |
116 Time(s) |
|
|
|
|
|
 |
Tubs

|
Posted: Thu Jan 26, 2006 11:21 pm Post subject: (No subject) |
|
|
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
|
Posted: 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
|
Posted: Thu Jan 26, 2006 11:37 pm Post subject: (No subject) |
|
|
You have:
In the function you pasted here.
This is in void context and does nothing. Why do you have it there?
|
|
|
|
|
 |
|