943,796 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 593
  • C++ RSS
Mar 20th, 2009
0

array size declaration after inputs

Expand Post »
Why won't this let me choose my own array size?

Queue line[qs_pair] is what i'm concern about. As you can see, I have the user input from the terminal the size of the array. But when I compile it, it gives me an error on visual studio.

line unknown size
cannot allocate an array of constant size 0

it works on unix, but does not work on visual studio. Anyone has any idea why?

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "queue.h"
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. int get_lowest(Queue [], int);
  7.  
  8. int main()
  9. {
  10. int qs_pair = 10, maxtran, prob, dur, seed;
  11. int count(0); // The number of customers served
  12. int entry_time(0); // When each served customer arrived
  13. int wait_sum(0); // Sum of waiting times
  14. int server_count(0); // count the server;
  15.  
  16. cout << "how many queue/server pair(s)? ";
  17. cin >> qs_pair;
  18. cout << "\nthe max time for customer transaction --> ";
  19. cin >> maxtran;
  20. cout << "\nthe probability a customer will arrive --> ";
  21. cin >> prob;
  22. cout << "\nduration of simulation(at least 1) --> ";
  23. cin >> dur;
  24. cout << "\nduration of simulation(at least 1) --> ";
  25. cin >> dur;
  26. cout << "\nenter an integer seed --> ";
  27. cin >> seed;
  28.  
  29. srand(seed);
  30. Queue line[qs_pair];
  31. int trans_time[qs_pair]; //time remaining in a transaction
  32.  
  33. for(int i = 0; i < qs_pair; i++)
  34. {
  35. trans_time[i] = 0;
  36. }
  37.  
  38. int lowest;
  39. int trans_time_c; // trans_time counter
  40.  
  41. for(int time = 0; time < dur; time++)
  42. {
  43. if(rand() % 100 < prob)
  44. {
  45. lowest = get_lowest(line, qs_pair);
  46.  
  47. lowest = get_lowest(line, qs_pair);
  48.  
  49. line[lowest].enqueue(time);
  50. }
  51.  
  52. //good above
  53.  
  54. for(trans_time_c; trans_time_c < qs_pair; trans_time_c++)
  55. {
  56. if(trans_time[trans_time_c] == 0)
  57. {
  58. if(line[trans_time_c].return_index() != 0)
  59. {
  60. entry_time = line[trans_time_c].dequeue();
  61. wait_sum += (time - entry_time);
  62. ++count;
  63. trans_time[trans_time_c] = (rand() % maxtran) + 1;
  64. }
  65. }
  66. else
  67. {
  68. trans_time[trans_time_c] -= 1;
  69. }
  70.  
  71. }
  72. }
  73.  
  74.  
  75. }
  76.  
  77. int get_lowest(Queue line[], int qs_pair)
  78. {
  79. int temp = line[0].return_index();
  80. int temp_i = 0;
  81.  
  82. for(int i = 0; i < qs_pair; i++)
  83. {
  84. if(line[i].return_index() == 0)
  85. return i;
  86. else if((i == qs_pair - 1) && line[qs_pair - 1].return_index() < temp)
  87. {
  88. temp = line[qs_pair - 1].return_index();
  89. temp_i = qs_pair - 1;
  90. }
  91. else
  92. {
  93.  
  94. if(line[i].return_index() > temp)
  95. {
  96.  
  97. }
  98. else
  99. {
  100. temp = line[i].return_index();
  101. temp_i = i;
  102. }
  103. }
  104. }
  105. return temp_i;
  106. }
Similar Threads
Reputation Points: 46
Solved Threads: 1
Junior Poster in Training
homeryansta is offline Offline
87 posts
since Jan 2009
Mar 20th, 2009
0

Re: array size declaration after inputs

The size of arrays are resolved at compile time, when qs_pair is 10, thus when you enter more than 10 you will get overflow errors.

You should be using dynamic allocation when dealing with creating an array of size N where N is specified by user input.
C++ Syntax (Toggle Plain Text)
  1. int *example = new int[N];
  2. ...
  3. delete [] example;

Chris
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: copy_n in GNU C++ Library
Next Thread in C++ Forum Timeline: ftell() is returning the wrong position of file why?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC