Using 2 Arrays in a fuction

Hi guys! I am having some trouble understanding how to define a function that can use 2 arrays. I apologize in advance for all the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int size = 100;

// Prototypes:
void bulgarian_move(unsigned int stcks[], const int max_size);
//other declared fuctions

int main()
{
  do
  {
    unsigned int stacks[size] = {0};
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    srand((unsigned)time(0));
    build_stacks(stacks, size, n);
    show_stacks(stacks, size);
    cout << "\nThere are " << count_stacks(stacks, size) << " stacks." << endl;
    bubble_sort(stacks, size); 
    cout << "\nAfter sorting, the stacks are: " << endl;
    show_stacks(stacks, size);	//show sequence of stacks
    cout << "\nBulgarian Move: " << endl;
    bulgarian_move(stacks, size);
    cout << endl;
    cin.clear();
    }
    while(cin.get()!= 'A');
  return 0;
}

void build_stacks(unsigned int stck[], const int size, int n)
{
  int i = 0;
  while(n>0)
  {
    stck[i] = rand()%n + 1;
    n -= stck[i];
    ++i;
  }
}

void show_stacks(unsigned int stck[], const int size)
{
  int i = 0;
  //cout << endl;
  while(stck[i] != 0)
  {
    cout << stck[i] << " ";
    ++i;
    if(i%80==0) cout << endl;
  }
}

unsigned int count_stacks(unsigned int a[], const int size)
{
  unsigned int sum = 0;
  for(int i = 0; i < size; ++i)
    if(a[i] != 0) ++sum;
  return sum;
}

void bubble_sort(unsigned int a[], const int size)
{
  for(int i = 1; i < size; ++i)
  for(int j = 1; j <= size-i; ++j)
    if(a[j-1]<a[j])
    {
      swp(a[j-1],a[j]);
      //show_stacks(a, size); //debug
    }
}

void swp(unsigned int & x, unsigned int & y)
{
  unsigned int temp = x;
  x = y;
  y = temp;
}

void bulgarian_move(unsigned int stcks[], const int max_size)
{
  int k = 0;
  int i = 0;  //This is where I want the second array
  int a[i] = {0};
  do
  {
    int i = 0;
    int sum = 0;
    while(stcks[i] != 0)
    {
      stcks[i] -= 1;
      sum += 1;
      ++i;
    }
    stcks[sum] = 0;
    while(stcks[i] >= 0)
    {
      if(stcks[i] == 0)
      {
        stcks[i] = sum;
        break;
      }
    }
    bubble_sort(stcks, max_size);
    int j = 0;
    while(stcks[j] != 0)
    {
    cout << stcks[j] << " ";
    ++j;
    if(j%80==0) cout << endl;
    }
    cout << endl;
    ++k;
  }
  while(k != 10);
}


Why can I not declare a second array in the function bulgarian_move? I am trying to place the values of the stacks array into a second array and compare the two to see if the values repeat.
What' the error you're getting?
I think because you are trying to create an array with "0" elements.
Last edited on
that is the error I am getting. I wanted the array to be blank so I could fill it will potentially an infinite number of numbers.
You should read more on arrays. In C++ you can't create an array with "blank" parameters unless you initialize it right away. You can't do something like this:
 
 int myArray[];
. If you want the size of the array to change you should try using vectors.
Like Hekri said, vectors are one storage option that can change size at run time, but you can also use new to create a dynamic array. The only problem there is that you would have to determine the size needed before you create the array anyway. Vectors don't suffer this limitation, but if you can ask the user to input how many numbers they will be entering you can use a dynamic array. Vectors require you to learn how to use them, but then that's something you'll have to learn anyway so it's up to you.
Last edited on
Topic archived. No new replies allowed.