Posted: Thu Jan 19, 2006 9:46 pm Post subject: (No subject)
Changed the parameter to a pointer and dynamically allocated memory for the variable?
Tubs
Posted: Thu Jan 19, 2006 9:47 pm Post subject: (No subject)
that code is giving me errors!!! line 19 makes a pointer from integer without a cast
wtd
Posted: Thu Jan 19, 2006 9:55 pm Post subject: (No subject)
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
Posted: Thu Jan 19, 2006 9:58 pm Post subject: (No subject)
wtd wrote:
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
Posted: Thu Jan 19, 2006 10:04 pm Post subject: (No subject)
Then I cannot help. The code I posted compiles flawlessly with GCC 3.4.2 (MinGW).
Tubs
Posted: Fri Jan 20, 2006 1:01 am Post subject: (No subject)
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:
code:
#include <stdio.h>
#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 );
}
Sponsor Sponsor
wtd
Posted: Fri Jan 20, 2006 1:13 am Post subject: (No subject)
Use a loop to follow the links?
Tubs
Posted: Tue Jan 24, 2006 7:29 pm Post subject: (No subject)
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:
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) = %.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
Posted: Tue Jan 24, 2006 7:34 pm Post subject: (No subject)
Look at that code really closely.
Now... remove all of the code inside the functions, leaving just the headers? See the problem?
Tubs
Posted: Tue Jan 24, 2006 8:03 pm Post subject: (No subject)
wtd wrote:
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.
wtd
Posted: Tue Jan 24, 2006 9:58 pm Post subject: (No subject)
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).