# Thread: Help with colege assignment

1. ## Help with colege assignment

Hello,

Im new to c trying to do a college assignment with it. I have to find the wages of five employees each week. I then have to say which employee gets paid the most. I have managed to get my program to display the highest pay, but not the employee that gets it. Any help would be much appreciated.

Jason
Jason_pink110@hotmail.com
Code:
```#include <stdio.h>
int max_array(int a[], int num_elements);
int employee1, employee2, employee3, employee4, employee5, premium, emp1stndpay, emp1overpay, emp1pay, emp1overtpay, emp1total, emp2stndpay, emp2overpay, emp2pay, emp2overtpay, emp2total, emp3stndpay, emp3overpay, emp3pay, emp3overtpay, emp3total, emp4stndpay, emp4overpay, emp4pay, emp4overtpay, emp4total, emp5stndpay, emp5overpay, emp5pay, emp5overtpay, emp5total, totalpay;
void print_array(int a[], int num_elements);

void main(void)

{
printf ("----------------------------Pay Program-----------------------\n "); /* prompt */

printf ("Enter the overtime premium (%): "); /* prompt */
printf ("***************************************************************\n "); /* prompt */
printf ("Please enter number of hours worked this week by EMPLOYEE1: "); /* prompt */
scanf ("%d", &employee1); /* read integer */
if (employee1<38)
printf ("--The standard hours worked by employee1 would be = %d\n", employee1); /* print sum */
else
printf ("--The standard hours worked by employee1 would be = %d\n", 38); /* print sum */
if ((employee1-38)<0)
printf ("--The overtime hours worked by employee1 would be = %d\n", 0); /* print sum */
else
printf ("--The overtime hours worked by employee1 would be = %d\n", (employee1-38)); /* print sum */
printf ("Please confirm amount of standard hours to be worked: "); /* prompt */
scanf ("%d", &emp1stndpay); /* read integer */
printf ("Please confirm amount of overtime hours to be worked: "); /* prompt */
scanf ("%d", &emp1overpay); /* read integer */
emp1pay = emp1stndpay * 5.75;
printf ("--Standard pay = %d\n", emp1pay); /* print sum */
emp1overtpay = (((5.75/100)* premium)+5.75) * emp1overpay;
printf ("--Overtime pay = %d\n", emp1overtpay); /* print sum */
emp1total = emp1overtpay + emp1pay;
printf ("--The total weeks pay for Employee 1 = %d\n", emp1total); /* print sum */
printf ("***************************************************************\n "); /* prompt */

printf ("Please enter number of hours worked this week by EMPLOYEE2: "); /* prompt */
scanf ("%d", &employee2); /* read integer */
if (employee2<38)
printf ("--The standard hours worked by employee2 would be = %d\n", employee2); /* print sum */
else
printf ("--The standard hours worked by employee2 would be = %d\n", 38); /* print sum */
if ((employee2-38)<0)
printf ("--The overtime hours worked by employee2 would be = %d\n", 0); /* print sum */
else
printf ("--The overtime hours worked by employee2 would be = %d\n", (employee2-38)); /* print sum */
printf ("Please confirm amount of standard hours to be worked: "); /* prompt */
scanf ("%d", &emp2stndpay); /* read integer */
printf ("Please confirm amount of overtime hours to be worked: "); /* prompt */
scanf ("%d", &emp2overpay); /* read integer */
emp2pay = emp2stndpay * 5.75;
printf ("--Standard pay = %d\n", emp2pay); /* print sum */
emp2overtpay = (((5.75/100)* premium)+5.75) * emp2overpay;
printf ("--Overtime pay = %d\n", emp2overtpay); /* print sum */
emp2total = emp2overtpay + emp2pay;
printf ("--The total weeks pay for Employee 2 = %d\n", emp2total); /* print sum */
printf ("***************************************************************\n "); /* prompt */

printf ("Please enter number of hours worked this week by EMPLOYEE3: "); /* prompt */
scanf ("%d", &employee3); /* read integer */
if (employee3<38)
printf ("--The standard hours worked by employee3 would be = %d\n", employee3); /* print sum */
else
printf ("--The standard hours worked by employee3 would be = %d\n", 38); /* print sum */
if ((employee1-38)<0)
printf ("--The overtime hours worked by employee3 would be = %d\n", 0); /* print sum */
else
printf ("--The overtime hours worked by employee3 would be = %d\n", (employee3-38)); /* print sum */
printf ("Please confirm amount of standard hours to be worked: "); /* prompt */
scanf ("%d", &emp3stndpay); /* read integer */
printf ("Please confirm amount of overtime hours to be worked: "); /* prompt */
scanf ("%d", &emp3overpay); /* read integer */
emp3pay = emp3stndpay * 5.75;
printf ("--Standard pay = %d\n", emp3pay); /* print sum */
emp3overtpay = (((5.75/100)* premium)+5.75) * emp3overpay;
printf ("--Overtime pay = %d\n", emp3overtpay); /* print sum */
emp3total = emp3overtpay + emp3pay;
printf ("--The total weeks pay for Employee 3 = %d\n", emp3total); /* print sum */
printf ("***************************************************************\n "); /* prompt */

printf ("Please enter number of hours worked this week by EMPLOYEE4: "); /* prompt */
scanf ("%d", &employee4); /* read integer */
if (employee4<38)
printf ("--The standard hours worked by employee4 would be = %d\n", employee4); /* print sum */
else
printf ("--The standard hours worked by employee4 would be = %d\n", 38); /* print sum */
if ((employee4-38)<0)
printf ("--The overtime hours worked by employee4 would be = %d\n", 0); /* print sum */
else
printf ("--The overtime hours worked by employee4 would be = %d\n", (employee4-38)); /* print sum */
printf ("Please confirm amount of standard hours to be worked: "); /* prompt */
scanf ("%d", &emp4stndpay); /* read integer */
printf ("Please confirm amount of overtime hours to be worked: "); /* prompt */
scanf ("%d", &emp4overpay); /* read integer */
emp4pay = emp4stndpay * 5.75;
printf ("--Standard pay = %d\n", emp4pay); /* print sum */
emp4overtpay = (((5.75/100)* premium)+5.75) * emp4overpay;
printf ("--Overtime pay = %d\n", emp4overtpay); /* print sum */
emp4total = emp4overtpay + emp4pay;
printf ("--The total weeks pay for Employee 4 = %d\n", emp4total); /* print sum */
printf ("***************************************************************\n "); /* prompt */

printf ("Please enter number of hours worked this week by EMPLOYEE5: "); /* prompt */
scanf ("%d", &employee5); /* read integer */
if (employee5<38)
printf ("--The standard hours worked by employee5 would be = %d\n", employee5); /* print sum */
else
printf ("--The standard hours worked by employee5 would be = %d\n", 38); /* print sum */
if ((employee5-38)<0)
printf ("--The overtime hours worked by employee5 would be = %d\n", 0); /* print sum */
else
printf ("--The overtime hours worked by employee5 would be = %d\n", (employee5-38)); /* print sum */
printf ("Please confirm amount of standard hours to be worked: "); /* prompt */
scanf ("%d", &emp5stndpay); /* read integer */
printf ("Please confirm amount of overtime hours to be worked: "); /* prompt */
scanf ("%d", &emp5overpay); /* read integer */
emp5pay = emp5stndpay * 5.75;
printf ("--Standard pay = %d\n", emp5pay); /* print sum */
emp5overtpay = (((5.75/100)* premium)+5.75) * emp5overpay;
printf ("--Overtime pay = %d\n", emp5overtpay); /* print sum */
emp5total = emp5overtpay + emp5pay;
printf ("--The total weeks pay for Employee 5 = %d\n", emp5total); /* print sum */
printf ("***************************************************************\n "); /* prompt */
totalpay = emp1total + emp2total + emp3total + emp4total + emp5total;
printf ("--The total weeks pay for all Employees = %d\n", totalpay); /* print sum */
printf ("--------------------------------------------------------------\n "); /* prompt */
int a[10] = {emp1total, emp2total, emp3total, emp4total, emp5total, 0, 0, 0, 0, 0};
int max;

/*printf("\nArray:\n");*/
/*print_array(a, 10);*/

max = max_array(a, 10);
printf("The max is %d\n", max);
if (max = emp1total)
printf ("--Employee1 is paid the most"); /* print sum */
if (max = emp2total)
printf ("--Employee2 is paid the most"); /* print sum */
if (max = emp3total)
printf ("--Employee3 is paid the most"); /* print sum */
if (max = emp4total)
printf ("--Employee4 is paid the most"); /* print sum */
if (max = emp5total)
printf ("--Employee5 is paid the most"); /* print sum */
}

int max_array(int a[], int num_elements)
{
int i, max=-32000;
for (i=0; i<num_elements; i++)

if (a[i]>max)

max=a[i];

return(max);

}

void print_array(int a[], int num_elements)
{
int i;
for(i=0; i<num_elements; i++)

printf("%d ", a[i]);

printf("\n");

}```
&#91;code]&#91;/code]tagged by Salem

Use CODE tags

3. ## Code tags?

im sorry this is probably going to sound like a stupid question but what are code tags? would it be possible to send an example?

Thanx

4. ## code tags

Code tags put little lines and a title of code around your code like this

Code:
```#include <stdio.h>

int main(void)
{
printf("Code tags are great");
return 0;
}```

#include <stdio.h>

int main(void)
{
printf("Code tags are great");
return 0;
}

If you copy and paste your code and your code has lots of tabs etc. in it will keep them exactly how they are with out wraping your code into the window!

Go to

5. thanx everybody i'll ive it a try

6. That's too long to mill through and work out. If you're having problems then try posting only the part that you're having a problem with. Myself, I'd handle things a little differently. Right or wrong, here you go.
Code:
```#include <stdio.h>
#include <ctype.h>

#define MAXEMP 3
#define MAXWAGE 5.75

int main(void)
{
int count, place;
char trash;
double temp;

struct employee
{
char name[20];
int regHours;
int otHours;
double total;
};

struct employee emp[MAXEMP];

temp = 0;

for(count = 0; count < MAXEMP; count++)
{
printf("Enter employees name: ");
fgets(emp[count].name, 20, stdin);

printf("Enter hours worked: ");
scanf("%d%c", &emp[count].regHours, &trash);

printf("Enter OT hours worked: ");
scanf("%d%c", &emp[count].otHours, &trash);

emp[count].total = MAXWAGE * emp[count].regHours + MAXWAGE * emp[count].otHours * 1.5;

if(temp < emp[count].total)
{
place = count;
temp = emp[count].total;
}
}

printf("\nThe employee earning the most was %s", emp[place].name);
printf("This person earned %.2f", emp[place].total);

return 0;
}```
Let me know if it works as I didn't finish playing with it. :P

7. Hi,

Thanx for all your help m8. It definately works but i dont understand it all entirely. We were all given a sheet to a make a simple program saying 'hello world' and then just given this assignment to do. Im using it your code as a reference and then trying to make heads and tails of it by reading c books.

one thing i dont understand is what is the realtionship between the numbers and the names on your program. In somthing like excel you have a table which looks up say the highest value and then brings back the name in the cell beside. How does this work in c?

If you have a few minutes to spare and dont mind, i would really appreciate it if you could annotate some of the code so i can see whats going on.

Anyway thanx for code,
Jason

8. How about the same thing but using arrays then since you seem to be familiar with those.

Code:
```#include <stdio.h>
#include <string.h>

#define OTSCALE 1.5 // standard US overtime rate
#define NUMEMP 5    // number of employees

int main(void)
{
int i, j, index;

double total, temp, earnings[NUMEMP] = { 0.0 };
float wages, regHours, otHours;

char empName[NUMEMP][20];
char trash;

temp = 0;

for(i=0; i < NUMEMP; i++)
{
printf("Enter the name of employee %d: ", i+1);
fgets(empName[i], 20, stdin);

j = strlen(empName[i])-1; // get rid of fgets '\n';
empName[i][j] = '\0';

printf("Enter the hourly rate for %s: ", empName[i]);
scanf("%f%c", &wages, &trash);

printf("Enter the hours worked for %s: ", empName[i]);
scanf("%f%c", &regHours, &trash);

if(regHours > 40)
{
otHours = regHours - 40;
// 
regHours = 40;
//  [/edit]
}
else
otHours = 0;

total = wages * regHours + wages * otHours * OTSCALE;
earnings[i] = total;

if(temp < total)
{
index = i;
temp = total;
}

printf("\n");
}

for(i=0; i < NUMEMP; i++)
printf("%s earned %.2f\n", empName[i], earnings[i]);

printf("\n%s earned the most, and this amount was %.2f",
empName[index], temp);

printf("\n\nThe End.");

return 0;
}```
I'm not sure what you were trying with your functions, so I left them out. If you understand this, it serves your needs, and most importantly it compiles then feel free to use it, modify it, or whatever you need.

Best Wishes.

9. Hello,

Thanx to everyone that helped, the variety of answers i got will give me plenty to go through. Below i have included how i eventually solved it if anyones interested, i know its very simplistic but works.

Thanx again,
Jason
Code:
```if (total < total2)
if (total2 < total3)
if (total3 < total4)
if (total4 < total5)
printf ("**The highest paid employee is EMPLOYEE 5\n");
else
printf ("**The highest paid employee is EMPLOYEE 4\n");
else
if (total3 < total5)
printf ("**The highest paid employee is EMPLOYEE 5\n");
else
printf ("**The highest paid employee is EMPLOYEE 3\n");
else
if (total2 < total4)
printf ("**The highest paid employee is EMPLOYEE 4\n");
else
printf ("**The highest paid employee is EMPLOYEE 2\n");
else
if (total < total3)
printf ("**The highest paid employee is EMPLOYEE 3\n");
else
printf ("**The highest paid employee is EMPLOYEE 1\n");```

Hammer says: You forgot the code tags again

10. While it may work, it's a horrible solution.

Code:
```pesudocode

for x = 0; x < max_employees; x++
if employee[x].something > highestvalue
highestvalue = employee[x].something
highest_employee = x
if employee[x].something < lowestvalue
lowestvalue = employee[x].something
lowest_employee = x

printf("%d is the highest paid employee, making %d\n", highest_employee, employee[highest_employee].something );
printf("%d is the lowest paid employee, making %d\n", lowest_employee, employee[lowest_employee].something );```
Much cleaner.

Quzah.

11. >> i know its very simplistic but works
Actually, I'd say its complex, or maybe the word I should use is obfuscated. It's tricky to follow the logic behind all those if tests. I'd suggest you rethink and find another way, like using arrays instead of variables named total1, total2, total3 etc etc.

Just to show you, this is how your code looked after I ran it through my code tidying prog:
Code:
```if (total < total2)
{
if (total2 < total3)
{
if (total3 < total4)
{
if (total4 < total5)
printf("**The highest paid employee is EMPLOYEE 5\n");
else
printf("**The highest paid employee is EMPLOYEE 4\n");
}
else if (total3 < total5)
printf("**The highest paid employee is EMPLOYEE 5\n");
else
printf("**The highest paid employee is EMPLOYEE 3\n");
}
else if (total2 < total4)
printf("**The highest paid employee is EMPLOYEE 4\n");
else
printf("**The highest paid employee is EMPLOYEE 2\n");
}
else if (total < total3)
printf("**The highest paid employee is EMPLOYEE 3\n");
else
printf("**The highest paid employee is EMPLOYEE 1\n");```
Dammit, quzah