In: Computer Science
#include
using namespace std;
int menu(){
int x;
cout<<"1.Add Entry\n";
cout<<"2.Edit Entry\n";
cout<<"3.remove Entry\n";
cout<<"4.print Entry\n";
cout<<"5.Close Console\n";
cout<<"Enter Your Choice:-";
cin>>x;
return x;
}
int main()
{
int x;
int entry[1000];
int entnum = 0;
int num = 0;
int nom =0;
while (1)
{
int choice =
menu();
if (choice == 1){
cout<<"A entry " << entnum<<" has been created
\n";
}
else if
(choice==2){
cout<<"Assign a new number to your entry:-";
cin>>num;
entry[entnum] = num;
entnum = entnum +
1;
}else if(choice== 3){
entry[entnum] =
0;
cout<<"the
last entry has been removed\n";
entnum = entnum
- 1;
}else if(choice ==4){
cout<<"Please Enter Your Entry Number:-";
cin>>
nom;
cout<<"Your Output is:- "<
}else if (choice ==5){
break;
}else{
cout<<"Incorrect input Please Try Again!!!"
}
num = 0;
}
return 0;
}
The code is done by using predefined array. Please edit it for dynamic memory allocation and when dynamic allocation has been used, it will definitely be different than existing code/output and that is what I want. The given code is just the basic structure. thanks
Heap Memory:
Heap memory is allocated or deallocated manually by the programmer but it is not managed automatically. This is a hierarchical data structure type memory that is used to store the global variable in the programming language.
Heap memory can be resized, with no limitation on memory size ad it supports dynamic memory allocation.
Dynamic Memory Allocation
Memory management is a technique in which we provide a way to dynamically allocate the memory to the program. Heap memory allows the dynamic memory allocation, access variable globally and the already declared variable can be resized.
Dynamic array is an array that expands automatically when you will add more elements.
The system calls for heap memory are given below:
malloc()
calloc()
realloc()
free()
The given source program by using the dynamic array is given below:
#include<iostream>
using namespace std;
int menu(){
int x;
cout<<"1.Add Entry\n";
cout<<"2.Edit Entry\n";
cout<<"3.remove Entry\n";
cout<<"4.print Entry\n";
cout<<"5.Close Console\n";
cout<<"Enter Your Choice:-";
cin>>x;
return x;
}
int main()
{
int x;
//int entry[1000];
//allocate memory dynamically by using malloc()
method
int *entry = (int *)malloc(1000 * sizeof(int));
int entnum = 0;
int num = 0;
int nom =0;
while (1)
{
int choice = menu();
if (choice == 1){
cout<<"A entry " << entnum<<" has been created
\n";
}
else if (choice==2){
cout<<"Assign a new number to your entry:-";
cin>>num;
entry[entnum] = num;
entnum = entnum + 1;
}else if(choice== 3){
entry[entnum] = 0;
cout<<"the last entry has been removed\n";
entnum = entnum - 1;
}else if(choice ==4){
cout<<"Please Enter Your Entry Number:-";
cin>> nom;
cout<<"Your Output is:- "<<"" ;
}else if (choice ==5){
break;
}else{
cout<<"Incorrect input Please Try Again!!!";
}
num = 0;
}
//delete the dynamically allocated memory
free(entry);
return 0;
}