Thread: arithmetic without variables?

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    arithmetic without variables?

    I have been told to write a program which calculates total cost using values for amounts of resources and cost. I am supposed to write the program in functions, which I understand how to do although I am told I cannot use global variablesn and don't need to use local variables.

    But I don't understand how I can do the arithmetic without variables, and also how to pass the values between the functions without using global variables.

    I am new to programming so I am sorry for asking questions which are probably very basic, I have had a quick look through the FAQ's and tutorials but I still can't seem to find anything that would help me.

    Thanks

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    You should pass a values to the functions as parameters... And (seems strange to me) use only these parameters variables in your calculations
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User SpaceCadet's Avatar
    Join Date
    Oct 2006
    Posts
    23
    Code:
    int calcDeficit(int x, int y)
    {
        return (x-y);
    }
    
    int main (void)
    {
        int MUFC_points=25;
        int LFC_points=14;
    
        int deficit=calcDeficit(MUFC_points, LFC_points);
    
        if (deficit > 10)
        {
            printf ("%d points behind. You're season is over Mickey!", deficit);
        }
        else
        {
            printf("Dream on mate, United are %d points ahead already. You know you can't catch them!", deficit);
        }
    }

    Enjoy. Know I did!
    All questions are easy. The answers cause the problem...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Parameters are local variables. The instructions you got are factually incorrect. It is impossible to do anything useful without local variables when you're not allowed to use global variables.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Pity C doesn't have lambda functions You could try argc and argv, but that'd be a stretch.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  6. #6
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Who are such teachers who give these STUPID assignments?

    They should be burned, because they are teaching stupid things...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. simplifying arithmetic using visitor pattern
    By MyglyMP2 in forum C++ Programming
    Replies: 0
    Last Post: 10-07-2007, 01:29 AM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM