Let's start at the top...
Code:
include <stdio.h>
#include <stdlib.h>
long id_number[5]={5466,5546,4432,3345,2345};
float hours[5];
float overtime[5];
float gross[5];
float deduction[5];
float net_pay[5];
float wage[5] = {10.60, 9.75, 10.50,12.25, 8.35};
Eesh. Globals? Why. Ok, no real problem there, but you're usually better off avoiding globals as much as possible. If you insist on using them, make sure and initialize them all at creation time. Just like you have 'id_number' initialized, do the same for the rest. If you just want them all initialized to zero, do:
Code:
float hours[5] = { 0.0 };
Next...
Code:
float get_hours (float wage[])
{
int temp, i = temp;
for (temp=1; temp<6; ++temp)
{
printf ("Enter employee #%d hours worked: ",id_number[temp-1]);
scanf ("%f", &hours[temp-1]);
}
return 0;
}
For starters, i isn't even used in this function, so get rid of it. If you do want it there for some reason, and you actually plan on using it, you should initialize 'temp' first.
Otherwise what happens is that 'temp' has some random uninitialized value (re: it could be anything), and you're giving 'i' whatever that random value happens to be. I doubt you want that.
As previously stated, you may as well make this function 'void', since you don't do anything with the return value.
If you do want it to return something, since you're using a floating point number, you'll need to do:
And finally, your array indexes start at zero and go through size-1. So if your array is declared as 5, you have valid use of 0 through 4. It's clearer if you start your loop indexes accordingly, as this is much easier to read:
Code:
for (temp=0; temp<5; ++temp)
{
printf ("Enter employee #%d hours worked: ",id_number[temp]);
scanf ("%f", &hours[temp]);
No need to keep adding -1 to everything this way. Personal preference, but it's just clearer. Less cluttered.
Next...
Code:
float calculate_overtime (float wage[])
{
...same issue as above with 'i' and 'temp'...
....same issue with loop counter/array...
overtime = hours[temp-1]- 40;
else
overtime = 0;
}
return (overtime);
}
Whenever you assign a value to a floating point number, you need to treat it as such and stop passing it integers.
That would be the correct method. Or you could typecast 0 to float, but that wouldn't be the preferred method.
Next...
Code:
float calculate_gross (float wage[])
{
...same issue with 'i' and 'temp'...
...same issue with loop/array...
}
Nothing really wrong here, other than what I've already mentioned.
Next...
Code:
float calculate_deduction (float gross)
{
{
...temp and i issue again...
...loop/array issue again...
deduction[temp-1]=gross[temp-1]/3;
}
return (deduction);
}
Why do you have two sets of braces here? There is no need for them.
Here you also see why globals are a bad idea. Anyone reading this code says "Where the hell did deduction come from?" Then they have to go look at all your globals to see if it is one. This isn't such a big deal in a small project, but it's annoying, and in a bigger program this would quickly become a nightmare.
I'll skip the rest of the functions, since the issue is similar in all of them.
Code:
int main ()
{
/*Execute functions*/
get_hours (hours);
calculate_overtime (overtime);
calculate_gross (gross);
calculate_deduction (deduction);
calculate_net (net_pay);
output ();
printf ("\n");
/*Keep window open*/
system("PAUSE");
}
Looking at the main function, none of your functions need to return anything, since you ignore the return value of all of them. Make them all void.
Ideally, you'd want to skip all of the globals, and actually use the return value for something. But for now, just take a look at the comments above and see what that gets you.
Quzah.