c++ - How to count how many times each number has been encountered? -


i trying write program count each number program has encountered. putting m input number of array elements , max maximum amount of number shouldn't exceed number when writing input in m[i]. reason program works fine when enter small input

data input:

10 3 1 2 3 2 3 1 1 1 1 3 

answer:

5 2 3 

but when put big input 364 array elements , 15 example max. output doesn't work expected , can't find reason that!

#include "stdafx.h" #include <iostream> #include<fstream> #include<string> #include <stdio.h> #include<conio.h> using namespace std;  int  arrayvalue; int max; int m[1000]; int checker[1000]; int element_cntr = 0; int cntr = 0; int n = 0; void main() {      cout << "enter lenght of elements, followed maximum number: " << endl;     cin >> arrayvalue>> max;      (int = 0; < arrayvalue; i++)     {         cin >> m[i];         checker[i]= m[i] ;         element_cntr++;          if (m[i] > max)         {             cout << "the element number " << element_cntr << " bigger " << max << endl;         }       }       (int = 0; < max; i++)     {         cntr = 0;         (int j = 0; j < arrayvalue; j++)         {              if (m[n] == checker[j])             {                 cntr+=1;              }                 }           if (cntr != 0)         {             cout << cntr << " ";         }         n++;     }      } 

you have general algorithm problem , several code issues make code hardly maintainable, non-readable , confusing. that's why don't understand why not working.

let's review step step.

the actual reason of incorrect output iterate through first max items of array when need iterate through first max integers. example, let have input:

7 3 1 1 1 1 1 2 3 

while correct answer is: 5 1 1, program output 5 5 5, because in output loop iterate through first 3 items , make output them:

 (int = 0; < max; i++)     (int j = 0; j < arrayvalue; j++)         if (m[n] == checker[j]) // m[0] 1, m[1] 1 , m[2] 1 

it output answers first 3 items of initial array. in example, worked fine because first 3 items 1 2 3.
in order make work, need change condition to

if (n == checker[j]) // oh, why need variable "n"? have "i" loop! {     cntr += 1; }     

it work, both code , algorithm absolutely incorrect...

not proper solution

you have unnecessary variable element_cntr - loop variable i provide the same values. duplicating it's value.

also, in output loop create variable n while have loop variable i the same. can safely remove variable n , replace if (m[n] == checker[j]) if (m[i] == checker[j]).

moreover, checker array full copy if variable m. why duplicate values? :)

your code should look, @ least, this:

using namespace std;  int arrayvalue; int max; int m[1000]; int cntr = 0;  int main() {      cout << "enter lenght of elements, followed maximum number: " << endl;     cin >> arrayvalue >> max;      (int = 0; < arrayvalue; i++)     {         cin >> m[i];          if (m[i] > max)         {             cout << "the element number " << << " bigger " << max << endl;         }     }       (int = 0; < max; i++)     {         cntr = 0;         (int j = 0; j < arrayvalue; j++)         {             if (i == m[j])             {                 cntr ++;             }               }          if (cntr != 0)         {             cout << cntr << " ";         }     }      return 0; } 

proper solution

why need nested loop @ all? take o(n*m) operations count occurences of items. can counted o(n) operations.

just count them while reading:

using namespace std;  int arraysize; int maxvalue; int counts[1000];  int main() {      cout << "enter lenght of elements, followed maximum number: " << endl;     cin >> arraysize >> maxvalue;      int lastreadvalue;      (int = 0; < arraysize; i++)     {         cin >> lastreadvalue;          if (lastreadvalue > maxvalue)             cout << "number " << << " bigger maxvalue! skipping it..." << endl;         else             counts[lastreadvalue]++; // read , increase occurence count     }       (int = 0; <= maxvalue; i++)     {         if (counts[i] > 0)                       cout << << " occurences: " << counts[i] << endl; // output existent numbers     }      return 0; } 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -