There is an arbitrary array in which you need to sort the repeating elements by decrease and output the total number of repetitions. I decided to implement it in the following way: first, just sort the array using the bubble method, then cycle out the condition for matches, and if they are there, simply display them on the screen and add +1 to the match counter, thus avoiding the re-creation of the array. Wrote the following code:
#include <stdio.h> #include <conio.h> #define MAX 50 main(){ int array[MAX], n, i, j, tmp, repeat; printf("Number of elements array = "); scanf("%i",&n); printf("Type array elements:\n"); for(i=0; i<n; i++){ printf("Array[%i] = ",i); scanf("%i",&array[i]); if(array[i]==0) break; } for(i=0; i<n; i++){ tmp = 0; for(j=(i+1); j<n; j++){ if(array[i] < array[j]){ tmp = array[i]; array[i] = array[j]; array[j] = tmp; } } } printf("The result of sorting:\n"); for(i=0; i<n; i++){ printf("%i\t",array[i]); } printf("\nRepeated elements:\n"); for(i=0,tmp=0; i<n; i++){ for(j=(i+1); j<n; j++){ if(array[i] == array[j]){ tmp++; printf("%i\t%i\t",array[i],array[j]); } } } if(tmp > 0){ printf("\nThe number of repetitions: %i",tmp); } else printf("\nThe array elements is not repeated"); getch(); return 0; }
As you might have guessed, the comparison algorithm does not work correctly, namely: Screen of the running program