C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 14th, 2009, 05:10 AM
ParadoxKing ParadoxKing is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 4 ParadoxKing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 20 m 7 sec
Reputation Power: 0
Counting Repeated Elements in an array (Solved)

I am attempting to write a program that takes input for an array from a file, then counts the number of times each element is repeated than outputs a single instance of each element and how many times it appears in ascending order. Vectors cannot be used.

This is my input file:
inScores.txt:
Code:
55
80
78
92
95
55
78
53
92
65
78
95
85
92
85
95
95


And this is what the output should look like:
Code:
    Element     No. of Instances
       53             1       
       55             2
       65             1
       78             3
       80             1
       85             2
       92             3
       95             4
    Press any key to continue . . .


I have tried 2 methods of coding this which have given me 2 separate results, neither of which are correct.

My first go around looked like this:
c++ Code:
Original - c++ Code
  1.     //Include Header Files
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.     //Declare Namespace
  6. using namespace std;
  7.  
  8. void insSort2(int *list, int length);
  9.  
  10. int main()
  11. {
  12.         //Declare Variables
  13.     ifstream inFile;
  14.     ofstream outFile;
  15.    
  16.     int count = 1;
  17.     int scores[100];
  18.     int last = scores[0];
  19.    
  20.     int x = 0;
  21.    
  22.         //Open input/output files
  23.     inFile.open("inScores.txt");
  24.     outFile.open("outScores.dat", ios::app);
  25.    
  26.    
  27.     for(;!inFile.eof();x++)
  28.     {
  29.         inFile >> scores[x];
  30.     }
  31.    
  32.     insSort2(scores, x);
  33.    
  34.     outFile << "Element     No. of Instances" << endl;
  35.     for(int z=1; z < x;z++)
  36.     {
  37.         if(scores[z] == last)
  38.             count++;
  39.        
  40.         else
  41.         { 
  42.             outFile << "   " << scores[z] << "             " << count << endl;
  43.             last = scores[z];
  44.             count = 1;
  45.         }
  46.    
  47.     }
  48.    
  49.    
  50.     system ("PAUSE");
  51.    
  52.     return 0;
  53.    
  54. }
  55.  
  56.  
  57. void insSort2(int *list, int length)
  58. {
  59.      for(int x = 1; x<length;x++)
  60.      {
  61.         for(int y = x-1; y>=0 && list[y+1]<list[y];y--)
  62.         {
  63.             register int temp = list[y+1];
  64.             list[y+1] = list[y];
  65.             list[y] = temp;
  66.         }
  67.     }
  68. }


The output for that looked like this:

Code:
    Element     No. of Instances
       55             1
       65             2
       78             1
       80             3
       85             1
       92             2
       95             3
    Press any key to continue . . .


Notice that not only are all the count numbers off but the single instance of 53 isn't even listed.

At first the only thing I saw that was wrong with the output was the fact that 53 was missing and the count for 95 was 3 instead of 4.

So I rewrote my code as thus:

c++ Code:
Original - c++ Code
  1.  
  2.     //Include Header Files
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6.     //Declare Namespace
  7. using namespace std;
  8.  
  9. void insSort2(int *list, int length);
  10.  
  11. int main()
  12. {
  13.         //Declare Variables
  14.     ifstream inFile;
  15.     ofstream outFile;
  16.    
  17.     int count = 1;
  18.     int scores[100];
  19.     int last = scores[0];
  20.    
  21.     int x = 0;
  22.    
  23.         //Open input/output files
  24.     inFile.open("inScores.txt");
  25.     outFile.open("outScores.dat", ios::app);
  26.    
  27.    
  28.     for(;!inFile.eof();x++)
  29.     {
  30.         inFile >> scores[x];
  31.     }
  32.    
  33.     insSort2(scores, x);
  34.    
  35.     outFile << "Element     No. of Instances" << endl;
  36.     for(int z=0; z < x;z++)
  37.     {
  38.         if(scores[z] == last)
  39.             count++;
  40.        
  41.         else if (scores[z] != last)
  42.         {   
  43.             if (scores[z] == scores[x - 1])
  44.             {
  45.                 count++;
  46.             }     
  47.             outFile << "   " << scores[z] << "             " << count << endl;
  48.             last = scores[z];
  49.             count = 1;
  50.         }
  51.    
  52.     }
  53.    
  54.     system ("PAUSE");
  55.    
  56.     return 0;
  57.    
  58. }


The output became:
Code:
Element     No. of Instances
   53             1
   55             1
   65             2
   78             1
   80             3
   85             1
   92             2
   95             4


As you can see, 53 is now present and the count for 95 is now correct, but everything else is still wrong. What am I doing wrong here?

I solved the problem, for those who are curious the correct code is:

c++ Code:
Original - c++ Code
  1.     //Include Header Files
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5.     //Declare Namespace
  6. using namespace std;
  7.  
  8. void insSort2(int *list, int length);
  9.  
  10. int main()
  11. {
  12.         //Declare Variables
  13.     ifstream inFile;
  14.     ofstream outFile;
  15.    
  16.     int count = 1;
  17.     int scores[100];
  18.    
  19.     int x = 0;
  20.    
  21.         //Open input/output files
  22.     inFile.open("inScores.txt");
  23.     outFile.open("outScores.dat");
  24.    
  25.    
  26.     for(;!inFile.eof();x++)
  27.     {
  28.         inFile >> scores[x];
  29.     }
  30.    
  31.     insSort2(scores, x);
  32.     int last = scores[0];
  33.    
  34.     outFile << "Element     No. of Instances" << endl;
  35.    
  36.     int z;
  37.    
  38.     for(z=1; z < x;z++)
  39.     {
  40.        
  41.         //cout << scores[z] << endl;
  42.         if(scores[z] == last)
  43.             count++;
  44.        
  45.         else
  46.         {       
  47.             outFile << "   " << scores[z-1] << "             " << count << endl;
  48.             last = scores[z];
  49.             count = 1;
  50.         }
  51.    
  52.     }
  53.     outFile << "   " << scores[z-1] << "             " << count << endl;
  54.    
  55.     return 0;
  56.    
  57. }
  58.  
  59. void insSort2(int *list, int length)
  60. {
  61.      for(int x = 1; x<length;x++)
  62.      {
  63.         for(int y = x-1; y>=0 && list[y+1]<list[y];y--)
  64.         {
  65.             int temp = list[y+1];
  66.             list[y+1] = list[y];
  67.             list[y] = temp;
  68.         }
  69.     }
  70. }

Last edited by ParadoxKing : December 14th, 2009 at 10:57 AM. Reason: solved the issue on my own

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Counting Repeated Elements in an array


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.

© 2003-2014 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap