Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. #define ROZM_TABL_MAX 1000
  9.  
  10. // Deklaracje (prototypy) własnych funkcji użytych w programie
  11.  
  12. int WypelnTabl( int tabl[], int rozmTabl );
  13. int Sortowanie( int tabl[], int rozmiarTabl );
  14. int MinIndex( int tabl[], int rozmTabl );
  15. int MaxIndex( int tabl[], int rozmTabl );
  16. // Sortowanie( int tabl[], int rozmiarTabl );
  17. double Srednia( int tabl[], int rozmTabl );
  18.  
  19. int CzytajInt( int min, int max );
  20. void PiszTabl( int tabl[], int rozmTabl, int nKol );
  21.  
  22.  
  23.  
  24.  
  25. //---------------------------------------------------------
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. int t[ROZM_TABL_MAX];
  30. int ilLiczb, ilKolumn;
  31. int idxMin,idxMax;
  32. double sr;
  33. int sorto;
  34.  
  35. // 1. Uzupelnic
  36. cout << "podaj liczbe"<<endl;
  37. ilLiczb = CzytajInt( 1, ROZM_TABL_MAX );
  38. WypelnTabl( t, ilLiczb );
  39.  
  40. // 2. Uzupelnic
  41. ilKolumn = CzytajInt( 1, 8 );
  42.  
  43. cout << "podaj liczbe" << endl;
  44. PiszTabl( t, ilLiczb, ilKolumn );
  45. cout<<endl<<endl;
  46. sorto = Sortowanie( t, ilLiczb);
  47. sr = Srednia( t, ilLiczb );
  48. idxMin = MinIndex( t, ilLiczb );
  49. idxMax = MaxIndex( t, ilLiczb );
  50.  
  51.  
  52. // 3. Uzupelnic
  53.  
  54. cout<<endl<< "srednia wynosi " << sr << endl;
  55. cout<< "najmniejszy element "<<idxMin<<endl;
  56. cout << "njwiekszy element " <<MaxIndex(t,ilLiczb)<<endl;
  57. //cout<<"Sortowanie: "<<sorto<<endl;
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63. //---------------------------------------------------------
  64.  
  65.  
  66. int WypelnTabl( int tabl[], int rozmTabl )
  67. {
  68. int ofs = RAND_MAX / 2;
  69. int i;
  70.  
  71. srand( (unsigned int)time( NULL ) );
  72. for( i = 0; i < rozmTabl; i++ )
  73. tabl[ i ] = rand( ) - ofs;
  74.  
  75. return rozmTabl;
  76. }
  77.  
  78. //---------------------------------------------------------
  79.  
  80. int MaxIndex( int tabl[], int rozmTabl )
  81. {
  82. int tmp,index=0;
  83.  
  84.  
  85. tmp=tabl[0];
  86. //cout<<tmp<<"<-Tutaj"<<endl;
  87.  
  88.  
  89. for(int i=1 ;i<rozmTabl ;i++)
  90. if(tmp <= tabl[i])
  91. {
  92. tmp = tabl[i];
  93. index=tmp;
  94. }
  95. else
  96. tmp=tmp;
  97.  
  98. return index;
  99. }
  100. // 4. Uzupelnic
  101.  
  102. //---------------------------------------------------------
  103.  
  104. int MinIndex( int tabl[], int rozmTabl )
  105. {
  106. int tmp,index=0;
  107.  
  108.  
  109. tmp=tabl[0];
  110.  
  111.  
  112. for(int i=1 ;i<rozmTabl ;i++)
  113. if(tmp > tabl[i])
  114. {
  115. tmp = tabl[i];
  116. index=tmp;
  117. }
  118. else
  119. tmp=tmp;
  120.  
  121. return index;
  122. }
  123. // 5. Uzupelnic
  124.  
  125. //---------------------------------------------------------
  126.  
  127. double Srednia( int tabl[], int rozmTabl )
  128. {
  129. double sr=0;
  130. int suma=0;
  131.  
  132. for(int i=0;i<rozmTabl;i++){
  133.  
  134. suma+=tabl[i];
  135. }
  136. //cout<<suma<<"<-Tutaj"<<endl;
  137. sr=(suma/rozmTabl);
  138. return sr;
  139. }
  140. // 6. Uzupelnic
  141.  
  142. //---------------------------------------------------------
  143.  
  144. int CzytajInt( int min, int max )
  145. {
  146. int iliczba;
  147.  
  148. cin>>iliczba;
  149.  
  150. if(iliczba>max)
  151. return max;
  152. else if (iliczba<min)
  153. return min;
  154. else
  155. return iliczba;
  156. }
  157. // 7. Uzupelnic
  158.  
  159. //---------------------------------------------------------
  160.  
  161. void PiszTabl( int tabl[], int rozmTabl, int nKol )
  162. {
  163. cout<<"0. "<<tabl[0]<<"\t";
  164. for(int i = 1 ; i <rozmTabl; i++)
  165. {
  166. if(i%nKol)
  167. cout <<"\t";
  168. else
  169. cout << "\n";
  170.  
  171. cout<<i<<". "<<tabl[i]<<" ";
  172. }
  173. }
  174.  
  175. // 8. Uzupelnic
  176.  
  177. int Sortowanie( int tabl[], int rozmiarTabl )
  178. {
  179. for( int i = 0; i < rozmiarTabl; i++ )
  180. {
  181. for( int j = 0; j < rozmiarTabl - 1; j++ )
  182. {
  183. if( tabl[ j ] > tabl[ j + 1 ] )
  184. swap( tabl[ j ], tabl[ j + 1 ] );
  185.  
  186. }
  187. cout<< tabl[i] <<endl;
  188. }
  189.  
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement