Chcel by som sa spytat ako by sa dalo spravit to ze mam zadanych N cisel (kazde je v inej premennej) ,a ak su vsetky rovnake tak
ich vypise... Dik
Fórum › C / C++
Práce s polem

Napríklad takto:
int numbers[] = {1, 1, 1, 2};
int i = 1;
for (i; i < 4; ++i)
{
if (numbers[i] != numbers[i - 1])
break;
}
if (i != 4)
... // cisla nejsu rovnake, urob nieco
Ospravedlnujem sa, som si nevšimol tú zátvorku kazde je v inej premennej. Predchádzajúci kód samozrejme platí v prípade, že čísla sú uložene v poli, alebo inej štruktúre s preťaženým operátorom []. V prípade, že každé číslo je uložené v inej premennej tak ak je tých premenných málo, potom operátorom ==. Ak ich je veľa, tak sa priznám že ma nič nenapadá (ešte som sa s takou situáciou nestretol).
No,takové nehezké řešení je třeba použití proměnného počtu parametrů(první parametr udaváva počet proměnných):
#include <iostream>
#include <cstdarg>
bool equal(int count, ...)
{
va_list list;
va_start(list, count );
int i, num = va_arg(list,int);
for(i = 1; i < count ; ++i)
{ int temp = va_arg(list,int);
if(num != temp) break;
num = temp;
}
va_end(list);
return ( i == count );
}
int main()
{
if(equal(5, 2,1,1,1,2)) std::cout << "stejne";
else std::cout << "rozdilne";
return 0;
}
Lenze ja to mam v 2D poli asi takto :
for (int i=0;i<N;i++)
{
for (int j=0;i<100;j++)
{
if (Cisla[i][j]==Cisla[i+1][j] && i!=N)
{
}
}
}
A neviem ako pokracovat .Proste chcem len aby vtedy ked je cislo rovnake vo vsetkych poliach nech ho niekam priradi...
To Serath : V riadku 5 máte zrejme chybu, i<100 ma byť j<100 a ak platí predchádzajúci predpoklad, alebo N<100, tak potom riadok osem je chybný a dochádza v ňom k prekročeniu hraníc poľa Cisla a teda k čítaniu nezmyslov. Konkrétne je chyba tu Cisla[i+1][j].

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Prace s polem — založil jp
Práce s polem — založil k1
Práce s polem - porovnání — založil MikeCZ
Práce s polem(odstraněni,pridani,vytisteni) — založil Petr
Menu , prace s textovymi soubory - závěrečná práce , nevim si rady :( — založil honzajs
Moderátoři diskuze