Thread: Array issue?

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    3

    Post Array issue?

    In working on a homework assignment and pretty much have it completed. However, I am having an issue with my first entry every time i use the program. It always gives me a large number for OT and the rest of the outputs are correct. Any thoughts, thanks.


    Code:
    #include <stdio.h>
    
    
    #define STD_HOURS 40.0
    #define OT_HALF 2.0
    //#define NUM_EMP 5.0
    
    
    int main (void)
    {
    	
    /*	Declare Variables	*/
    	int NUM_EMP = 5;
    	int i = 0;				//Accumulator for Loop;
    	int a = 0;				//Accumulator for Arrays;
    	int clock_num[4];		//Employee Clock Number;
    	float emp_hours[4];		//Employee Hours;
    	float emp_wage[4];		//Employee Wage Rate;
    	float emp_gross[4];		//Employee Total Gross;
    	float emp_otime[4];		//Employee Over Time;
    	float ot_acc[4];		//Accumulator for OT;
    	float ot_wage[4];		//Accumulator for OT Wage;
    
    
    /*	Main Program	*/
    	
    	printf("***Gross Pay Calculator***\n");
    	printf("\n");
    
    
    
    
    /*	Begin Employee Input */	
    
    
    	for (i = 0; i < NUM_EMP; i++)
    	{
    		for (a = 0; a < NUM_EMP; a++)
    		{
    		printf("Enter Employee's Clock #:\n");
    		scanf("%i", &clock_num[a]);
    		printf("Enter hourly wage:\n");
    		scanf("%f", &emp_wage[a]);
    		printf("Enter number of hours worked:\n");
    		scanf("%f", &emp_hours[a]);
    		printf("\n");
    			if (emp_hours[a] > STD_HOURS)
    			{
    			ot_wage[a] = emp_wage[a];
    			ot_wage[a] = ((ot_wage[a] / OT_HALF) + emp_wage[a]);
    			ot_acc[a] = emp_hours[a] - STD_HOURS;
    			emp_otime[a] = ot_acc[a] * ot_wage[a];
    			}
    		emp_gross[a] = emp_wage[a] * emp_hours[a] + emp_otime[a];
    		}
    	
    /*	Display Results	*/
    
    
    	printf("-----------------------------------------\n");
    	printf("Clock #	Wage	Hours	OT	Gross\n");
    	printf("-----------------------------------------\n");
    		
    		for (a = 0; a < NUM_EMP; a++)
    		{
    		printf("%06i	%5.2f	%5.1f	%.1f	%.2f\n", clock_num[a], emp_wage[a], emp_hours[a], ot_acc[a], emp_gross[a]);
    		printf("\n");
    		}
    	}
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    One of your problems is that your accessing your arrays out of bounds. You created your arrays with a size of 4 yet you loop 5 times.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *need help in array issue*
    By RyanC in forum C Programming
    Replies: 1
    Last Post: 11-26-2015, 03:59 PM
  2. issue with point to array.
    By mgracecar in forum C Programming
    Replies: 4
    Last Post: 04-25-2012, 09:58 PM
  3. Array Issue.
    By mcertini in forum C Programming
    Replies: 7
    Last Post: 02-16-2011, 10:56 AM
  4. array of pointers issue
    By Brain Cell in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2005, 04:18 PM
  5. array issue
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 07-10-2003, 08:37 AM

Tags for this Thread