# C++ help with arrays..



## Cro (Oct 10, 2005)

hi guys. im really having probs with arrays. Basically i open an input file like:


```
ifstream infile;

infile.open("A:\\students.txt");
```
now the file contains different int numbers...like:

76,89,150,135,200,76,12,100,150,28,178,189,167,200,175,150,87,99,129,149,176,200,87,35,157,189

now what i need to do is to put all those in an array...and then determine the number of students having scroes in each of the following ranges:

0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200.

it should then display the results like:

------------COUNTS(the total number)
0-24 ------- 1
25-49-------- 2
50-74 --------0 
.. ----------- .
. ----------- .
.. ----------- .
.

please if someone could help me.... thanks!

ive tried some code, but im really stuck:,, this is what ive done:


```
int list[26];
	int z;
	
	infile >> list[26];
	while(infile)
	{
		///for(z = 0; z < 26; z++)
		//{
			
			if(list[z] > 0 && list[z] < 25)
			{
				list[z]++;
				sum = sum + list[z];
				
				
			}
		infile >> list[26];
		cout << sum;
	}
```


----------



## Cwwoods (Oct 16, 2005)

*Binary Search or Bubble Sort?*

Have you tried something like this?

Example of Binary Seach.....

int binarysearch (int array[], int size, int searchKey)

{

int low = 0, middle, high;

high = -1;

while (low <= high);

{

middle = (low + high) / 2;

if (searchKey == array [middle])
return middle;

else if (searchKey < array [middle])
high = middle -1;

else 
low = middle + 1;

}

return -1; //not found

}

Does this help? Once it finds the values in the array you should be able to sort them with some more code.


----------



## mgoldb2 (Dec 16, 2004)

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

using namespace std; 

int main() 
{ 
	int n[26];
	
	ifstream input;  
	input.open("input.dat"); 
	
	if(input.fail( ))
	{ 
       cout << "Cannot open file"<<endl; 
       return 1; 
    } 

	int x=0;
	
	while(!input.eof())
	{
		input>> n[x];
		x++;
	}

	input.close( );
	
	int y=25;
	int size=0;
	cout<<setw(3)<<"Range"<<setw(9)<<"count"<<endl<<endl;
	for(int a=0;a<8;a++)
	{
		int z=0;
		while(z<x)
		{
			if(n[z]<y && n[z]>y-25)
			{
				size++;
			}
			
			z++;
			
		}
		
		cout<<setw(3)<<y-25<<setw(2)<<"-"<<setw(3)<<y<<setw(3)<<size<<endl;
		size=0;
		y=y+25;
	}
	
			
	return 0;
}
```
This will work. basically what it does it save each value in the file into a array. then it searchers the array 8 times each time looking for a different one of the ranges you needed then displays how many time it found a number in that range.


----------



## Cro (Oct 10, 2005)

thanks for all the help  very much appreciated!


----------



## Andrew'smom (Feb 2, 2010)

Hi I am struggling with a similar problem...but it is 5 years later...anybody still reading this?
Thanks.
Andrew'smom


----------



## dm01 (Oct 10, 2006)

Generally, it is a good idea to create a new thread for a separate issue; especially if the original thread is more than a year old.

This thread is closed to further replies.

Thank you.


----------

