fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. void generateGnuplotScript(const vector<double>& G, const vector<double>& sucplain, const vector<double>& sucslotted) {
  10. ofstream scriptFile("plot_script.gnu");
  11. if (!scriptFile.is_open()) {
  12. cerr << "Failed to open plot script file!" << endl;
  13. return;
  14. }
  15.  
  16. scriptFile << "set terminal png\n";
  17. scriptFile << "set output 'plot.png'\n";
  18. scriptFile << "set xlabel 'G'\n";
  19. scriptFile << "set ylabel 'Throughput'\n";
  20. scriptFile << "plot '-' with lines title 'Plain ALOHA', '-' with lines title 'Slotted ALOHA'\n";
  21.  
  22. for (int i = 0; i < G.size(); ++i) {
  23. scriptFile << G[i] << " " << sucplain[i] << endl;
  24. }
  25. scriptFile << "e\n";
  26.  
  27. for (int i = 0; i < G.size(); ++i) {
  28. scriptFile << G[i] << " " << sucslotted[i] << endl;
  29. }
  30. scriptFile << "e\n";
  31.  
  32. scriptFile.close();
  33.  
  34. cout << "Gnuplot script generated successfully!" << endl;
  35. }
  36.  
  37. int main() {
  38. const int N = 100000; // Number of packets in unit time
  39. vector<double> PacketArrivalTimes(N);
  40.  
  41. // Generate random numbers between 0 and 1
  42. generate(PacketArrivalTimes.begin(), PacketArrivalTimes.end(), []() { return rand() / (double)RAND_MAX; });
  43.  
  44. // Sort PacketArrivalTimes
  45. sort(PacketArrivalTimes.begin(), PacketArrivalTimes.end());
  46.  
  47. vector<double> y1(N - 1);
  48. vector<double> y2(N - 1);
  49.  
  50. // Calculate left difference between arrival times
  51. for (int i = 0; i < N - 1; ++i) {
  52. y1[i] = PacketArrivalTimes[i + 1] - PacketArrivalTimes[i];
  53. }
  54.  
  55. // Calculate right difference (shift x by 1)
  56. for (int i = 0; i < N - 1; ++i) {
  57. y2[i] = PacketArrivalTimes[(i + 1) % N] - PacketArrivalTimes[i];
  58. }
  59.  
  60. vector<double> sucplain(100);
  61. vector<double> sucslotted(100);
  62. vector<double> G(100);
  63.  
  64. // Varying G between 0 and 1.5 in 100 steps
  65. for (int m = 0; m < 100; ++m) {
  66. G[m] = m / 66.0;
  67.  
  68. double dur = G[m] / N;
  69.  
  70. int NumberSuccessfulPlain = 0;
  71. int NumberSuccessfulSlotted = 0;
  72.  
  73. // Count successful transmissions for Plain ALOHA and Slotted ALOHA
  74. for (int i = 0; i < N - 1; ++i) {
  75. if (y1[i] > dur && y2[i] > dur) {
  76. ++NumberSuccessfulPlain;
  77. }
  78. if (y1[i] > dur) {
  79. ++NumberSuccessfulSlotted;
  80. }
  81. }
  82.  
  83. // Calculate throughput for Plain ALOHA and Slotted ALOHA
  84. sucplain[m] = NumberSuccessfulPlain * dur;
  85. sucslotted[m] = NumberSuccessfulSlotted * dur;
  86. }
  87.  
  88. // Generate Gnuplot script
  89. generateGnuplotScript(G, sucplain, sucslotted);
  90.  
  91. // Invoke Gnuplot
  92. system("gnuplot plot_script.gnu");
  93.  
  94. return 0;
  95. }
  96.  
Success #stdin #stdout #stderr 0.06s 5808KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Failed to open plot script file!
sh: 1: gnuplot: not found