10 Replies - 768 Views - Last Post: 01 February 2017 - 09:12 PM Rate Topic: -----

#1 Scorpion___95   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 11
  • Joined: 01-February 17

I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 05:51 AM

#include<iostream>
using namespace std;

int main(){
	int n = 0;           // no longer const
	int *a = new int[n];  // allocated on heap
	cout << "Enter the size of array : ";
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];// now a points to new array.
	}
	for (int i = 0; i < n; i++) {
		cout<< a[i];// now a points to new array.
	}
	int x = 0;
	n = x;
	int *b = new int[x+1]; // create new bigger array.
	x = x * 2;  
	x++;
	a = b;
	cout << "Enter the size of array : ";
	cin >> x;
	for (int i = n; i < x; i++) {
		cin >> b[i];// now a points to new array.
		b[i] = a[i];       // copy values to new array.
	}
	
	for (int i = n; i < x; i++) {
		cout << b[i];// now a points to new array.
	}
			system("pause");
}

This post has been edited by snoopy11: 01 February 2017 - 11:33 AM
Reason for edit:: added [ CODE ] tags please learn to use them !


Is This A Good Question/Topic? 0
  • +

Replies To: I am making dynamic array and resize it, But I'm stuck. help me.

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 06:07 AM

Hi. Welcome to DIC. In future, please put code tags around your code so it will be properly formatted. (Follow the instructions printed in faint text in the post box.)

For your dynamic array, you need to have the user input the desired size of the array before you allocate it so the new operator can allocate an array of the necessary size.

Also, when you are finished using a dynamically allocated array, you must use the delete[] operator to de-allocate it to avoid a memory leak, e.g., assuming a is a pointer to the array:
delete[] a;

Was This Post Helpful? 0
  • +
  • -

#3 Scorpion___95   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 11
  • Joined: 01-February 17

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 06:29 AM

Bro I do not pick you. Can you explain how to copy.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,947
  • Joined: 08-August 08

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 07:53 AM

We didn't pick you either. Do a search on memcpy, and if you want help there are better ways to ask for it.
Was This Post Helpful? 0
  • +
  • -

#5 Scorpion___95   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 11
  • Joined: 01-February 17

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 11:08 AM

I am working on IDE(visualstudio), malloc is not working.
int *b= malloc(sizeof(int)* 2);
memcpy(a, b, sizeof(int)* 2);

This post has been edited by snoopy11: 01 February 2017 - 11:36 AM
Reason for edit:: added [ CODE ] tags please learn to use them !

Was This Post Helpful? 0
  • +
  • -

#6 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 12:07 PM

If this is C++ don't use malloc use the keyword
new


then
delete
what you
new
.

But FYI you would use malloc in a C++ program like so casting to the pointer type as you go.

int* b = (int*)malloc(sizeof(int)* size_of_array);


remembering to
free
what you
malloc
.

As for memcpy its
memcpy(destination, source, sizeof(destination));


so if your array is of ten elements of type int it would be
memcpy(my_array2, my_array1, sizeof(int)*10);


lastly be descriptive with your variables do not call them a or b it tells you nothing and leads to heavily commented code.

Regards


Snoopy.
Was This Post Helpful? 1
  • +
  • -

#7 Scorpion___95   User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 11
  • Joined: 01-February 17

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 02:18 PM

I am new in programming.I tried this but could not, BRO HOW TO RESIZE DYNAMIC ARRAY. SUPPOSE user entered 2 size, then it's ask user to update size. how to do it.confused.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6834
  • View blog
  • Posts: 28,358
  • Joined: 12-December 12

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 02:31 PM

@OP Stop abbreviating with text-speak and calling everyone bro. You are not texting your mates. Write clearly.
Was This Post Helpful? 0
  • +
  • -

#9 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 02:52 PM

Well telling us in words what your actual assignment is would go along way,

at the moment its hard to give out more help without first knowing what you are trying to achieve.


but..

say you have an array declared as so

int array_size =10;
int* my_array1 = new int[array_size];



and you fill that array...

then you want a bigger size, to add more stuff to it.. say 2 lots of more stuff of type int.

you would copy that array over to another array of equal size to my_array1 call it my_array2

then you would do this...

int* my_array1 = new int[array_size+2]


the contents of my_array1 are now destroyed.

but you can

memcpy(my_array1, my_array2, sizeof(int)*(array_size+2));


and they are back, you can then add more stuff to my_array1.
Was This Post Helpful? 0
  • +
  • -

#10 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 02:59 PM

BROOO read snoopy's post!
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,947
  • Joined: 08-August 08

Re: I am making dynamic array and resize it, But I'm stuck. help me.

Posted 01 February 2017 - 09:12 PM

You could use vectors, or simulate them if they're not allowed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1