1 Replies - 385 Views - Last Post: 12 November 2012 - 08:15 AM Rate Topic: -----

#1 timlacee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-November 12

Having trouble figuring out how to SUM the contents of an array

Posted 12 November 2012 - 08:14 AM

I've been trying to figure this out for a few hours and now I'm stuck. I can't figure out for the life of me how to SUM the contents of my array. I used the counter variable to set the size of it rather than using a fixed size. Maybe that's where I've messed up?...

// Ch10Intermediate19.cpp : Defines the entry point for the console application.
//program to calculate employees gross pay as well as display the total gross pay for the payroll manager to review.
//overtime included for hours over 40

#include "stdafx.h"
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
void EmployeeGrossPay(double a, double B)/>/>;
double TotalGrossPay(double a, double B)/>/>;

int _tmain(int argc, _TCHAR* argv[])
{
	//define variables
	double HoursWorked = 0.0;
	double TotalHoursWorked = 0.0;
	double EmployeeGrossPay = 0.0;
	double TotalGrossPay = 0.0;
	double rate = 0.0;
	double Overtime = 0.0;
	char again = 'y';
	int counter = 0;
	//define array constant variable for the number of items in the array
	const int MaxEmployees = counter;

	//define array for employees pay
	int EmployeeGrossPayList[100] = {MaxEmployees};

	//Calculate Employee Gross pay
	while (again == 'y' || again == 'Y' && MaxEmployees <= counter, counter++)
	{
		//get pay input
	cout << "How many hours did the employee work?:  ";
	cin >> HoursWorked;
	cout << endl << endl;
	cout << "What is the employees pay rate?:  ";
	cin >> rate;
	cout << endl << endl;

	if (HoursWorked <= 40)
	{
		EmployeeGrossPay = HoursWorked * rate;
	}
	else
	{
		Overtime = (HoursWorked - 40) * (rate * 1.5);
		EmployeeGrossPay = HoursWorked + Overtime;
	}//end if

	//load the EmployeeGrossPay[] array
	int NumOfEmployees = 0;
	EmployeeGrossPayList[NumOfEmployees] = EmployeeGrossPay;

	cout << "Keep going?  ";
	cin >> again;
	cout << endl;

	//advance counter
	counter += 1;

	}//end while

	

	cout << "Array Contents: " << EmployeeGrossPayList[MaxEmployees] << endl;
	
	cout << "total Employee hours worked = " << TotalHoursWorked << endl;

	cout << "Total Employee Gross Pay =  " << TotalGrossPay << endl;

	system("pause");
	
	return 0;
}



Any advice would be appreciated!

Sorry I'm also supposed to put this into functions but I wanted to make sure I can actually get the code working first before I start messing with passing values etc...

Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble figuring out how to SUM the contents of an array

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,939
  • Joined: 02-June 10

Re: Having trouble figuring out how to SUM the contents of an array

Posted 12 November 2012 - 08:15 AM

for loop

Loop through the array, adding each element to a Total variable
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1