Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. #define INPUT_FILE "input.txt"
  5. #define OUTPUT_FILE "output.txt"
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. bool end = false;
  12.  
  13. int N; //numero di valori
  14. int X; //numero da confrontare
  15. int y; //valore da leggere
  16.  
  17. fstream infile, outfile;
  18. infile.open(INPUT_FILE,fstream::in);
  19. outfile.open(OUTPUT_FILE,fstream::out);
  20.  
  21. if (!infile.is_open())
  22. {
  23. cerr << "errore apertura file input " << INPUT_FILE << endl;
  24. return -1;
  25. }
  26.  
  27. if (!outfile.is_open())
  28. {
  29. cerr << "errore apertura file output " << OUTPUT_FILE << endl;
  30. return -1;
  31. }
  32.  
  33. while (!end)
  34. {
  35. infile >> N >> X;
  36.  
  37. if (infile.eof())
  38. {
  39. cout << "fine input, bestia!" << endl;
  40. end = true;
  41. }
  42.  
  43. else if (infile.fail())
  44. {
  45. cerr << "errore lettura file input " << INPUT_FILE << endl;
  46. end = true;
  47. }
  48.  
  49. else
  50. {
  51. int cont = 0;
  52. for (int i=0; i<N; i++)
  53. {
  54. infile >> y;
  55. if (y > X) {
  56. cont++;
  57. }
  58. }
  59.  
  60. int vett[cont] = {0};
  61. for (int i=0; i<N; i++)
  62. {
  63. infile >> y;
  64. if (y > X) {
  65. vett[i] = y;
  66. }
  67. }
  68.  
  69. outfile << cont << " ";
  70. for (int i=0; i<cont; i++ ) {
  71. outfile << vett[i] << " ";
  72. }
  73. outfile << endl;
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement