Thread: #define variable from main() command

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    #define variable from main() command

    when i did
    Code:
    #include <stdio.h>
    #define d
    int main()
    {
            int t;
            int array[] = {1,2,2,3,5,6,1};
           d=7;
        return 0;
    }
    it says no variable d
    ??i cant use global variable

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    #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.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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.

  4. #4
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Sounds like you need the "static" keyword to so the value doesn't get changed.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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)

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    the problem is that i am not allowed to add parameters to the partition signature
    only to the external function

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    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
    That's what King Mir tries to say that you DON'T have to do - use the start and end point of the partition, instead of size to determine if you have more data to process or not.

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

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    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
    Code:
    recursive_partition(int array[], int size, int start, int end)
    from the partition function

    Code:
    recursive_partition(array, size, x,size)!=half
    where x is the start (always zero)
    and size is used to mark end
    i cant put
    local variable in the partition function called "end=size"
    and say
    Code:
    recursive_partition(array, size, x,end-1)!=half
    like this
    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 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 get the same the same start and the same end each call
    i need to get the end to go down each call

    ??

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what do you mean
    "instead of size to determine if you have more data to process or not."
    what do you mean by data
    ??

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant see the idea

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what should i do here?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  2. Compiling error: Too many arguments.
    By Tuah in forum C++ Programming
    Replies: 16
    Last Post: 06-10-2008, 04:28 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. Error check problem again
    By rtransfi in forum C Programming
    Replies: 6
    Last Post: 02-27-2003, 04:55 PM
  5. keyboard control
    By MathFan in forum C++ Programming
    Replies: 1
    Last Post: 04-16-2002, 11:55 AM