what is the wrong ?

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
# include <iostream>
using namespace std ;

int main ()
{
    char objfun ;
    cout << " Please Enter (X) to maxmization and (N) to minmization : " ;
    cin >> objfun ;
    cout << endl ;
    
    if ( objfun == 'X' || objfun == 'x' )
    {
    int objfunvar , nconstrains ;
    cout << "Please Enter no of variable of objective function : " ;
    cin >> objfunvar ;
    double OBJFUN [objfunvar] ;
    
    for ( int i=0 ; i < objfunvar ; i++ )
    {
        cout << "Please Enter objective function " ;
        cout << "X" << i+1 << " = " ;
        cin >> OBJFUN [i] ;
    }
    double RHS ;
    cout << "Please Enter objective function R.H.S = " ;
    cin >> RHS ;
    
    cout << "Please enter no of constrains : " ;
    cin >> nconstrains ;

    double S[nconstrains] = {0.0} ;
    
    double matrix[objfunvar][nconstrains] ;
    
    for ( int i = 0 ; i < nconstrains ; i++ )
        {
           for ( int j = 0 ; j < objfunvar ; j ++ )
           {
            cout << " enter X ( " << j+1 << " ) " ;
            cin >> matrix[i][j] ;
           }
         }
    double rhs[nconstrains] ;
    
    for ( int i= 0 ; i < nconstrains ; i++ )
    {
        cout << "Enter R.H.S for constrain " << i+1 <<" : " ;
        cin >> rhs[i];
    }

    cout << endl << endl << " This is the Formulation u entered " << endl << endl ;
    
    for ( int i=0 ; i < objfunvar ; i++ )
    {
        cout << OBJFUN[i]<< " x" << i+1 << " + " ;
    }
    cout << " = " << RHS << endl ;
    
    for ( int i = 0 ; i < nconstrains ; i++ )
        {
           for ( int j = 0 ; j < objfunvar ; j ++ )
           {
            cout << matrix [i][j] << "X" << j+1 << "+"   ;
           }
           cout << " <= " << rhs[i] << endl ;
         }


}
   
    system("pause");
    return 0 ;
}
what is the wrong in line 31

double S[nconstrains] = {0.0}
You can't use a nonconst variable to create an array.

1
2
int size = 20;
int array[size];  // can't do this 


If you need the array to be dynamically sized, you can use a vector instead:

1
2
3
// double S[nconstrains] = {0.0} ; //  bad

vector<double> S(nconstrans, 0.0);  // good 
As Disch said, you can't do that because the compiler needs to know how large the array will be to allocate enough memory for it.

You could also dynamically create the array:

1
2
3
4
5
6
7
8
9
    std::cout << "Enter a number: ";
    int num;
    std::cin >> num;
    std::cin.ignore();
    
    int *ptr_array = new int[num];
    
    std::cin.get();
    return 0;


But using the vector container is probably better.
if you use new[] remember to delete[]
Topic archived. No new replies allowed.