Q about array length.

Hi, I am trying to make an array that forks for any sized array. So i tried that i start the code that the user get an input what is x, than that is assign to the array valeu, and after that is called the function array.

My Q is that completly wrong? and if its wrong why its execute under linux and not execute under windows.
1
2
3
4
5
int x;
cin >> x;
cout << x << endl;
int value [x];
array(value,x);

complete 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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

void printarray (int arr[], int length) {
    cout << "{";
  for (int n=0; n<length; n++){
    if (n != 0){
        cout << ", ";
        }
    cout << arr[n];
  }
    cout << "}";
    cout << endl;
}
void bubblesort(int arr[], int n) {
    int i,j;
    int temp;
     for(i=0;i<=n-1;i++){
		for(j=i+1;j<=n;j++){
			if(arr[i] > arr[j]){
				temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
}
int sum (int arr[], int n){
    int sum=0;
    for (int i=0;i<n;i++){
        sum += arr[i];
    }
    return sum;
}
int array(int arr[],int n){
    srand(time(NULL));
    for (int i = 0; i < n; i++){
        arr [i] = rand () % 100;  
    }
}

int main (){
  int x;
  cin >> x;
  cout << x << endl;
  int value [x];
  array(value,x);
  cout << "original array " << endl;  
  printarray (value,x);
  cout << endl;
  bubblesort (value,x-1);
  cout << "sorted array " << endl;
  printarray (value,x);
  cout << "small: " << value[0] << endl;
  cout << "bigg: " << value[x-1] << endl;
  cout << "sum: ";
  cout << sum (value,x) << endl;
  cout << "avarage: ";
  cout << (sum (value,x))/x << endl;
}
Last edited on
I haven't worked on Linux, but as for my little experience on Windows, I know that when you use static memory for an array, it is important the array size to be constant. When you want an array with indefinite size you should use dynamic memory.
@heliusa
ty for reply, i am just learning so i didnt get yet to dynamic arrays.
I'm learning, too. I don't have much experience and this is the reason I did't give you an example because I'm sure for the theory only and don't want to give you wrong advise :)
variable length arrays are not allowed in C++ standard. For this, use std::vector

The difference you are observing does not lie in the differences in Linux and Windows but in compiler between GCC and MSVC++.

This type of code is legal and allowed in C (as of C99). It's not, however, allowed in C++ -- but gcc provides it as an extension in C++ anyway. In this particular respect, MS VC++ attempts to follow the (C++) standard more closely, and does not provide this particular extension.

If you run gcc on Windows, however, (e.g., Cygwin or MinGW) you'll get the same behavior on Windows that you're currently observing on Linux.
@shadowCODE
Ty you for your answer and explanation.
Topic archived. No new replies allowed.