array size declaration after inputs

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2009
Posts: 62
Reputation: homeryansta is an unknown quantity at this point 
Solved Threads: 0
homeryansta homeryansta is offline Offline
Junior Poster in Training

array size declaration after inputs

 
0
  #1
Mar 20th, 2009
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: array size declaration after inputs

 
0
  #2
Mar 20th, 2009
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.
  1. int *example = new int[N];
  2. ...
  3. delete [] example;

Chris
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC