Thread: functions - please help!!!!

  1. #1

    Question functions - please help!!!!

    I am in the second semester of computer programming and am not so familiar with functions. Arrays I know but don't know how to use them in functions either.

    I am looking for someone who can help me with the following:

    The problem is as follows:
    Read two numbers, representing a rate of pay ($ per hour) and a number of hours. Print out the total pay, with hours up to 40 being paid at a basic rate, from 40 to 60 at a rate-and-a-half, above 60 at double-rate. Print the pay as $ to 2 decimal places.

    Terminate the loop when a zero rate is encountered. At the end of the loop, print out total pay.

    The code for computing the pay from the rate and hours is to be written as a function.

    A loop , arrays and at least two functions should be used.
    One function for calculation and another for printing the total pay.

    The output format should look something like this:

    Pay at $2.10 / hr for 12 hours is $25.20
    Pay at $2.20 /hr for 48 hours is $114.40
    Pay at $2.40 / hr for 68 hours is $206.40
    Pay at $2.60 / hr for 48 hours is $135.20
    Pay at $2.80 / hr for 68 hours is $240.80
    Pay at $3.00 / hr for 48 hours is $156.00

    Total Pay is 928.80

    I've tried to write the program without arrays, and it kind of works , but don't know how to use arrays and functions at the same time

    This is how I started:

    Double pay_calc(double[10], double rate[10]);
    Double total_pay(double);
    Main ()
    double num1[10], num2[10];
    double j=0;
    for (j=0;j<10;j++){
    printf("please enter hours for employee:",j+1);

    for (j=0;j<10;j++){
    printf("\nplease enter rate for employee:"j+1);

    if (num1[j]<0 || num2[j]<0)
    printf("\nplease enter a positive value!!!");

    return 0;

    Double pay_calc(double hour[10], double[10])
    double j=0, pay_calc[10];
    for (j=0;j<10;j++){
    if hour[j]>60
    else if
    else if

    printf("\n\n Pay at %.2lf for %.2lf hours is %.2lf", rate[j],hour[j], pay_calc[j]);
    return pay_calc[10];

    Now I need another function that prints the total pay, how do I terminate the loop when a zero is entered?

    PLEASE HELP ! ! ! Your urgent assistancewill be highly appreciated.

  2. #2
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Ok, first thing, when using one dimensional arrays with functions you don't pass the bounds of the array, so your original function should become:

    Double pay_calc(double hour[10], double[10]){...}
    // should be:
    double pay_calc(double hour[], double rate[], int hourArraySize, int rateArraySize){...}
    // and the declaration would be:
    double pay_calc(double[], double[], int, int);
    Then, within your function the 'for' loops would look like:

    for(i=0; i<hourArraySize, i++)
         do stuff to hour[i];

    Another thing I can notice is your return value in the pay_calc function is incorrect:

    return pay_calc[10];

    This would return the value held at pay_calc[10], which is a piece of memory you don't own!

    If you want to return an array you will have to return a pointer to it. So the function becomes:

    double *pay_calc(......)

    and you return:

    return pay_calc;

    The most important thing about learning to use arrays with functions is realising the connection between arrays and pointers, look this up and that should point you in the right direction (no pun intended)!

    As for your problem of not being able to check for an input of zero, your error checking code:
    if (num1[j]<0 || num2[j]<0)
    printf("\nplease enter a positive value!!!");
    should be in your loops because, as it is, you are only checking a single element of each array (num1[j]). You should check that value just after it has been entered (still inside the for loop). So you get:

    for (j=0; j<10; j++)
       printf("\nplease enter rate for employee:"j+1); // j+1??? what is this intended to do?
          if(num2[j] < 0)
             printf("\nplease enter a positive value!!!");
       }while(num2[j] < 0);
    hope that helps

    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  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