#include <iostream> using namespace std; int max(int, int); // <<<<<<<<<<Обьявляем функцияю до её использования void main()//--> Начало главной функции !!! { setlocale(LC_ALL,"Rus"); int mas[15] = {-1,10,3,-6,10,-8,7,-1,6,7,7,6,4,9,-8}; // <<Массив int min = 0; int min2 = 0; // <<Минимальное число число int counter = 0; // <<Счётчик поBторений //-> cout<<"Вводим массив\n"; min2 = mas[0]; //-1 //Присваиваем минимальному числу первый элемент массива for(int e=0;e<14;e++){ //< пробегаем цикл гарантированно все 15 элементов while(min>mas[e]){ //Если минимальное число меньше находящегося в массиве, тогда присвоить ему это число counter++; // Увеличить счётчик на 1 min = mas[e]; if (counter=1){ min = min2; cout<<min; counter=0; break; //////////////////////////////////////////////////////<<<<<<<<<<<<< Найдено } else break; } } } //<--------------|конец главной функции !!! 

He thinks incorrectly, why not, why, already confused, help please give out -1, -1, -1, if there is a better algorithm, write, it will be better

  • one
    @IgorbShum, firstly you wrote if (counter = 1) instead of if (counter == 1) what you obviously wanted, and secondly, this is not the point. Just your algorithm does not solve this problem. - Probably the simplest (but not efficient) way is to count the number of entries in the array of each negative element. Those. for (i = 0; i <n; i ++) {if (mas [i] <0 && count (mas, n, mas [i]) == 1) {cout << "Found" << mas [i] << "in pos" << i << '\ n'; break; }} if (i == n) cout << "Not found \ n"; And write the count() function yourself. - avp 8:08 pm

1 answer 1

There are mistakes.

 for(int e=0;e<14;e++){ //< пробегаем цикл гарантированно все 15 элементов 

Not 15, but 14 passes. From 0 to 13. 14 under the condition "<14" will not work.

if (counter=1)

^ There must be "==", otherwise this condition will always be met.

In general, a lot of things are misunderstood, for example, the working version:

 #include <iostream> using namespace std; int main()//--> Начало главной функции !!! { setlocale(LC_ALL,"Rus"); int mas[15] = {-1,10,3,-6,10,-8,7,-1,6,7,7,6,4,9,-8}; // <<Массив //-> for(int e=0;e<15;e++){ //< пробегаем цикл гарантированно все 15 элементов bool unique=true; // уникален ли элемент if( mas[e] >= 0 ) continue; // Пропускаем неотрицательные элементы for(int f=0;f<15;f++){ if(f==e) continue; // Пропуск самого себя if(mas[e]==mas[f]) { unique = false; // Найден дубликат break; } } if( unique ) cout << mas[e] << endl; // Выводим отрицательный элемент, уникальный в массиве } return 0; }