Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8.  
  9. /*Zadatak 5. Napišite program koji iz tekstualne datoteke učitava brojeve, pohrani ih u vektor, te napravi
  10. sljedeće:
  11. 1. ispiše učitane brojeve u obrnutom redoslijedu,
  12. 2. ispiše aritmetičku sredinu
  13. 3. ispiše najmanji i najveći broj*/
  14.  
  15.  
  16.  
  17.  
  18. int main() {
  19. vector <int> V_Brojeva;
  20.  
  21. ifstream fileRead("brojevi.txt");
  22.  
  23. if (!fileRead){
  24. cout << "Dogodila se greska prilikom otvaranja datoteke!" << endl;
  25. return 1;
  26. }
  27.  
  28. int brojevi;
  29.  
  30. while (fileRead >> brojevi){
  31. V_Brojeva.push_back(brojevi);
  32. }
  33.  
  34. int sum = 0;
  35. double a = 0;
  36. int najveci=V_Brojeva[0];
  37. int najmanji=V_Brojeva[0];
  38. for (int i = 0; i < V_Brojeva.size(); i++){
  39. a++;
  40. sum = sum + V_Brojeva[i];
  41. if (najveci < V_Brojeva[i]){ //Najveci if
  42. najveci = V_Brojeva[i];
  43. }
  44. if (najmanji > V_Brojeva[i]){ //Najmanji if
  45. najmanji = V_Brojeva[i];
  46. }
  47. }
  48.  
  49. fileRead.close();
  50. cout << "Aritmeticka sredina je:" << sum / a << endl;
  51. cout << "Najveci broj je:" << najveci << endl;
  52. cout << "Najmanji broj je:" << najmanji << endl;
  53. cout << "-------------------" << endl;
  54. cout << "Ispis obrnutim redosljedom:" << endl;
  55. for (int i = V_Brojeva.size()-1; i >=0; i--){
  56.  
  57. if (i > 0){
  58. cout << V_Brojeva[i] << " ,";
  59. }else
  60. cout << V_Brojeva[i] << "." << endl;
  61. }
  62.  
  63.  
  64.  
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement