Valami

From Rock3r, 9 Years ago, written in C++, viewed 679 times.
URL https://paste.godclan.hu/view/jU-zH7Rf Shorturl http://gw.gd/w33z Embed
Download Paste or View Raw
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.     void Rendezes( char alma[], int n ) {
  7.             char asd;
  8.            
  9.             for ( int  i = 0; i < n; i++ ) {
  10.                   for ( int j = i + 1; j < n; j++ ) {
  11.                          if ( alma[j] < alma[i] ) {
  12.                                  asd = alma[i];
  13.                                  alma[i] = alma[j];
  14.                                  alma[j] = asd;
  15.                               }
  16.                       }
  17.                 }
  18.          }
  19.     int Kereses( char alma[], int n, char ker ) {
  20.            int alsoh = 0;
  21.            int felsoh = n - 1;
  22.            int kozep = 0;
  23.            
  24.            
  25.            for ( kozep = (alsoh + felsoh) / 2; felsoh >= alsoh; kozep = ( alsoh + felsoh ) / 2 ) {
  26.                   if ( alma[kozep] == ker ) {
  27.                          return kozep;
  28.                        }
  29.                   if ( alma[kozep] < ker ) {
  30.                          alsoh = kozep + 1;
  31.                        }
  32.                   else {
  33.                          felsoh = kozep - 1;
  34.                        }
  35.                }
  36.                return -1;
  37.         }
  38. int main(int argc, char *argv[])
  39. {
  40.     int n = 10;
  41.     char alma[n];
  42.     int a;
  43.     char ker = 'a';
  44.    
  45.     for ( int i = 0; i < n; i++ ) {
  46.           cout << "[" << i+1 << ".]: ";
  47.           cin >> alma[i];
  48.         }
  49.     Rendezes( alma, n );    
  50.    
  51.     a = Kereses(alma, n, ker);
  52.    
  53.     cout << a << endl;    
  54.    
  55.     system("PAUSE");
  56.     return EXIT_SUCCESS;
  57. }
  58.  

Reply to "Valami"

Here you can reply to the paste above