Array initialization

Is there a way to initialize an array according to user input?

Array lenght?




1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
using namespace std;

int main(){
int cells;

cout << "Array lenght?" << endl;
cin >> cells;

int T[cells]={};

}


I can´t compile this, so, is there another way or I HAVE to initialize an array with a constant value???

Thanks in advance!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

int main()
{
	int size;
	
	std::cin >> size;
	int *A = new int[size];

	for(int i=0; i<size; ++i)
	{
		A[i] = i;
	}

	for(int i=0; i<size; ++i)
	{
		std::cout << A[i] << std::endl;
	}
	delete[] A;
}


input:3
output:
0
1
2
Last edited on
@ Darkmaster: Nice trick...
Ok, so just by creating it in the heap I can add "places" to my pointer A and therefore act as if I had a variable size array?

Anyway, I tried that and I got:

expected primary-expression before ']' token|
||=== Build finished: 1 errors, 0 warnings ===|

Then I erased []

int *A = new int[];

As if reserving only one variable, and it worked...
Last edited on
changed it, now it works the way you wanted

simply mixed up the order and forgot the delete at the end :D

but yeah that "trick" works as long as the memory isn't used by anything else
Last edited on
closed account (D80DSL3A)
Does that really work? Line 6 doesn't look right, and you should wait until after the user has entered the size before creating the array. Also, the memory should be freed in the end.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>

int main()
{
	int size;	
	std::cin >> size;
        int *A = new int[size];

	for(int i=0; i<size; ++i)
	{
		A[i] = i;
	}

	for(int i=0; i<size; ++i)
	{
		std::cout << A[i] << std::endl;
	}

        delete [] A;
}

EDIT: I was responding to Darkmasters code as it appeared several minutes ago.
Last edited on
works, just run it
Thank you!
And what do you think of doing this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
using namespace std;

int main(){
int size;
cout << "size?" << endl;
cin >> size;

typedef int array[size];

array myArray;
for (int i = 0; i < size; i++){
    myArray[i] = i + 1;
    }

cout << '|' ;
for ( int i = 0; i < size; i++){
    cout << ' ' << myArray[i] ;
    }
cout << '|' << endl;
}
i think its poorly indented and you are using namespace std
I don't think that would compile
wont compile since the arrray isn't a pointer

@ fun2code: look at the post above yours. that's excactly what i changed
Last edited on
@Darkmaster: how should I indent it? What´s wrong with namsepace std? I mean, if you really want to teach a noob!

@maeriden: it does compile, why do you think it would not?
you write
1
2
3
4
int main(){
int size;
...
}


i would write
1
2
3
4
5
int main()
{
      int size;
      ...
}

that way you see the begging and end of a function. this gets helpful if you have loops inside loops and if statements inside and ...
to give you an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	for(int i=0; i<10; ++i)
	{
		for(int j=0; j<10;++j)
		{
			if(j==i)
			{
				...
			}
		}
	}
}

is just soooo much easier to read than
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
for(int i=0; i<10; ++i)
{
for(int j=0; j<10;++j)
{
if(j==i)
{
...
}
}
}
}




and it doesn't compile, since your arrays size is not constant
Last edited on
@ Darkmaster: I see, thanks, I´ll use your advice.

So now I´ve come to this point where the user can create bidimensional arrays according to his/her needs, thanks to typedef. Do you see any problems that could come up with memory usage?

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

int main(){
   int m, n;
   cout << "m? n?" << endl;
   cin >> m >> n;

   typedef int matrix[m][n];

   matrix myMatrix;

   for (int i = 0; i < m; i++)
   {
       for (int j = 0; j < n; j++)
      {
            myMatrix[i][j] = 0;
      }
   }

   for (int i = 0; i < m; i++)
   {
       cout << '|';
       for (int j = 0; j < n; j++)
       {
           if (myMatrix [i][j]< 10) cout << ' ';
           cout << myMatrix[i][j] << ' ';
       }
       cout << '|' << endl;
   }
}



m? n?

2 3

| 0  0  0|
| 0  0  0|

Last edited on
Topic archived. No new replies allowed.