Thread: how do i tell the compiler to check each number only once for occurences?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    13

    Question how do i tell the compiler to check each number only once for occurences?

    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

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    when you want to end a loop use

    break;

    also doing

    char filename[50] isnt such a good idea. A better way would be to

    Code:
    string filename;
    ifstream in_file;
    
    filename = filename.c_str();
    in_file.open(filename);
    Its "GOOD" code to fill the whole array first before using it. When defining an array, old stuff is in their and could cause a bad effect to your program.
    Last edited by D-Man; 11-11-2002 at 09:15 PM.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    13
    Thank you D-Man!!

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    14

    Talking

    No Problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. HELP!!!!emergency ~expert please help
    By unknowppl in forum C Programming
    Replies: 1
    Last Post: 08-19-2008, 07:35 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM