Thursday, August 20, 2009

How to extend an array. C++

This post is mainly about the C++ exercise. I think a lot of my classmates might need help on Q3 of the exercise where the teacher will give bonus if we successfully done it.

I don't know if i do it correctly but i can run my program smoothly and there is no error. But the way i do it is correct or not, i am not sure. So, if you feel that my way is wrong, then don't continue to read my post.

Actually it is kind of easy.

First, declare an array with only 1 length.

eg: int a[1];

Second, get an input from user and store it to the first memory space of the array.

eg: cin>>input1;
a[0] = input1;

Third, get another input that u need to store it into the array too

eg: cin>>input2;

Fourth, store it directly into the second memory space of the array.

eg: a[1] = input2;

I don't know if it is a bug or what but i tried and i can use this way. Even if i don't do the correct array declaration, i still can store it directly.

Actually, i first think of re declaring the same array variable and copy all the previous data into it through pointer. Maybe it will work too but i haven't tried. Maybe i should get some advice from C++ pro.

Well, i hope this can help anyone who read my post.... Haha.

1 comment:

  1. hey man, you are so kind, thanks for your sharing~ anyway i still didn't do that exercise~ lol

    ReplyDelete