Jump to content

little confusion in C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
wwwildbill

wwwildbill

    Newbie

  • Members
  • Pip
  • 1 posts
Is there an easy way to rewrite this program without using any global variables???

#include <stdio.h>

/* Variables*/
	struct info
{
	char first[30];
	char last[30];
};
	struct info name[100];
	
	float hours[30];
	float rate[30];
	int employees,x;
	float gross_pay[10];
	float fed_tax[10];
	float net_pay[10];
	float overtime[10];
	float temp=0;


float overtime_pay(float hours[], float rate[])
{	
	temp = hours[x];
	overtime[x] = (temp-40)*rate[x]*1.5;
	printf("OT $%.2f\n\n",overtime[x]);
	return overtime[x];
}	

float calculate_tax(float gross_pay[])
{
		fed_tax[x] =.2*gross_pay[x] ;
		return fed_tax[x];
		}


main()
{


	


printf ("*****WELCOME TO THE PAYROLL CALCULATOR*****\n\n");


	for (x = 1; x <=1; ++x)
		{
			printf("Please enter # of employees(1-10): ");
			scanf("%i",&employees);
			if (employees > 10)
				{	printf ("Invalid Entry\n");
					--x;
				}
			if (employees < 1)
				{	printf ("Invalid Entry\n");
					--x;
				}
			
		}
for (x = 1; x <=employees; ++x)
	{
		printf ("\nEnter first and last name of employee%i: ",x);
		scanf("%s %s",name[x].first, name[x].last);
		printf("Enter pay rate for employee %i: ",x);
		scanf("%f", &rate[x]);
		printf("Enter hours worked for employee %i: ",x);
		scanf("%f", &hours[x]);
					if(hours[x] > 40)
						{overtime_pay(hours, rate);
						}
				
	}
	
for (x = 1; x <= employees; ++x)
		{	gross_pay[x]=hours[x]*rate[x]+overtime[x];
			printf("GROSS%i: %.2f\n",x, gross_pay[x]);
			calculate_tax(gross_pay);
			printf("TAX%i: %.2f\n",x, fed_tax[x]);
			net_pay[x]=gross_pay[x]-fed_tax[x];
			printf("NET%i: %.2f\n\n",x, net_pay[x]);
	}
}

Edited by WingedPanther, 13 February 2009 - 04:58 PM.
add code


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You can pass more parameters in your function calls. I'm not sure whether you would consider that "easy".
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
lucuis

lucuis

    Newbie

  • Members
  • PipPip
  • 21 posts
Sure you can. Don't know, if it would be easy, but not using global variables will make the program much clearer. For instance in function calculate_tax, if you pass all variables as parameters, you will avoid usage of global vars:
void calculate_tax(float fed_tax[], float gross_pay[], int x)
{
	fed_tax[x] =.2*gross_pay[x] ;	
}
Or even better:
void calculate_tax(float fed_tax[], float gross_pay[], int employees)
{
	int x;
	for (x = 1; x <= employees; ++x)
		fed_tax[x] =.2*gross_pay[x] ;	
}