Iterative functions

This is a discussion on Iterative functions within the C Programming forums, part of the General Programming Boards category; Hello world! I am having a hard time dealing with a machine problem. The problem requires me to create a ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    9

    Iterative functions

    Hello world!

    I am having a hard time dealing with a machine problem. The problem requires me to create a program that will enable the user to input the number of employees of a certain company. After that, the program must ask the user to input the data of each employee(Employee Number, Hours of Work, and Employee type--permanent or temporary). It will do the loop until all of the employees' data are stored. Then the program must compute for their salaries and display it on the screen in table format.

    Now my problem is that I am not allowed to use Iterative constructs/functions. I am also not allowed to use function calls, strings, arrays, pointers, and the likes. I can only use the basic printf/scanf functions, variable declarations and the conditional statements(if-else and switch), plus the #define. No more. Another problem is that I don't know where to store the input values and how to print the table out.

    Please help me.
    I really hope you can help me with this.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this a trick question?

    If you can't repeat something (iterate) in the code, and not call funcitons, then I doubt that you can solve the problem as described.

    I would ask for clarification on this, because it seems like a very difficult to solve problem [yes, you could repeat the code, like this
    Code:
    ... 
       if (num_employees > 1) 
          ... // ask employee one values and process it's calculation. 
       if (num_employees > 2)
          ... 
       if (num_employees > 3)
          ...
    But it becomes highly unpractical to solve this way if the number of employees is bigger than a few dozen - one million [ok, so that's probably making it the worlds largest employer, but still] employees would make the code unmanageably large.]

    I'm sure there's something that has gone wrong in the problem description - or it's just a trick question to prove the fallibility of the solution.

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

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Do you know how many employees are, or the max number? Can you use goto (I suppose not)?
    The input values can be stored in variables. If you can't use arrays (why???) use structs, or just plain variables.

    Also, can you use the define to create an array? Like a macro:
    Code:
    #define array(num) *(array+(num))
    which will create effectively an array, if you can use malloc to allocate memory...

    As for the print.... you just print every value, don't know where is the problem there. Elaborate a bit

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Also, can you use the define to create an array? Like a macro:
    Code:
    #define array(num) *(array+(num))
    which will create effectively an array, if you can use malloc to allocate memory...
    If you can't use pointers, I don't think you can "fake arrays" either. You definitely need a pointer to use malloc [unless we assume that it's ONLY the pointer operator that you can't use, and we make use of various typecasts and macros to "hide" the fact that we have a pointer - but again, I don't thing it's a "obfuscated C" task]. I think there's either a misunderstanding or a trick somewhere that we're missing.

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

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Well thanks for the help!

    Okay, this is the thing. the problem has been modified and now we are allowed to use iterations and function calls, plus the if-else and switch statements and #define[at last!].
    but still I have a problem. Where can I put all those inputs? The maximum number of employees to be entered is 20. and how can i print them out. I can' use arrays, strings, pointers, structs, etc. Just the basic I/O of the stdio.h library, and variable declarations.






    Quote Originally Posted by matsp View Post
    Is this a trick question?
    If you can't repeat something (iterate) in the code, and not call funcitons, then I doubt that you can solve the problem as described.

    I would ask for clarification on this, because it seems like a very difficult to solve problem [yes, you could repeat the code, like this
    Code:
    ... 
       if (num_employees > 1) 
          ... // ask employee one values and process it's calculation. 
       if (num_employees > 2)
          ... 
       if (num_employees > 3)
          ...
    But it becomes highly unpractical to solve this way if the number of employees is bigger than a few dozen - one million [ok, so that's probably making it the worlds largest employer, but still] employees would make the code unmanageably large.]

    I'm sure there's something that has gone wrong in the problem description - or it's just a trick question to prove the fallibility of the solution.

    --
    Mats

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Well thanks for the help!

    Okay, this is the thing. the problem has been modified and now we are allowed to use iterations and function calls, plus the if-else and switch statements and #define[at last!].
    but still I have a problem. Where can I put all those inputs? The maximum number of employees to be entered is 20. and how can i print them out. I can' use arrays, strings, pointers, structs, etc. Just the basic I/O of the stdio.h library, and variable declarations.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Wait. If i would use the pseuocode you posted here, how will it run/work? What will the compiler do? Thanks a lot!


    Quote Originally Posted by matsp View Post
    Is this a trick question?

    If you can't repeat something (iterate) in the code, and not call funcitons, then I doubt that you can solve the problem as described.

    I would ask for clarification on this, because it seems like a very difficult to solve problem [yes, you could repeat the code, like this
    Code:
    ... 
       if (num_employees > 1) 
          ... // ask employee one values and process it's calculation. 
       if (num_employees > 2)
          ... 
       if (num_employees > 3)
          ...
    But it becomes highly unpractical to solve this way if the number of employees is bigger than a few dozen - one million [ok, so that's probably making it the worlds largest employer, but still] employees would make the code unmanageably large.]

    I'm sure there's something that has gone wrong in the problem description - or it's just a trick question to prove the fallibility of the solution.

    --
    Mats

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The simple solution is to just ask for the number of employees, then make a loop that reads each employee's data and output the result of the pay calculation.

    This is what C_ntua already suggested, but I guess it may have got lost in the other posts with the confusion avoiding loops (iterations).

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  3. splitting linked list recursive vs. iterative
    By Micko in forum C Programming
    Replies: 7
    Last Post: 03-17-2005, 04:51 PM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21