SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Try It Free
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 31st, 2002, 11:00 AM
learning110 learning110 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 7 learning110 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Program in c

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.

Thanx in advance
Jason
Jason_pink110@hotmail.com

#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 */
scanf ("%d", &premium); /* read integer */
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");

}

Reply With Quote
  #2  
Old January 1st, 2003, 09:14 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
Jason,

You might want to read the sticky post at the top of the forum: How to post a question. That much code is too much to go through.

It might not be appropriate for you to try to get us to do your homework. Every developer has their own style, and it will be very obvious to an instructor who's reading your code if you turn in code that isn't yours.

As for solving your problem, you need to break it down into simple pieces and solve each piece individually. C can quickly overwhelm a developer, so working on only a small piece at one time is the easiest. Also, your code was rife was global variables. Global variables are a problem, and you probably don't need them. Consider making them local to the function where you're using them. You have way too many comments in your code, as well. We all know that scanf("%d") reads an integer, we don't need a comment to tell us that. If your professor is telling you to comment that heavily, then do it, but you won't win any friends in the outside world by commenting like that.


A general routine to find the highest value in an array of integers might look like this:

Code:
/* Return the largest element in an array. */
int arraymax(int* value, int size) {

  int i;
  int max = value[0];

  for(i=1; i < size; i++)
    if (value[i] > max)
      max = value[i];

  return max;

}


Note that I started out by assigning the maximum to the first value in the array. Assigning a constant value to max is a bad idea, because your constant could wind up being larger than the maximum value in the array.
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/

Reply With Quote
  #3  
Old January 1st, 2003, 05:36 PM
learning110 learning110 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 7 learning110 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi m8,

Thanx for the code, and i know i posted alot but last time i tried picking out a small section i ended missing an important bit. Im not trying to get people to do my work for me - just trying to get examples to learn from. In class i was given a sheet to make a simple program saying 'hello world' and then just given this assignment. Iv been looking at source code and then trying to work out what it means by using books.

So far i can find out the maxmum of a number fine i.e

Employee 1 = 60
employee 2 = 45
employee 3 = 52

I can get my program to return 60 as the highest number, but want it to bring back employee 1 instead. I dont understand the relationship in c - im looking at it all from the point of view of excel and lookup tables.

Anyway thanx for taking the time to write the code, its appreciated,
Jason

Reply With Quote
  #4  
Old January 1st, 2003, 07:03 PM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
Do yourself the favor of not thinking about anything in C as related to excel.

The code that I provided, with a slight modification, will still solve your problem for you. Modify the function to have an additional variable that tracks the index of the array with the highest value. Then instead of returning the highest value, return the index of the highest value.

What text are you using for your class? When I was teaching I found that the assigned text was a little less than useful for learning the language. We used it because the department stipulated it, but I recommended some outside texts for my students that turned out to be more useful.

If you don't have it and you're planning on studying Computer Science, grab Brian Kernighan & Dennis Ritchie's "The C Programming Language" and/or Bjarne Stroustrup's "The C++ Programming Language". Any bookstore with a computer section worthy of the name will have both. I refer to mine regularly.

Reply With Quote
  #5  
Old January 1st, 2003, 09:41 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
Here's one way to do it (with minimal error-checking, etc):

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>

#define MIN(x,y) ((x)<(y) ? (x) : (y))
#define MAX(x,y) ((x)>(y) ? (x) : (y))

#define MAX_EMPLOYEES 10
#define WAGE          5.75
#define STD_HOURS     38

#define MAX_BUFFER_SIZE 80

typedef struct {
  float std;
  float ot;
  float total;
} WAGES;

void sighandler(int i)
{
    printf("\n** Caught signal %d. Exiting...\n", i);
    exit(EXIT_FAILURE);
}

int readline(char* buffer)
{
    int count = 0;
    char ch;

    memset(buffer, 0x00, MAX_BUFFER_SIZE);
    ch = getchar();

    while ((ch != '\n') && (count < MAX_BUFFER_SIZE)) {
        buffer[count++] = ch;
        ch = getchar();
    }

    buffer[count] = 0x00;   /* null-terminate */
    return strlen(buffer);
}

int main(int argc, char* argv[])
{
  int hours=0, ot=0, maxemp=0, numemp=0;
  float premium=0, totalpay=0, maxpay=0;
  char buf[MAX_BUFFER_SIZE];

  WAGES emp[MAX_EMPLOYEES];

  signal(SIGINT,  sighandler);
  signal(SIGPIPE, sighandler);

  printf("\nEnter the overtime premium percentage: ");
  readline(buf);
  premium = MAX(atof(buf), 1);

  for (register int i=0; i < MAX_EMPLOYEES; ) {
    printf("\nEnter the hours worked by employee %d: ", i+1);
    readline(buf);

    hours = atoi(buf);
    ot    = MAX(0, (hours - STD_HOURS));
    hours = MIN(hours, STD_HOURS);

    printf("\nStandard hours: %d\n", hours);
    printf("Overtime hours: %d\n", ot);
    printf("\nIs this correct? (y/n): ");
    readline(buf);

    if (toupper(buf[0]) == 'Y')
        numemp++;
    else
        continue;

    emp[i].std   = (float)(hours * WAGE);
    emp[i].ot    = (float)((WAGE * premium) * ot);
    emp[i].total = (float)(emp[i].std + emp[i].ot);

    printf("\nWAGES:\n");
    printf("Standard:  %.2f\n", emp[i].std);
    printf("Overtime:  %.2f\n", emp[i].ot);
    printf("Total:     %.2f\n", emp[i].total);

    totalpay += emp[i].total;

    if (emp[i].total > maxpay) {
      maxpay = emp[i].total;
      maxemp = i+1;
    }

    printf("\nEnter wages for another employee? (y/n): ");
    readline(buf);

    if (toupper(buf[0]) == 'Y') {
      i++;
      continue;
    }
    break;
  }

  printf("\nTotal pay for %d employees: %.2f\n", numemp, totalpay);
  printf("The max earner was employee %d with %.2f\n", maxemp, maxpay);
  printf("\nWAGE LIST:\n");

  for (register int i=0; i < numemp; i++)
    printf("Employee %d: %.2f\n", i+1, emp[i].total);

  return EXIT_SUCCESS;
}


Last edited by vpopper : January 2nd, 2003 at 01:17 AM.

Reply With Quote
  #6  
Old January 2nd, 2003, 10:27 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,834 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 23 h 30 m 30 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
no offence vpopper but I would imagine that for someone who is just starting out in C, the code you posted is way over thier head.
You can complete the above assignement as ClayDowling stated earlier with a simple loop and array returning the array index for the employee number with the highest pay.

Later on down the line he/she can get into structs, macros, memory handling and signal handling. Also, Clay mentioned over-commenting to learning110 and I will mention under-commenting to you, especially when you are helping someone, comments are good.

Reply With Quote
  #7  
Old January 7th, 2003, 04:42 PM
learning110 learning110 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 7 learning110 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

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");

Reply With Quote
  #8  
Old January 8th, 2003, 07:24 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
learning110:

The following example would also work:
Code:
int main() {

  int cursalary;
  int maxsalary = 0;
  int maxemployee = -1;
  int i;

  for(i=0; i < 6; i++) {
    printf("Employee %d: ", i);
    scanf("%d", &cursalary);

    if (cursalary > maxsalary) {
      maxsalary = cursalary;
      maxemployee = i;
    }

  }

  printf("Employee %d had the highest salary, $%d", maxemployee, maxsalary);

  return 0;

}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Program in c


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway