Average through Arrays & File I/O

Finding the average of a range of numbers in an array after reading fr

Page 1 of 1

4 Replies - 788 Views - Last Post: 20 October 2009 - 01:01 AM Rate Topic: -----

#1 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Average through Arrays & File I/O

Post icon  Posted 19 October 2009 - 09:50 PM

Hello, all ;)

So, here's my problem: I have a file that I must read numbers from, then request the user give an index (Start number, end number), and perform some functions on that range of numbers. The file itself is fairly large - about 5000 random numbers from 0 - 100. It may be easier to grasp what I'm after by seeing the code I have so far, which just reads back the start value and end value. Apologies for the lack of commenting...

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
const int maxCount = 5000;

int _tmain(int argc, _TCHAR* argv[])
{int data[maxCount];
 int i, k, indexStart, indexEnd;
	cout << "Index Start: ";
	cin >> indexStart;
	cout << "Index End: ";
	cin >> indexEnd;
  ifstream dataFile;
  dataFile.open("NumberList.txt");
   i = 0;
	 while(!dataFile.eof() && i < maxCount)
	   {dataFile >> k;
		data[i] = k;
		i++;
	   }

 cout << data[indexStart] << endl;
 cout << data[indexEnd] << endl;
	return 0;
}



So, there it is. I've set up an array with size 5000, read the numbers from the file into it, and then print the start and end values given by the user. That was the easier part. My issue now is trying to find the average of a range of numbers. Say, I input 0 and 100, I'll need it to find the average of all the numbers in spots 0 - 100 in the array. I'm not quite certain where to start.

Any ideas? I'm thinking I need another loop, and maybe another array.

Is This A Good Question/Topic? 0
  • +

Replies To: Average through Arrays & File I/O

#2 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Average through Arrays & File I/O

Posted 20 October 2009 - 12:23 AM

You can make a variable sum where you will add all the number' between the the range,then you will divide it by the total number of digits.Here is a snippet i am giving you:

for(i=0;i<100;i++)
		sum+=i;
average=sum/100;


This post has been edited by aaa111: 20 October 2009 - 12:24 AM

Was This Post Helpful? 1
  • +
  • -

#3 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: Average through Arrays & File I/O

Posted 20 October 2009 - 12:49 AM

for(i=0;i<100;i++)
		sum+=i;
average=sum/100;



I can see what you're getting at, but I think you *may* have misunderstood me.

The file is filled with about 5000 random numbers anywhere from 0 to 100. The program reads this file, then places each of the values into the array. It then prompts the user to enter a start position in the array, and an end. It must then work on all the values in between the start index and end index.

For instance:
Start Index: 0
End Index: 100
First value is 71
Last value is 62
Average value is 51.5248

As you can see, there's different values in between 71 and 62, and the range can be variable due to user input.

Sorry if you did read it correctly originally, and it's just me who misunderstood you.

This post has been edited by Chaosnub: 20 October 2009 - 12:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Average through Arrays & File I/O

Posted 20 October 2009 - 12:54 AM

Well,the things remains the same with some minor modification:
for(i=indexStart;i<indexEnd;i++)
		sum+=data[i];
average=sum/indexEnd;



Hope,i have understood u this time.

This post has been edited by aaa111: 20 October 2009 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 Chaosnub  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 19-October 09

Re: Average through Arrays & File I/O

Posted 20 October 2009 - 01:01 AM

View Postaaa111, on 19 Oct, 2009 - 11:54 PM, said:

Well,the things remains the same with some minor modification:
for(i=indexStart;i<indexEnd;i++)
		sum+=data[i];
average=sum/indexEnd;


Ah, I see now.

I modified it to about like that.

sum+=data[i]; //was the only thing I didn't manage to get.

Thanks, you definitely helped me understand!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1