Storing input values while iterating

This is a discussion on Storing input values while iterating within the C Programming forums, part of the General Programming Boards category; Hello again! I am having a hard time dealing with my machine problem. The problem requires me to create a ...

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

    Exclamation Storing input values while iterating

    Hello again!


    I am having a hard time dealing with my 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.

    Okay, this is the trick. We are now allowed to use iterative functions and function calls. My problem now is how can i store the values in variables for the data of each employee. The maximum number of employees is 20. And we are not allowed to use arrays, strings, pointers, structs and the likes.

    Another problem would be how can i print the table out?

    Thanks for the people who replied on my last post, especially "mats".

    I hope you can help me with this.
    Last edited by russel1013; 07-19-2008 at 07:00 PM. Reason: Lack of detail

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Um, you sure that is really what the requirements are? It seems a little restrictive.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    > Okay, this is the trick. We are now allowed to use iterative functions and function calls
    And this is supposed to teach you what - frustration?

    This is the kind of code real noobs come up with before they learn about arrays and loops. But if you already know these things, then this seems a pointless exercise from a bone-head of a tutor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Since you can't use arrays, and presumably malloc(), your only choice of container is the program stack; i.e. you must use recursion. This has the unfortunate drawback is that this means that the results will come out in reverse order of the input. But besides that it is strait forward.

    Basically you need to write a function with the following structure:
    Code:
    void func(int numEmployees){
        Data data;
        if(numEmployees==0)
            return;
        data = getEmployeeData();
        func(numEmployees-1);
        printSalary(data);
    }
    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.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by King Mir View Post
    This has the unfortunate drawback is that this means that the results will come out in reverse order of the input.
    Print before you make the recursive call to change that:

    Example:

    Code:
    void func(int numEmployees){
        Data data;
        if(numEmployees==0)
            return;
        data = getEmployeeData();
        printSalary(data);
        func(numEmployees-1);
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    Quote Originally Posted by MacGyver View Post
    Print before you make the recursive call to change that:
    Yes it would, but russel1013 said "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." This indicates that the employees data must all be stored before the results are printed. Your solution would not satisfy this.

    Without that requirement, there is no point in using recursion, since you do not actually need to store the data for each employee after you print the result. Just iterate through each element, getting the data, print the result, then write over the data with the next iteration.
    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.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Very good point. I think his assignment sucks.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Well actually King Mir is right.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    9
    Well I am not allowed to use stacks, because it has not been taught to us yet.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Either you got your assigment all confused or your professor is an idiot.

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

    Talking

    Ok!

    Thanks for all the comments about the problem and my teacher. I got a solution to this machine problem...

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Can you post it here, so we all share in the joy?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  2. Replies: 12
    Last Post: 04-12-2009, 05:49 PM
  3. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  4. storing integer values in files using WriteFile
    By Fender Bender in forum Windows Programming
    Replies: 1
    Last Post: 01-14-2006, 11:15 PM
  5. How do I ignore values from an input file
    By jaisch in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2005, 04:08 PM

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