0% found this document useful (0 votes)
42 views1 page

Dynamic Array Creation in C++

This C++ program dynamically allocates memory for an array using pointers, allows the user to enter multiple integer elements, and then prints out the stored values. It first prompts the user to create a new array, allocates memory for it, then enters a loop to accept input elements until the user chooses to exit. Finally, it prints out each element by iterating through the pointers.

Uploaded by

Vijay Kalmath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views1 page

Dynamic Array Creation in C++

This C++ program dynamically allocates memory for an array using pointers, allows the user to enter multiple integer elements, and then prints out the stored values. It first prompts the user to create a new array, allocates memory for it, then enters a loop to accept input elements until the user chooses to exit. Finally, it prints out each element by iterating through the pointers.

Uploaded by

Vijay Kalmath
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 #include<iostream>

2 /* Creating a array dynamically*/


3 using namespace std;
4 int main()
5 {
6 int *a;/* Creating a pointer*/
7 int *c;
8 int b,sz;
9 sz=0;
10 char choice;
11 cout<<"Do you want to create a new array "<<endl;
12 cin>>choice;
13 if(choice=='y' || choice=='Y')
14 a = new int;/* First free memory assigned to the pointer*/
15 else
16 {
17 cout<<endl<<"You are exiting the program";
18 return 0;
19 }
20
21 c=a;
22 while(1)
23 {
24 cout<<"Do you want to add an element to the existing array "<<endl;
25 cin>>choice;
26 if(choice=='y' || choice=='Y')
27
28 {
29 cout<<" Enter the value ";
30 cin>>b;
31 cout<<a<<endl;
32 *a=b;
33 sz++;
34 cout<<*a<<endl;
35 }
36
37 else
38 break;
39 a++;
40
41 }
42 cout<<"Reading it"<<endl;
43 for(int j=0;j<sz;j++)/* Reads array backwards*/
44 {
45 cout<<*c<<endl;
46 c++;
47 /* To read it from start,store starting address in new variable*/
48
49 }
50 }

You might also like