# find average of an array

• (2 Pages)
• 1
• 2

## 20 Replies - 5778 Views - Last Post: 06 August 2009 - 06:07 PMRate 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=118633&amp;s=4b6bf9d47da834f41b11d2afc1aa15b8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

# find average of an array

Posted 05 August 2009 - 01:12 PM

Ok, I'm working on this program and i need to find the average of the array np "netpay" This is what i have so far but it says average np is 0.

```#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//function prototypes
int readalldata(char[][15], char[][15], long int[], int[], float[], const int);
void findregularhours(int[], int[], int);
void findregularpay(int[], float[], float[], int);
void findovertimehours(int[], int[], int);
void findovertimepay(float[], float[], int[], int);
void findgp(float[], float[], float[], int);
void findtaxrate(float[], float[], int);
void findtaxamount(float[], float[], float[], int);
void findnetpay(float[], float[], float[], int);
void printalldata(char[][15], char[][15], long int[], int[], float[], float[], float[], float[], float[], int);
//end prototypes

int main (){

const int maxNum=100;
char firstname[10][15];
char lastname[10][15];
long int ei[maxNum];
float gp[maxNum],np[maxNum],otp[maxNum],tax[maxNum], hr[maxNum], tr[maxNum], rp[maxNum];
int hw[maxNum], oth[maxNum], rh[maxNum];
int n;
float sum = 0;
float average = 0;
float netpay[maxNum] = {np[maxNum]};

//function calls

n=readalldata(firstname, lastname, ei, hw, hr, maxNum);
findregularhours(hw, rh, n);
findregularpay(rh,rp, hr, n);
findovertimehours(hw, oth, n);
findovertimepay(otp, hr, oth, n);
findgp(rp, otp, gp, n);
findtaxrate(gp, tr, n);
findtaxamount(tr, gp, tax, n);
findnetpay(np, gp, tax, n);
printalldata(firstname, lastname, ei, hw, hr, gp, tr, tax, np, n);

for (int i=0; i<n; i++)
sum+=netpay[i];
average = sum/np[maxNum];
cout<<"Average Netpay:"<<average<<endl;
//end calls

system ("pause");
return 0;
}//MAIN

//function definitions

int readalldata( char firstname[][15], char lastname[][15], long int ei[], int hw[], float hr[], int n){
ifstream fin("employee.txt");
n=0;
while(fin>>firstname[n]>>lastname[n]>>ei[n]>>hw[n]>>hr[n]) n++;//WHILE
fin.close();
return n;

void findregularhours(int hw[], int rh[], int n){
for (int i=0; i<n; i++){
if(hw[i]>40)rh[i]=40;
else rh[i]=hw[i];
}//FOR
}//findregularhours

void findregularpay(int rh[], float rp[], float hr[], int n){
for (int i=0; i<n; i++){
rp[i]=rh[i]*hr[i];
}//FOR
}//findregularpay

void findovertimehours(int hw[], int oth[], int n){
for (int i=0; i<n; i++){
if(hw[i] > 40) oth[i] = hw[i] - 40;
else oth[i] = 0;
}//FOR
}//end oth function

void findovertimepay(float otp[], float hr[], int oth[], int n){
for (int i=0; i<n; i++){
otp[i] = oth[i] * hr[i] * 1.5;
}//FOR
}//end ot pay function

void findgp(float rp[], float otp[], float gp[], int n){
for (int i=0; i<n; i++){
gp[i] = rp[i] + otp[i];
}//FOR
}//end gp function

void findtaxrate (float gp[], float tr[], int n){
for (int i=0; i<n; i++){
if(gp[i] > 1000) tr[i]=0.30;
else if(gp[i] > 800) tr[i]=0.20;
else if (gp[i] > 500)tr[i] = 0.10;
else tr[i] = 0.0;
}//FOR
}//end taxrate function

void findtaxamount(float tr[], float gp[], float tax[], int n){
for (int i=0; i<n; i++){
tax[i] = gp[i] * tr[i];
}//FOR
}//end taxamount function

void findnetpay (float gp[], float np[], float tax[], int n){
for (int i=0; i<n; i++){
np[i] = gp[i] - tax[i];
}//FOR
}//end netpay function

void printalldata(char firstname[][15], char lastname[][15], long int ei[], int hw[], float hr[], float gp[], float np[], float otp[], float tax[], int n) {
cout<<setw(1)<<"First Name"<<setw(15)<<"Last Name"<<"\t"<<"Employee ID"<<"\t"<<"HW"<<"\t"<<"HR"<<"\t"<<"GP"<<"\t"<<"NP"<<"\t"<<"OTP"<<"\t"<<"Taxes"<<"\t"<<"\t"<<endl;
cout<<"\n"<<endl;
for (int i=0; i<n; i++){
cout<<setw(10)<<firstname[i]<<setw(15)<<lastname[i]<<setw(15)<<ei[i]<<"\t"<<hw[i]<<"\t"<<hr[i]<<"\t"<<gp[i]<<"\t"<<np[i]<<"\t"<<otp[i]<<"\t"<<tax[i]<<endl;
}//FOR
}//end printall function

```

This post has been edited by L-uke: 06 August 2009 - 09:31 AM

Is This A Good Question/Topic? 0

## Replies To: find average of an array

### #2 Ancient Dragon

Reputation: 82
• Posts: 679
• Joined: 19-July 09

## Re: find average of an array

Posted 05 August 2009 - 01:15 PM

Please edit your post and add code tags so that we can read it.

### #3 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: find average of an array

Posted 05 August 2009 - 02:16 PM

As mentioned before, edit your post to use CODE tags.

So your program doesn't do what you want it to do. It's called a bug. What you need to learn to do is to isolate and fix bugs. The first step is to isolate the problem.

We know that the correct value of average isn't being displayed. Let's look at the line that deals with the display of average: cout<<"Average Netpay:"<<average<<endl; It doesn't look like there's anything wrong with the line that deals with output, so perhaps for some reason, average is 0. Ok, where is average being set to something other than 0?

average = sum/np[maxNum];. This is the previous line. average is a calculated value. That means if average is 0, sum must also be 0. Do you follow? Now we see how sum is being assigned to.

for (int i=0; i<n; i++) sum+=netpay; If sum is an aggregate of all the values in the array netpay, and if sum is 0, then it follows that all the values in netpay are 0, for whatever reason.

I've shown you my method of thinking up to this point. Now the next obvious question is: why are the values in netpay 0? To answer that, indicate the line of code that makes all values of netpays [i]not equal
to 0. Are you calculating and assigning a value? Where is the code that modifies netpay?

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: find average of an array

Posted 05 August 2009 - 04:05 PM

im not going to try to go through all of your code, but
to find the average of an array do something like this
```int myArray[size];
int total = 0;
for(int i=0; i<size; i++)
{
total += myArray[i];
}
total/=size;

```

This post has been edited by ImaSexy: 05 August 2009 - 04:06 PM

### #5 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 09:35 AM

Oler1s, on 5 Aug, 2009 - 01:16 PM, said:

As mentioned before, edit your post to use CODE tags.

So your program doesn't do what you want it to do. It's called a bug. What you need to learn to do is to isolate and fix bugs. The first step is to isolate the problem.

We know that the correct value of average isn't being displayed. Let's look at the line that deals with the display of average: cout<<"Average Netpay:"<<average<<endl; It doesn't look like there's anything wrong with the line that deals with output, so perhaps for some reason, average is 0. Ok, where is average being set to something other than 0?

average = sum/np[maxNum];. This is the previous line. average is a calculated value. That means if average is 0, sum must also be 0. Do you follow? Now we see how sum is being assigned to.

for (int i=0; i<n; i++) sum+=netpay; If sum is an aggregate of all the values in the array netpay, and if sum is 0, then it follows that all the values in netpay are 0, for whatever reason.

I've shown you my method of thinking up to this point. Now the next obvious question is: why are the values in netpay 0? To answer that, indicate the line of code that makes all values of netpays [i]not equal
to 0. Are you calculating and assigning a value? Where is the code that modifies netpay?

Ok, so I think the code that modifies netpay would be the function that calculates netpay? Or is it the

```float netpay[maxNum] = {np[maxNum]};

```

### #6 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: find average of an array

Posted 06 August 2009 - 09:40 AM

Why are you asking us? It's your code, right? I mean, you had to explicitly type out the code. So you believe a certain line of code does something specific. If you want to say netpay is modified, show us precisely the line of code and how that line of code of modifies netpay.

Or do you not know? I'm trying to get you to learn something here. You wrote the code, so do you understand what it does? Did you guess your way through code, or did you type it out line by line, with an expectation of what each line does?

### #7 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 10:09 AM

Oler1s, I am focusing on graphic/ web design. I took this course because I thought it would be good to understand a programing language, but I didn't think it would be so hard for me to understand. And to be honest I'm just trying to pass the course. I do not understand all of the code and how it works. I'm an artist and my brain dose not work this way. I made a poor choice enrolling in this class but I don't want it to be a waste of my money.

### #8 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 10:23 AM

Ok, I think that the problem is in the code below. I am getting a value for the sum but then I am trying to calculate average by dividing the sum value by itself or the np[maxNum] array.

I need to divide the sum by the number of netpays that are stored in the np[] array.

```	for (int i=0; i<n; i++)
sum+=netpay[i];
average = sum/np[maxNum];
cout<<"Average Netpay:"<<average<<endl;

```

### #9 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: find average of an array

Posted 06 August 2009 - 10:27 AM

Just a small suggestion for your future coding habits, try camelCasing you're variables and function prototypes, it makes it a little easier to read and on the eyes.

```//example

int KickTheDoorDown > int kickthedoordown

```

This post has been edited by IngeniousHax: 06 August 2009 - 10:28 AM

### #10 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: find average of an array

Posted 06 August 2009 - 10:31 AM

So it's not that I'm being unsympathetic to your situation, in that you find yourself in a course you aren't interested in. But we still don't hand out free answers.

Learning how to think like a programmer is a learned skill. That's why we say you can't just learn by reading your way through books. Being an artist does not prevent you from developing the thought process that benefits programmers. I think of such a statement as an excuse rather an explanation. Being a designer and a developer is not an either or choice.

But of course, you aren't interested in these ideas. As you noted, you don't understand your code. And you make it clear your real interest is to just get something working. I don't criticize your attitude; I don't think it's the right one as you can tackle your course if you set your mind to learning how to think like a programmer but it's a natural development.

However, our interest in seeing people learn to tackle problems. In that respect, the actual answer is a secondary concern. If we give out answers, and you learn nothing from the experience, the entire exchange was a waste of time. We don't do free work here. We educate. You might not care for the education. We do.

Sorry. Maybe someone else will take pity on your plight. If they believe you. Probably not though.

EDIT: "I need to divide the sum by the number of netpays that are stored in the np[] array."

Yes you do. Is there a variable or number in your code that indicates how many netpays are to be calculated? Hint: look at your for loop. How many netpays do you add? 1? 10? 15? A variable number? What variable?

This post has been edited by Oler1s: 06 August 2009 - 10:33 AM

### #11 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 10:49 AM

You are right and I apologize for the last post. I not looking for hand outs and I will keep trying to understand this.

### #12 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 12:52 PM

Oler1s, Thanks for telling me to buck up and do it myself. I figured it out and I am now getting a value for the average net pay. I still can't figure out why my numbers for gross pay won't work. I keep getting a long decimal.

Below is the function that I am using to calculate gross pay along with the function to calculate the regular pay. and I have tried tweaking it but I'm not having much luck. This same function calculated properly in a prior assignment. Do you see anything that I am missing.

I have other functions that are calculating the rp(regular pay) and otp(overtime pay). I know the overtime pay function is working because it calculates correctly in the program when I compile and run.

These are the three functions that are involved in calculating the gross pay, asides form the one calculating overtime pay but that one works properly.

Any way you could shove me in the right direction.

-All the best, luke

```void findregularhours(int hw[], int rh[], int n){
for (int i=0; i<n; i++){
if(hw[i]>40)rh[i]=40;
else rh[i]=hw[i];
}//FOR
}//findregularhours

void findregularpay(int rh[], float rp[], float hr[], int n){
for (int i=0; i<n; i++){
rp[i]=rh[i]*hr[i];
}//FOR
}//findregularpay

void findgp(float rp[], float otp[], float gp[], int n){
for (int i=0; i<n; i++){
gp[i] = rp[i] + otp[i];
}//FOR
}//end gp function

```

### #13 eker676

• Software Engineer

Reputation: 379
• Posts: 1,833
• Joined: 18-April 09

## Re: find average of an array

Posted 06 August 2009 - 12:56 PM

Are you sure the values are being stored properly and your not being stuck with garbage values?

You should debug the code and look at what values are being passed in and their correctness.

I would suggest passing in the gp array as a pointer like this

```void findgp(float rp[], float otp[], float* gp, int n)
{
for(int i = 0; i < n; ++i)
{
*gp = rp[i] + otp[i];
++gp;
}
}
```

This post has been edited by eker676: 06 August 2009 - 12:58 PM

### #14 L-uke

• New D.I.C Head

Reputation: 0
• Posts: 10
• Joined: 05-August 09

## Re: find average of an array

Posted 06 August 2009 - 01:11 PM

eker676, I just altered the function calls, prototypes and definitions to use a pointer for the gross pay. It changed the variables but they are still messed. I will keep trying.

-all the best, luke

### #15 Oler1s

• D.I.C Lover

Reputation: 1397
• Posts: 3,884
• Joined: 04-June 09

## Re: find average of an array

Posted 06 August 2009 - 03:23 PM

I mentioned earlier the need to isolate the bug. Do that again here. You aren't getting grosspay as expected. Isolate that issue first. Determine if findgp is at fault. You have a for loop calculating gp[i]. Why don't you throw an output statement in the for loop, and check the values of gp[i], rp[i], and otp[i]. Is everything as expected?