Dec 18, 2012 at 3:52pm UTC
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:
Last edited on Dec 18, 2012 at 3:59pm UTC
Dec 18, 2012 at 3:59pm UTC
@ 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 Dec 18, 2012 at 4:02pm UTC
Dec 18, 2012 at 4:00pm UTC
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 Dec 18, 2012 at 4:01pm UTC
Dec 18, 2012 at 4:02pm UTC
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 Dec 18, 2012 at 4:10pm UTC
Dec 18, 2012 at 4:30pm UTC
i think its poorly indented and you are using namespace std
Dec 18, 2012 at 4:30pm UTC
I don't think that would compile
Dec 18, 2012 at 4:32pm UTC
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 Dec 18, 2012 at 4:34pm UTC
Dec 18, 2012 at 4:38pm UTC
@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?
Dec 18, 2012 at 4:45pm UTC
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 Dec 18, 2012 at 4:46pm UTC
Dec 18, 2012 at 4:52pm UTC
@ 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 Dec 18, 2012 at 5:03pm UTC