1

Program

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main(int argc, char** argv)  
{
    if(argc < 2)
        exit(1);
    cout<<strtof(argv[1],NULL);
    int SIZE = (int) (strtof(argv[1],NULL)/8);
    int **arr = new int[SIZE][SIZE*4]();

    for(int i = 0; i < SIZE; i++) {
        for(int j = 0; j < SIZE*4; j++) {
            cout<<arr[i][j];
        }
    }

    return 0;
}

Input

32

Error

prog.cpp: In function ‘int main(int, char**)’: prog.cpp:13:39: error: array size in new-expression must be constant
     int **arr = new int[SIZE][SIZE*4]();
                                       ^
prog.cpp:13:39: error: the value of ‘SIZE’ is not usable in a constant expression prog.cpp:12:9: note: ‘int SIZE’ is not const
     int SIZE = (int) (strtof(argv[1],NULL)/8);
         ^~~~

Is there any other way, rather than using malloc or calloc to do this in C++11?


Code in Ideone

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Tarun Maganti
  • 3,076
  • 2
  • 35
  • 64

1 Answers1

0

If you really don't want to use vector, then maybe this working code can help.

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

int main(int argc, char** argv)  
{
    if(argc < 2)
        exit(1);
    cout<<strtof(argv[1],NULL);
    int SIZE = (int) (strtof(argv[1],NULL)/8);
    int** arr = new int*[SIZE];

    for(int i = 0; i < SIZE; i++) {
        arr[i] = new int[ 4* SIZE];
    }

    for(int i = 0; i < SIZE; i++) {
        for(int j = 0; j < SIZE*4; j++) {
            cout<<arr[i][j];
        }
    }

    return 0;
}
v78
  • 2,803
  • 21
  • 44