# Finding average of array contents issue

Page 1 of 1

## 3 Replies - 1181 Views - Last Post: 06 December 2009 - 08:55 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=143857&amp;s=9171b9816a0e9a8cb7eb0677fab285e9&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aebaus

Reputation: 0
• Posts: 17
• Joined: 15-November 09

# Finding average of array contents issue

Posted 06 December 2009 - 08:08 PM

I am attempting to write a program using an array that is suppose to display the total number of items input at the end and then the average of them as well. Based on the code I have it seems like the average is being populated with the last number in the array instead of the actual contents. I am not sure where the problem is, from what I have found in my text book I think I have the array and the average setup but they are not working properly.

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

using namespace std;

int main()
{

const int	MAXSIZE = 5;				//Array maximum size

int			pos_num[MAXSIZE];	//Array to store positive numbers
int			num,				//Array subscript
sum = 0;			//Sum of the array numbers

double		pos_avg;			   //Average of numbers in the array

cout	<< setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);

cout	<< endl;
cout	<<"Please input up to " << MAXSIZE << " positive numbers, use 0 to indicate that you are done."
<< endl << endl;

for (num = 0; num < MAXSIZE; ++num)
{
cout	<< endl << endl;
cout	<< "Please input  postive number " << num + 1 << ": ";
cin		>> pos_num[MAXSIZE];
}

for (num = 0; num < MAXSIZE; ++num)
sum += pos_num[MAXSIZE];

pos_avg = double(sum) / MAXSIZE;
cout	<< endl << endl;
cout	<< "There are " << num << " postive numbers in the array. ";
cout	<< "The average of the numbers in the array is " << pos_avg << endl;

//system("pause");

return 0;

```

Is This A Good Question/Topic? 0

## Replies To: Finding average of array contents issue

### #2 Kimyoufox

Reputation: 4
• Posts: 23
• Joined: 29-November 09

## Re: Finding average of array contents issue

Posted 06 December 2009 - 08:17 PM

change
```for (num = 0; num < MAXSIZE; ++num)
sum += pos_num[MAXSIZE];

```

to

```for(num=0; num < MAXSIZE; ++num)
sum += pos_num[num];

```

and
```for (num = 0; num < MAXSIZE; ++num)
{
cout	<< endl << endl;
cout	<< "Please input  postive number " << num + 1 << ": ";
cin		>> pos_num[MAXSIZE];
}

```

to
```for (num = 0; num < MAXSIZE; ++num)
{
cout	<< endl << endl;
cout	<< "Please input  postive number " << num + 1 << ": ";
cin		>> pos_num[num];
}

```

### #3 aebaus

Reputation: 0
• Posts: 17
• Joined: 15-November 09

## Re: Finding average of array contents issue

Posted 06 December 2009 - 08:26 PM

Thank you for pointing that out. Now that you have pointed that out it makes perfect sense, I was looking at it seeing the brackets and thinking of the array variable that I initialized at the begging of the program.

### #4 aebaus

Reputation: 0
• Posts: 17
• Joined: 15-November 09

## Re: Finding average of array contents issue

Posted 06 December 2009 - 08:55 PM

In order to indicate the end of my input by entering a 0, would I need to setup a character string or can I do while statement similar to this below in order to end the program and run my last two operations?

```while (pos_num[num] != '\0')
++pos_num[num]

```