#include<stdio.h> #include<iostream> using namespace std; int main() { setlocale(LC_ALL, ""); int a[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }, i, j,k; int(*b)[5]; b = (int(*)[5])a; for (i = 0; i < 4; i++) { for (j = 0; j < 5; j++) cout << b[i][j] << " "; cout << "\n"; } system("pause"); } 
  • 2
    Look at the map . - avp pm
  • @Xeller, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

3 answers 3

All answers are bad, except for the advice given by avp. I want to clarify only that if the task is taken literally, i.e. it is necessary to determine only the fact of the presence of repeating elements, then you can still distort yourself like this:

 #include <stdio.h> #include <stdlib.h> int main(void) { int array[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }; int repetitions = 0; int compare(const void* pa, const void* pb) { int a = *(int*)pa; int b = *(int*)pb; if (a == b) { repetitions = 1; return 0; } return a < b ? -1 : 1; } qsort(array, sizeof(array) / sizeof(int), sizeof(int), compare); puts(repetitions ? "Yes!" : "No."); return 0; } 
  • I'm afraid your example is not working ideone.com/05Lyzr - inceon
  • one
    Slyly invented. If you really pervert, then you can look at this modification: main () {... sigjmp_buf jmp; void L = && L1; ... if (sigsetjmp (jmp, 0)) goto * L; int compare (const void pa, const void * pb) {int a = (int ) pa; int b = (int ) pb; if (a == b) {repetitions = 1; siglongjmp (jmp); } return a <b? -eleven; } qsort (...); L1 :; puts (...); ... Too bad, a non-local goto (by void * L) does not allow to correctly exit functions, otherwise it could be ... - avp
  • @inceon, nested functions (and label addresses) is an extension to gcc. This is C, not C ++. It all works. - avp
  • In order to stop sorting on time, I would just write its code. For example, dwarf sorting is so laconic that it will turn out to be almost shorter than calling standard qsort with this your longjump. - VadimTukaev
  • However, there is a difference in the speed of work (IMHO for hundreds of millions of numbers the difference will be measured in hours). - avp

If we mean the same elements in the array, then

 for(i=0;i<20;i++){ for(j=i+1;j<20;j++){ if(a[i]==a[j]){ cout << "Identical elements: " << i << " and " << j << endl; } } } 

Or

 #include <algorithm> ... for(i=0;i<20;i++){ if(count(a,a+20,a[i])>1){ cout << "There identical " << a[i] << endl; break; } } 
     #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main(){ int a[] = { 1, 2, 3, 4, 5, -1, -2, -3, -4, -5, 7, 7, 7, 9, 10, -6, -7, -8, -9, -10 }; vector<int> v(a, a + 20); // Π·Π°ΠΊΠΈΠ½ΡƒΠ»ΠΈ массив Π² Π²Π΅ΠΊΡ‚ΠΎΡ€ set<int> s; // сюда ΠΌΡ‹ Π±ΡƒΠ΄Π΅ΠΌ Π»ΠΎΠΆΠΈΡ‚ΡŒ ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡŽΡ‰ΠΈΠ΅ числа Π² массивС ΠΈ Ρ…Ρ€Π°Π½ΠΈΡ‚ΡŒ ΠΈΡ… Π² СдинствСнном числС (искомый Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚) for (vector<int>::iterator it = v.begin(); it != v.end(); it++){ // Π±Π΅Ρ€Π΅ΠΌ число ΠΈΠ· массива if(count(v.begin(), v.end(), *it) > 1){ // Ссли Π΅ΡΡ‚ΡŒ Π΄ΡƒΠ±Π»ΠΈΡ€ΡƒΡŽΡ‰Π΅Π΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ cout << "finded: " << *it << endl; // ΠΎΡ‚Π»Π°Π΄ΠΊΠ° s.insert(*it); // Π»ΠΎΠΆΠΈΠΌ Π΅Π³ΠΎ Π² Π½Π΅ΠΏΠΎΠ²Ρ‚ΠΎΡ€ΡΡŽΡ‰ΠΈΠΉΡΡ ΠΊΠΎΠ½Ρ‚Π΅ΠΉΠ½Π΅Ρ€ Ρ‚ΠΈΠΏΠ° set continue; } } for (set<int>::iterator it = s.begin(); it != s.end(); it++){ // Π²Ρ‹Π²ΠΎΠ΄ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° cout << "Look for duplicate value: -> " << *it << " <- found: " << count(v.begin(), v.end(), *it) << endl; } return 0; } 

    alt text