Re: a little C programming help I hope

  • From: "John Miller" <n1umj@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Thu, 18 Oct 2007 08:23:28 -0400

This is his instructions followed by what he calls a good outline to stick to but I thought I tried and it didn't work.



Write a C program that will calculate the gross pay of a set of employees.
The program should prompt the user to enter the number of hours each employee worked. When prompted, key in the hours shown below. The program determines the overtime hours (anything over 40 hours), the gross pay and then outputs a table in the following format. Column alignment, leading zeros in Clock number, and zero suppression in float fields is important. Use 1.5 as the overtime pay factor.
--------------------------------------------------------------------------
Clock#  Wage  Hours   OT   Gross
--------------------------------------------------------------------------
098401 10.60 51.0 11.0 598.90
526488  9.75 42.5  2.5 426.56
765349 10.50 37.0  0.0 388.50
034645 12.25 45.0  5.0 581.88
127615  8.35  0.0  0.0   0.00


Use the following information to initialize your data.

 98401 10.60
526488  9.75
765349 10.50
 34645 12.25
127615  8.35
You should implement this program using one array for clock number, one array for wage rate, etc.
Limit your use of global variables
Learn how to pass parameters
Create a separate module for each function. For instance, you might have a module (a function in C terms) for obtaining the hours from the user, another module for calculating overtime hours, another for calculating gross pay and another module for producing the output.



use global variables (except IF you want to specify a file pointer). I want you to learn how to pass arguments and work with parameters. The homework is due two weeks from this Friday. It will be significantly bigger than your previous homeworks. Take your time and do it right. I expect there will be questions/problems doing this, so don't hesitate to send questions over the class mailer. There are NO dumb questions!
Don't forget to put in function headers for each function

Samples to help you with homework 5
Passing array element values to functions
#include float GetClockedHours ( unsigned long int ClockNumber ) { float ClockedHours; printf ( "Enter hours worked for clock %6lu: ", ClockNumber ); scanf ( "%f", &ClockedHours ); return ( ClockedHours ); } /* Add other functions */ main() { /* Declare variables */ float ClockedHours[5]; /* hours worked array */ /* clock number array */ unsigned long int ClockNumber[5] = { 98401, 526488, 765349, 34645, 127615 }; unsigned int EntryNumber; /* index variable */ float GrosslyPaid[5]; /* gross pay array */ unsigned int NumberOfEntries = 5; /* number of entries */ float OvertimeHours[5]; /* overtime hrs array */ /* wage rates array */ float WageRate[5] = { 10.60, 9.75, 10.50, 12.25, 8.35 }; /* Prompt for input values from the screen */ /* calculate overtime hours and gross pay */ for ( EntryNumber = 0; EntryNumber < NumberOfEntries; EntryNumber++ ) { ClockedHours[EntryNumber] = GetClockedHours ( ClockNumber[EntryNumber] ); OvertimeHours[EntryNumber] = CalcOvertimeHours ( ClockedHours[EntryNumber] ); GrosslyPaid[EntryNumber] = CalcGrossPay ( ClockedHours[EntryNumber], OvertimeHours[EntryNumber], WageRate[EntryNumber] ); } /* print out employee information to the screen */ DisplayInfo ( ClockNumber, WageRate, ClockedHours, OvertimeHours, GrosslyPaid, NumberOfEntries ); }


Passing entire arrays to functions #include #define NUM_EMPL 5 #define OVERTIME_RATE 1.5 #define STD_WORK_WEEK 40 /************************************************************************/ /* Function: Get_input */ /* */ /* Purpose: Obtains input from user, the number of hours worked per */ /* employee and stores the results in an array that is */ /* passed back to the calling program by reference. */ /* */ /* Parameters: emp_num - Array of employee clock numbers. */ /* hrs - Array of number of hours worked by an employee*/ /* */ /* Returns: Nothing, since 'emp_num' & 'hrs' arrays are passed by */ /* reference. */ /************************************************************************/ void Get_input (long emp_num[], float emp_hrs[]) { /*Local Variable Declaration */ int count; /* Variable used in loop counter */ /* Gets number of employee hours and stores them in an array. */ for (count = 0; count < NUM_EMPL; ++count) { printf("Enter the numbers of hours worked by employee # %06li: ", emp_num[count]); scanf ("%f", &emp_hrs[count]); } printf("\\n\\n"); } /* Add other functions */ main() { /*Variable Declaration */ long clock_number[NUM_EMPL]={98401,526488,765349,34645,127615}; float gross[NUM_EMPL]; float hours[NUM_EMPL]; float wage_rate[NUM_EMPL]={10.60,9.75,10.50,12.25,8.35}; /* Function call to get input from user. */ Get_input (clock_number, hours); /* Function call to calculate gross pay. */ Gross_pay_calc (wage_rate, hours, gross); /* Function call to output results to the screen in table format. */ Output_results_screen (clock_number, wage_rate, hours, gross); /* Function call to output results to file (hw5.txt) in table format.*/ Output_results_file (clock_number, wage_rate, hours, gross); }

General Structure with headers /*______________________________________________________________________*/ /* */ /* HOMEWORK: #5 Functions (hmwork5.c) */ /* */ /* Name: JOE STUDENT */ /* */ /* Class: C Programming, Fall 1996 */ /* */ /* Date: today */ /* */ /* Description: This program prompts the user for the number of hours */ /* worked for each employee. It then calculates gross pay */ /* including overtime and displays the results in table */ /* format. Functions are used throughout this program, */ /* making the program modular. */ /*______________________________________________________________________*/ #include #define NUM_EMPL 5 #define OVERTIME_RATE 1.5 #define STD_WORK_WEEK 40 /************************************************************************/ /* Function: Get_input */ /* */ /* Purpose: Obtains input from user, the number of hours worked per */ /* employee and stores the results in an array that is */ /* passed back to the calling program by reference. */ /* */ /* Parameters: emp_num - Array of employee clock numbers. */ /* hrs - Array of number of hours worked by an employee*/ /* */ /* Returns: Nothing, since 'emp_num' & 'hrs' arrays are passed by */ /* reference. */ /************************************************************************/ void Get_input (long emp_num[], float emp_hrs[]) { } /************************************************************************/ /* Function: Overtime_hours */ /* */ /* Purpose: Calculates the number of overtime hours given the number*/ /* of hours worked. Overtime hours are the number of hours */ /* worked after 40 hrs. */ /* */ /* Parameters: hrs- Number of hours worked. */ /* */ /* Returns: The number of overtime hours. */ /************************************************************************/ float OT_hours (float hrs) { } /************************************************************************/ /* Function: Gross_pay */ /* */ /* Purpose: Calculates gross pay including overtime given the number*/ /* of hours an employee works and wage rate. */ /* */ /* Parameters: hrs_wkd - Array of Number of hours worked by employees*/ /* empl_wage - Array of Hourly rates of employees. */ /* gr_pay - Array of gross pay amounts for employees. */ /* */ /* Returns: Nothing,since gross pay (gr_pay)is returned by reference*/ /* overtime hours are determined by a call to function, */ /* OT_hours. */ /************************************************************************/ void Gross_pay_calc (float empl_wage[], float hrs_wkd[], float gr_pay[]) { } /************************************************************************/ /* Function: Output_results_screen */ /* */ /* Purpose: Outputs to screen in a table format the following: */ /* - Employee clock number */ /* - Wage rate for an employee. */ /* - Total hours worked by employee. */ /* - Overtime Hours. */ /* - Gross Pay. */ /* */ /* Parameters: clk_num_s - Array of employee clock numbers. */ /* wage_s - Array of hourly rates for each employee. */ /* hrs_s - Array of number of hours worked per employee*/ /* gross_pay - Array of gross pay for employees. */ /* */ /* Returns: Nothing. */ /************************************************************************/ void Output_results_screen (long clk_num_s[],float wage_s[], float hrs_s[], float gross_pay[]) { } /************************************************************************/ /* Function: Output_results_file */ /* */ /* Purpose: Outputs in a table format the following: */ /* - Employee clock number */ /* - Wage rate for an employee. */ /* - Total hours worked by employee. */ /* - Overtime hours. */ /* - Gross Pay. */ /* */ /* Parameters: clk_num_f- Array of employee clock numbers. */ /* wage_f - Array of hourly rates for each employee. */ /* hrs_f - Array of number of hours worked per employee.*/ /* g_pay - Array of gross pay for employees. */ /* */ /* Returns: Nothing. */ /************************************************************************/ void Output_results (long clk_num_f[],float wage_f[], float g_pay[], float hrs_f[]) { } /*/////////////////////////////////////////////////////////*/ main() { /*Variable Declaration */ long clock_number[NUM_EMPL]={98401,526488,765349,34645,127615}; float gross[NUM_EMPL]; float hours[NUM_EMPL]; float wage_rate[NUM_EMPL]={10.60,9.75,10.50,12.25,8.35}; /* Function call to get input from user. */ Get_input (clock_number, hours); /* Function call to calculate gross pay. */ Gross_pay_calc (wage_rate, hours, gross); /* Function call to output results to the screen in table format. */ Output_results_screen (clock_number, wage_rate, hours, gross); /* Function call to output results to file (hw5.txt) in table format.*/ Output_results_file (clock_number, wage_rate, hours, gross); }


----- Original Message ----- From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, October 18, 2007 2:00 AM
Subject: Re: a little C programming help I hope


Sorry John,

That information doesn't really help us. Is it possible to post the actual question?

 Martin


----- Original Message ----- From: "John Miller" <n1umj@xxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, October 18, 2007 1:55 AM
Subject: Re: a little C programming help I hope


I'm so lost I think it's not even funny. It looks like we're supposed to only be prompted for hours at this point, I don't know, I really don't know what I'm doing here. Looking at what he says in the class notes it looks like all we should be prompted for is hours and the other numbers are in the program. It looks kind of different from here too but I'd have more faith in this one than what's in the class notes 90% of the time. I thought I got this, apparently I don't. I don't really have time to work on it at all tonight unless I stay up all night which this class isn't worth. I'm just so confused now. I guess that's what I get for thinking I had it close. ----- Original Message ----- From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, October 17, 2007 3:50 PM
Subject: Re: a little C programming help I hope


Hmmm,

There are a number of problems here John. Firstly, you have to declare a prototype for each function before the start of main. Then in main, you declare an instance of the function you want to use so you can call it. Finally, and it can be after the end of the main function, you need to define the actual body of the function.

So, just thinking about inputting the clock numbers, your program might look like:

#include <stdio.h>

/* function prototypes */
int getClockNumber (int n);
void printArray(int* array);

int main (void)
{
 int clock[5]; /* clock numbers */
 int i;

/* Prompt for clock numbers */
for (i = 0; i < 5; ++i)
{ /*begin for loop*/
 /* call getClockNumber to return one clock number
 clock[i] = getClockNumber (i);
} /* end of for loop */

/* check the information has reached the array */
printArray (clock);
return 0;
} /* end of main */

/* define functions */
int getClockNumber (int n)
{
 int value;

 printf("input clock number %d: ", n+1);
 scanf_s("%3d", &value);
 return value;
}


void printArray (int* array)
{
 int i;

 for(i = 0; i < 5; i++)
 {
   printf("\n%d", array[i]);
 }
}

Obviously you will need to write more functions to input the rest of the information and print out the results.

This approach still leaves a certain amount of looping structure in the main function, but I don't know whether your teacher wants you to put the looping in the "get" functions so that main is just a straight list of function calls.

 hth

Martin

----- Original Message ----- From: "John Miller" <n1umj@xxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Wednesday, October 17, 2007 3:52 PM
Subject: a little C programming help I hope


Hi hope this is just a little help this time. I've been working on this long enough this morning, and last night and before I finish and find out I screwed up royally, I'm wondering if some one can look this over and let me know how I've done so far, what I might want to do different and since I am getting stuck a little, maybe a little advice on how to do the rest of it though, if I am very close, or good here I think I know where I'm going with the rest of it. this is just what I get from the class notes and I'm getting to the point where I'm not comfortable with what I am doing though I'm about 90% sure I started off right.
Thanks,

(begin code)

#include <stdio.h>

int main (void)

{

int clock[5];

/* clock number */

float gross[5];

/* gross pay */

float hours[5];

/* hours worked */

float overtime;

float wage[5];

/* hourly wage */

int i;

/* Prompt for employee information */

for (i = 0; i < 5; ++i)

{ /*begin for loop*/

#include float GetClockedHours ( unsigned long int ClockNumber )

{ /* begin clocked hours function */

float ClockedHours;

printf ( "Enter hours worked for clock %6lu: ", ClockNumber );

return ( clocked hours );

}


printf("Enter number of hours worked: ");

scanf_s("%f", &hours[i]);

void calc_gross_pay1 (float wage[], float hours[], float ot_pay[],

float gross_pay[], int emp_count)

{ int i; /* array index */

for (i = 0; i <EMP_COUNT; ++i)

{

/* test to see any overtime was worked */

if (hours[i]> 40)

/* calculate gross pay */

gross_pay[i] = 40 * wage[i] + ot_pay[i];

else gross_pay[i] = wage[i] * hours[i];

}



GrosslyPaid[5]; /* gross pay array */ unsigned int NumberOfEntries = 5; /* number of entries */ float OvertimeHours[5]; /* overtime hrs array */ /* wage rates array */ float WageRate[5] = { 10.60, 9.75, 10.50, 12.25, 8.35 }; /* Prompt for input values from the screen */ /* calculate overtime hours and gross pay */ for ( EntryNumber = 0; EntryNumber < NumberOfEntries; EntryNumber++ ) { ClockedHours[EntryNumber] = GetClockedHours ( ClockNumber[EntryNumber] ); OvertimeHours[EntryNumber] = CalcOvertimeHours ( ClockedHours[EntryNumber] );

GrosslyPaid[EntryNumber] = CalcGrossPay ( ClockedHours[EntryNumber], OvertimeHours[EntryNumber], WageRate[EntryNumber] );

}

} /*end first for*/

/* calculate gross pay */

/* print out employee information to the screen */

for (i = 0; i < 5; ++i)

{ /*start printf loop*/

printf ("\t----------------------------------------------------------\n");

printf ("\tClock#        Wage        Hours        Gross\n");

printf ("\t----------------------------------------------------------\n");

printf ("\t%06i %5.2f %5.1f %7.2f\n",clock[i],wage[i],hours[i],gross[i]); printf("This employee worked %f hours with a per-hour basis of %f.\n", hours[i], wage[i]);

printf(" gross pay is %f.\n", gross[i]);

} /* end printf loop*/


return 0;

}

(end code)







John Miller N1UMJ
AIM and yahoo messenger: N1UMJ Skype: n1umjjohn
home page:
http://home.comcast.net/~n1umj/wsb/html/view.cgi-home.html-.html
myspace: http://www.myspace.com/n1umj
__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind



__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: