i'm hoping someone can help me with this. for this program i have to read in numbers from a file, put them in numerical order, and include the occurence of each.
my problem is that after it gives the occurence of a number it goes through again and gives the occurence of the remaining numbers, etc...
for example, it finds the number 7 three times, then it goes through again and finds it 2 times, then it goes through one last time and finds it one time........
i need it to find the number and then stop.
does anyone know how to do this??
any help is greatly appreciated!
Code:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
void puente();
char filename[50],instring[50];
int i=0, IntegerArray[50], num;
int j, temp;
void main()
{
ifstream x;
cout<<"Type the name of the file to read and then press the enter key. ";cin>>filename;cout<<endl;
x.open(filename);
if(x.fail())
{
cout<<"input file opening failed";
exit(1);
}
i = -1;
while (!x.eof())
{
i++;
x>>IntegerArray[i];cout<<setw(3)<<IntegerArray[i]<<endl;
}
num = i;
cout<<"These are the numbers in numerical order and how many times each occurs."<<endl;
for(i=0; i<(num-1); i++)
{
for(j=(i+1); j<num; j++)
{
if(IntegerArray[i]<IntegerArray[j])
{
//Switch the values
temp=IntegerArray[i];
IntegerArray[i]=IntegerArray[j];
IntegerArray[j]=temp;
}
}
}
puente();
}
void puente()
{
int counter=0;
for(j=0; j<=num; j++)
{
for(i=j; i<=num; i++)
{
if(IntegerArray[j] == IntegerArray[i])
{
counter++;
}
}
cout<<setw(3)<<IntegerArray[j]<<" "<<setw(3)<<counter<<'\t'<<endl;
counter=0;
}
}
/* This is the output
Type the name of the file to read and then press the enter key. intfile.txt
12
3
5
7
-56
32
-4
3
-2
0
5
16
8
-3
21
-12
3
7
-9
7
0
0
3
These are the numbers in numerical order and how many times each occurs.
32 1
21 1
16 1
12 1
8 1
7 3
7 2
7 1
5 2
5 1
3 4
3 3
3 2
0 3
0 2
0 1
-2 1
-3 1
-4 1
-9 1
-12 1
-56 1
3 1
Press any key to continue
*/
Code tags added by Hammer