3 Replies - 911 Views - Last Post: 06 December 2009 - 08:55 PM Rate Topic: -----

#1 aebaus  Icon User is offline

  • New D.I.C Head

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




Thanks for whatever help you guys give me.

Is This A Good Question/Topic? 0
  • +

Replies To: Finding average of array contents issue

#2 Kimyoufox  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • 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];
}


Was This Post Helpful? 1
  • +
  • -

#3 aebaus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#4 aebaus  Icon User is offline

  • New D.I.C Head

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



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1