Question

In: Computer Science

#include using namespace std; int menu(){    int x;    cout<<"1.Add Entry\n";    cout<<"2.Edit Entry\n";   ...

#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

Solutions

Expert Solution

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;
}


Related Solutions

#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y);...
#include <iostream> #include <string> #include <array> #include <vector> using namespace std; void f(int x, int &y); int main(){ char s1[] = "zoey"; char s2[] = "zoey"; string s3 = s1; string s4 = s2; cout << "1. sizeof(s1) = " << sizeof(s1) << endl; if(s1 == s2) cout << "2. s1 == s2 is true" << endl; else cout << "2. s1 == s2 is false" << endl; if(s3 == s4) cout << "3. s3 == s4 is true" <<...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1;...
#include <iostream> using namespace std; int main() {     int hour;     int min;     for (hour = 1; hour <= 12; hour++)     {         for (min = 0; min <= 59; min++)         {             cout << hour << ":" << min << "AM" << endl;         }     }       return 0; } 1.      Type in the above program as time.cpp. Add a comment to include your name and date. Compile and run. 2.      What is the bug or logic error in the above program? Add the...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){...
Write the pseudocodes for these programs: a) #include <iostream> using namespace std; void print(int arr[],int n){ if(n==0) return; cout<<arr[n-1]<<" "; print(arr,n-1); } int main() { int n; cout<<"Enter the size of the array:"; cin>>n; int arr[n]; int i; cout<<"Enter the elements of the array:"<<endl; for(i=0;i<n;i++){ cin >> arr[i]; } cout<<"Displaying the elements of the array in reverse order:"<<endl; print(arr,n); return 0; } b) #include<iostream> #include<fstream> using namespace std; //student structure struct student { int id; float gpa; }; //node structure...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int...
#include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() {        const int SIZE = 20;     char str[SIZE];     char str1[SIZE];     int n;     int k =1;        printf("Enter a word: \n");     fgets(str,SIZE,stdin);     printf("Enter another word: \n");     fgets(str1,SIZE,stdin);        if (str1[strlen(str1) - 1] == '\n')     {         str1[strlen(str1)-1] = '\0';     }     if (str[strlen(str) - 1] == '\n')     {         str[strlen(str)-1] = '\0';     }      ...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)...
#include<vector> #include<iostream> using namespace std; void println(const vector<int>& v) {    for (int x : v)        cout << x << " ";    cout << endl; } void println(const vector<string>& v) {    for (const string& x : v)        cout << " "<< x << " ";    cout << endl; } int main() {    vector<int> v0;    cout << "An empty vector of integers: ";    println(v0);    vector<int> v1(3);    cout << "A...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice;...
Add File I/O to the voting program below #include<iostream> using namespace std; int main() {int choice; int biden = 0 , trump = 0 , bugs = 0 ; int vc = 0 ; do { cout<<"\n\n\nEVOTE\n-----" <<"\n1.Joe Biden" <<"\n2.Donald Trump" <<"\n3.Bugs Bunny" // 4. Print current tally [hidden admin option] // 5. Print audit trail [hidden admin option] // 6. mess with the vote [hidden hacker option] E.C. // 7. END THE ELECTION <<"\n\n Your selection? "; cin >>...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int...
code from assignment 1 #include #include using namespace std; const int nameLength = 20; const int stateLength = 6; const int cityLength = 20; const int streetLength = 30; int custNomber = -1; // Structure struct Customers { long customerNumber; char name[nameLength]; char state[stateLength]; char city[cityLength]; char streetAddress1[streetLength]; char streetAddress2[streetLength]; char isDeleted = 'N'; char newLine = '\n'; int zipCode; }; int main() { ofstream file; file.open("Customers.dat", fstream::binary | fstream::out); char go; long entries = 0; struct Customers data; do...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char...
***Convert the C++ to Python*** #include <iostream> using std::cout; using std::cin; using std::endl; int charClass; char lexeme[100]; char str[200]; char nextChar; const int LETTER = 0; const int DIGIT   = 1; const int UNKNOWN = -1; const int OPAREN = 2; const int CPAREN = 3; const int PLUS = 4; const int MINUS = 5; const int MUL = 6; const int DIV = 7; const int ID_CODE = 100; const int PLUS_CODE = 101; const int MINUS_CODE =...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" <<...
#include <iostream> using namespace std; double print_instructions() { cout << "WELCOME TO BandN book stores" << endl; cout << "Today we have a deal on e-books. Customers will receive a discount off their entire order.." << endl; cout << "The discount is 15% off your total order and cost per book is 8.99." << endl; cout << "Customers who buy 20 or more e-books will receive 20% off instead." << endl; cout << endl; return 0; } int no_of_books() {...
Fill in the code only using pointer variables #include using namespace std; int main() { int...
Fill in the code only using pointer variables #include using namespace std; int main() { int longside; // holds longside (length) int wideside; // holds wideside(width) int total; // holds total (area) int *longsidePointer = nullpointer; // int pointer which will be set to point to length int *widthPointer = nullpointer; // int pointer which will be set to point to width cout << "Please input the longside of the rectangle" << endl; cin >> longside; cout << "Please input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT