when i did
it says no variable dCode:#include <stdio.h> #define d int main() { int t; int array[] = {1,2,2,3,5,6,1}; d=7; return 0; }
??i cant use global variable
when i did
it says no variable dCode:#include <stdio.h> #define d int main() { int t; int array[] = {1,2,2,3,5,6,1}; d=7; return 0; }
??i cant use global variable
#define is a macro, not a variable. If you want a variable you'll have to declare it just like you declare every other variable, like "int d" or similar.
the problem is i need to keep a certain original value that i input in main
the process is not working because in the partition function
the t_sum changes all the time
but i want it to keep its first sum that he got
??
Code:#include <stdio.h> int recursive_partition(int array[], int size, int start, int end); int partition(int arr[], int size); int main() { int t; int array[] = {1,2,2,3,5,6,1}; t=partition(array,7); printf("%d",t); return 0; } int recursive_partition(int array[], int size, int start, int end)//sum of a given range { int result; if (start==end) { return 0; } result=array[start]+recursive_partition(array,size,start+1,end); printf("%d\n",result); return array[start]+recursive_partition(array,size,start+1,end); } int partition(int array[], int size) { int x=0; int t_sum,half; t_sum=recursive_partition(array,size,0,size); half=t_sum/2; if (size==0) { return 0; } if (recursive_partition(array, size, x,size)!=half) { return partition(array,size-1); } else { return 1; } }
Last edited by transgalactic2; 01-19-2009 at 04:22 PM.
Sounds like you need the "static" keyword to so the value doesn't get changed.
OS: Linux Mint 13(Maya) LTS 64 bit.
What do you mean you want to keep it?
Keep it for what/how long?
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
i need to keep the size of the array into another variable inside
partition for the whole running of the program.
the problem is that my size parameter ha to be cut by 1
in every recursive call
but i have to keep some where accessible the original value of size
as it was passed to the function.(without the use of public variables)
So, you use a local variable in main, and pass it to the function, just like any other local variable/argument passing. You can't have it both ways - global and not global. You could have a static variable, so that it's not visible outside this module - in which case it is a "global for this file", but I don't think that is the right thing here either.
But again, it seems like you are trying to solve problems that aren't really problems - just like tying your shoes with one hand doesn't actually make any sense unless you ONLY HAVE ONE HAND. In the C language, there are many things you can, should, can't and shouldn't do. But making rules about how you should solve a particular problem, and then retrofitting a solution to solve it from those rules, without understanding what the rules are there for is doing it backwards.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
the problem is that i am not allowed to add parameters to the partition signature
only to the external function
Don't reduce size each round, keep that parameter as the size of the entire array. To get the size of the partition use (end-start).
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
but i need to reduce by 1 the end that i am sending to recursive_partition
the only way that i found is sending size-1 to the next partition call
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
my start value is always starts from 0
so there is no problem
but the problem is with the "end" ,it has to be reduced by one each recursive call
that i make from the partition function
how am i supposed to reduce it by one
i am not allowed to add parameters on the signature of partition function
the only "index" i could use
is the size variable.
i call
from the partition functionCode:recursive_partition(int array[], int size, int start, int end)
where x is the start (always zero)Code:recursive_partition(array, size, x,size)!=half
and size is used to mark end
i cant put
local variable in the partition function called "end=size"
and say
like thisCode:recursive_partition(array, size, x,end-1)!=half
i get the same the same start and the same end each callCode:#include <stdio.h> int recursive_partition(int array[], int size, int start, int end); int partition(int arr[], int size); int main() { int t; int array[] = {1,2,2,3,5,6,1}; t=partition(array,7); printf("%d",t); return 0; } int recursive_partition(int array[], int size, int start, int end)//sum of a given range { int result; if (start==end) { return 0; } result=array[start]+recursive_partition(array,size,start+1,end); printf("%d\n",result); return array[start]+recursive_partition(array,size,start+1,end); } int partition(int array[], int size) { int end=size; int x=0; int t_sum,half; t_sum=recursive_partition(array,size,0,size); half=t_sum/2; if (size==0) { return 0; } if (recursive_partition(array, size, x,end-1)!=half) { return partition(array,size); } else { return 1; } }
i need to get the end to go down each call
??
what do you mean
"instead of size to determine if you have more data to process or not."
what do you mean by data
??
i cant see the idea
what should i do here?