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

Page 1 of 1

## 1 Replies - 492 Views - Last Post: 12 November 2012 - 08:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=299820&amp;s=9ecdaaba3ddf7e81a5a9301bdc96193d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 timlacee

Reputation: 0
• 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

int NumOfEmployees = 0;
EmployeeGrossPayList[NumOfEmployees] = EmployeeGrossPay;

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

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;
}

```

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

• Freelance developer

Reputation: 6096
• Posts: 13,448
• 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