Searim
Searim

Reputation: 1

putting an array into a function and odd numbers of the array don't work

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime,int p)
{
if (&hoursworked[p] > 40)
    overtime[p] = ((hoursworked[p] - 40) * payperhour[p] * 1.5);
    wage[p] = overtime[p] + (payperhour[p] * 40);
if (hoursworked[p] <= 40)
    overtime[p] = 0;
    wage[p] = hoursworked[p] * payperhour[p];
}



void main(void)
{
int i = 0;
char employee[5][10];
float payperhour[10];
float hoursworked[10];
float wage[10];
float overtime[10];
int p = 0;
// establish the variables needed to run the formulas for pay




for (i = 0; i <= 4; i)
{
    printf("Give me an Employees name.\n");
    scanf_s(" %[^\n]s %d", &employee[i], 10);
    if (strcmp(employee[i], "-1") == 0)
        break;
    printf("What is this Employees wage per hour?\n");
    scanf_s("%f", &payperhour[i]);
    if (payperhour[i] == -1)
        break;
    printf("How many hours did this employee work?\n");
    scanf_s("%f", &hoursworked[i]);
    if (hoursworked[i] == -1)
        break;
    i++;
}
//for loop that assigns all the variables needed to figure out pay
for (p = 0; p <= 4; p)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}
//actualy equation that computes all the pay that everyone is receiving
for (p = 0; p < i; p)
{
    printf("Employee %s:\n", employee[p]);
    printf("Pay per hour:\n %.2f\n", payperhour[p]);
    printf("Hours Worked:\n %.2f\n", hoursworked[p]);
    printf("Gross Pay:\n %.2f\n", wage[p]);
    printf("Overtime pay:\n %.2f\n", overtime[p]);
    printf("Pay after taxes:\n %.2f\n", wage[p] * .8);
    p++;
    //finishes the application by giving the user the information that was computed
}
system("pause");
}

This is my code I am currently taking C programming and have been stuck on this problem. When you input a user into array spot 0 (on all arrays) it works fine. But if you input information into spot 1 or 3 it does not produce a proper wage or overtime for that information. I cannot figure out why it would work on even arrays, but not on even numbers.

the wage and overtime for [1] are always coming out as numbers in the millions but the wage and overtime for [0] is working correctly

ps i know the code is not even near perfect, but i am trying to fix this problem and cannot for the life of me figure out anything or get any information on the problem that i am having.

i have edited to include the entirety of the code as it is being asked for

Upvotes: 0

Views: 54

Answers (3)

chqrlie
chqrlie

Reputation: 144715

Since calculate reads the pth entries of the arrays, the base address of the arrays should be passed. It should be invoked as:

calculate(payperhour, hoursworked, wage, overtime, p);

The syntax in your loop is confusing and likely incorrect:

for (p = 0; p <= 4; p) {
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}

If the arrays have 4 elements, you should stop the loop with i < 4 as the valid index values are 0, 1, 2 and 3. Also increment the index in the for statement, not at the end of the loop body:

for (p = 0; p < 4; p++) {
    calculate(payperhour, hoursworked, wage, overtime, p);
}

Furthermore, there is also a major problem in calculate: Your post's indentation is bogus. With correct indentation it looks like this:

void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime, int p) {
    if (hoursworked[p] > 40)
        overtime[p] = ((hoursworked[p] - 40) * payperhour[p] * 1.5);
    wage[p] = overtime[p] + (payperhour[p] * 40);
    if (hoursworked[p] <= 40)
        wage[p] = hoursworked[p] * payperhour[p];
    overtime[p] = 0;
}

which is incorrect as overtime is not necessarily 0 upon entering the function, and should not be forces to 0 before returning.

The function should be written this way:

void calculate(const float *payperhour, const float *hoursworked, float *wage, float *overtime, int p) {
    if (hoursworked[p] > 40) {
        overtime[p] = ((hoursworked[p] - 40) * payperhour[p] * 1.5);
        wage[p] = overtime[p] + (payperhour[p] * 40);
    } else {
        overtime[p] = 0;
        wage[p] = hoursworked[p] * payperhour[p];
    }
}

EDIT: After re-reading your code, indexing both in the calling sequence and in the function does explain the observed behavior: you are only accessing even indices, as explained by immibis. Apply the corrections explained above to correct both bugs and the program should work correctly.

Upvotes: 1

John Burger
John Burger

Reputation: 3672

You're confusing a few different things here. First, the calling loop:

for (p = 0; p <= 4; p)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p], p);
    p++;
}

This steps through the individual people, and passes their details to calculate(...). There is no need to pass p in - you've already used it to get the data from the arrays. Note that the p++ should be moved to the final part of the for:

for (p = 0; p <= 4; p++)
{
    calculate(&payperhour[p], &hoursworked[p], &wage[p], &overtime[p]);
}

The problem with it skipping entries is because you are repeating the [p] inside calculate(...) after it has already been applied in the call. So calculate(...) needs to be completely rewritten - not the logic, but the variable references:

void calculate(float *payperhour, float *hoursworked, float *wage, float *overtime)
{
    if (*hoursworked > 40)
        *overtime = ((*hoursworked - 40) * *payperhour * 1.5);
    *wage = *overtime + (*payperhour * 40);
    if (*hoursworked <= 40)
        *wage = *hoursworked * *payperhour;
    *overtime = 0;
}

But also note that some of the passed-in variables are only used as inputs to calculations, while others are output. You should distinguish between the two: only pass the values for "input-only" variables, and the addresses into pointer variables for "input/output" variables:

void calculate(float payperhour, float hoursworked, float *wage, float *overtime)
{
    if (hoursworked > 40)
        *overtime = ((hoursworked - 40) * payperhour * 1.5);
    *wage = *overtime + (payperhour * 40);
    if (hoursworked <= 40)
        *wage = hoursworked * payperhour;
    *overtime = 0;
}

for (p = 0; p <= 4; p++)
{
    calculate(payperhour[p], hoursworked[p], &wage[p], &overtime[p]);
}

Note I have also changed your indenting - perhaps you need braces ({ and }) around the statements in the if?

Upvotes: 1

You're passing &payperhour[p] as payperhour and then using payperhour[p].

Note that (&payperhour[p])[p] is the same as payperhour[p+p] or payperhour[p*2].

It looks like you want to call the function like this:

calculate(payperhour, hoursworked, wage, overtime, p);

Upvotes: 2

Related Questions