Count repetitive Input (Array)

Hello again,

I wrote a modular program, which

-gets 10 integers and saves it in an array,
-sorts them by value ( + displays the biggest and smallest one),
-counts the amount of identical numbers typed in-->doesn't work

The function of interest can be found at the bottom of this post !


**MAIN-FUNCTION**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include "functions.cpp"
using namespace std;

int main(int argc, char* argv[])
{
	
	
	
	arrayInitializer(array);
	arrayDisplay(array);
	
	sortArray(array);
	displaySortArray(array);
	
	displaySmallest_and_BiggestVal(array);
	
	counter(array);
	
	
	confirm();
	
	fflush(stdin);
	
    return 0;
}



**FUNCTIONS**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int array[10]{0};
int count[2][10];

int arrayInitializer(int array[10])
{
	int x=0;
	cout<<"Please type in 10 integers:\n";
	for(int i=0;i<10;i++)
	{
	cin>>x;
	array[i]=x;
	}
	
	return *array;              

}


int arrayDisplay(int array[10])
{
	cout<<"\n\n"<<endl;
	cout<<"This is your Array-Data:\n"<<endl;
	for(int i=0;i<10;i++)
	{
	cout<<array[i]<<" ";
	}
	cout<<"\n\n"<<endl;
}


int sortArray(int array[10]){    


	int temp;
	int i, j;
	
	 for(i=0;i<10;i++)
	 {	
     for(j=0;j<10;j++) 				
      if(array[j]>array[j+1]){ 	//Index changes +1, so instead of 0...10, we now have 1...11
         temp=array[j]; 
         array[j]=array[j+1]; 
         array[j+1]=temp; 
      						 } 
     }

	return *array;
}


void displaySortArray(int array[10]){
	cout<<"\n";
	cout<<"This is your sorted Array:\n"<<endl;
	for(int i=1;i<11;i++)	//Index +1  -> See upper comment
	{
	cout<<array[i]<<" "; 
	}

}

void displaySmallest_and_BiggestVal(int array[10]){
	cout<<"\n\n";
	cout<<"Smallest Value: "<<array[1]<<endl;
	cout<<"Biggest  Value: "<<array[10]<<endl;
}

void counter(int array[10]){
 

 		
 	for(int j=0;j<10;j++){
 	count[1][j]=1;     	//Initialise 2nd Row with 1
 	}
 	
 	for(int i=0;i<1; i++){	     //keep i constant at 0, the first row
 	for(int j=0;j<10;j++){
 	for(int r=0;r<10;r++){
 	
 	if(count[i][j]==count[i][r]){ //if count[0][j]== count[0][r]  (r variable from 0->9)
 		count[1][j]+=1;       //yes: a certain number(i) occured several
                                      //times(at least 2 times),
                                      //so increase element cout[1][j] by 1
 	} else{}	              //else: do nothing
 	}
 	}
						}
 cout<<"This is your occurence-counting Array\n"<<endl;
 
 for(int i=0;i<2;i++){
 for(int j=0;j<10;j++){
 cout<<count[i][j]<<"\t ";
}
cout<<endl;
}
}


void confirm(){
	cout<<"In order to quit your program, please press '~' \n";
	char z;
	do{
	cin>>z;
	}
	while(z!='~');
}



The problem I have is HOW to count this repitition,

what I tried was the following(see function "counter(int array[10])") :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void counter(int array[10]){
 

 		
 	for(int j=0;j<10;j++){
 	count[1][j]=1;    //Initialise 2nd Row with 1
 	}
 	
 	for(int i=0;i<1; i++){	//keep i constant at 0, the first row
 	for(int j=0;j<10;j++){
 	for(int r=0;r<10;r++){
 	
 	if(count[i][j]==count[i][r]){	//if count[0][j]== count[0][r] 
                                        //(r variable from 0->9)
 		count[1][j]+=1;	//yes: a certain number(i) occured several
                                // times(at least 2 times),
                                // so increase element cout[1][j] by 1
 	} else{}                //else: do nothing
 	}
 	}
						}
 cout<<"This is your occurence-counting Array\n"<<endl;
 
 for(int i=0;i<2;i++){
 for(int j=0;j<10;j++){
 cout<<count[i][j]<<"\t ";
}
cout<<endl;
}
}



Does anybody see a mistake in the design, so the general idea of how I count ?

What I intended to do with the variable "int r" is the following:

Note: i is constant
For e.g. an element of the value 12, "count[i][j=0]", iterate over all j.
Because this element has to stay constant, i use "r" to iterate over the elements of the 2nd row: count[i][r]
If one of those (iterated) elements equals the constant one(here 12), increment the counter.

The loop does that for every elements[0][j], so that all typed in numbers are being checked.

Does that make sense? I seriously can't tell ^^

(one thing: Did I maybe mess up with the IMPLEMENTATION(#include "...") of the functions-code in my main-code ?)


I am thankful for every attempt to help me out here,

Have a nice evening !
Last edited on
I see one right away, you initialize all of the counts to 1, but then in your if statement you increment plus 1. Basically your

1
2
3
4
5
6
if(count[i][j]==count[i][r]){	//if count[0][j]== count[0][r] 
                                        //(r variable from 0->9)
 		count[1][j]+=1;	//yes: a certain number(i) occured several
                                // times(at least 2 times),
                                // so increase element cout[1][j] by 1
 	}


will start by comparing the variable to itself on the first time through, and adding one. So right off the bat, your count will be 2 for each variable. You'd be better off setting each count number to 0 if you're going to check them this way. If not, have an if statement that checks to make sure that j does not equal r (preventing it from checking itself as the duplicate). That's the first problem I noticed.

What other problems is the function giving you? If you give me specific undesired outcomes, I could probably help you better. What is your program doing? Cause all you gave me is an explanation of how it works, which is helpful, but more specifics on the problem itself will help me determine if the problem is within the count function, or if it is outside the function.
Last edited on

Try something like the following:
1
2
3
4
5
6
7
8
9
10
11
count[10][2] = {0} // possibly 10 different ints; first column will contain the number
                   // second the count
for loop to iterate over array
  for loop to iterate column 1 in count
    check if number from array is in count
      if so increment column 2 in count
      break
    if not in count
      add it
      and increment
      break

1
2
3
4
5
6
for (int i=0; i<10; ++i) { // for loop for output
  if(count[i][0] == 0){	
    break;		
  }
  count cout << count[i][0] << "\t" << count[i][1] << endl;
}

HTH

Edit: typo
Last edited on
Thanks for your time !
I am pretty busy at the moment , I am programming as an assistant scientist, but I will try to deal with your ideas today, or at last tomorrow!

@KvltKitty :

My problem is, that the array count[Values][Occurence] does not count the amount of identical Values.

See you soon !
If the array is sorted, you can rely on this to count the number of repetitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int size = 10;

void counter(int array[size]) {
    int counter = 0;
    
    for (int t = 0; t < size; ) {
        int prev = t;
        for (int r = t + 1; r < size && array[t] == array[r]; r++, t++)
        ;
        if (prev != t) {
            counter++;
        }
        else {
            t++;
        }
    }
    
    std::cout << counter << std::endl;        
}


if the array is unsorted, you can either sort it first then count number of repetitions using the above function or use a map datatype to count number of repetitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <map>

void counter(int array[size]) {
    int counter = 0;
    std::map<int, int> table;
    
    for (int t = 0; t < size; t++) {
        if (table[array[t]]++ == 1) {
            counter++;
        }
    }
    std::cout << counter << std::endl;        
}


http://coliru.stacked-crooked.com/a/4b879c9fa8e34d91
Last edited on
Topic archived. No new replies allowed.