Tech Support banner

Status
Not open for further replies.
1 - 6 of 6 Posts

·
Registered
Joined
·
5 Posts
Discussion Starter #1
hi guys. im really having probs with arrays. Basically i open an input file like:

Code:
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:

Code:
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;
	}
 

·
Registered
Joined
·
4 Posts
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.
 

·
Registered
Joined
·
604 Posts
PHP:
#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.
 

·
TSF Team, Emeritus
Joined
·
2,367 Posts
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.
 
1 - 6 of 6 Posts
Status
Not open for further replies.
Top