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> 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...
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() {...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int...
#include <iostream> #include <fstream> #include <string> using namespace std; const int QUIZSIZE = 10; const int LABSIZE = 10; const int PROJSIZE = 3; const int EXAMSIZE = 3; float getAverage(float arr[], int size) { float total = 0; for (int i = 0; i < size; i++) { total += arr[i]; } return total/size; } // the following main function do.... int main() { ifstream dataIn; string headingLine; string firstName, lastName; float quiz[QUIZSIZE]; float lab[LABSIZE]; float project[PROJSIZE]; float midExam[EXAMSIZE];...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count =...
#include <iostream> #include <iomanip> using namespace std; int main() {     int studentid, numberreverse[20], count = 0, maximum = 0, minimum = 0;     cout << "Enter your student ID number: ";     cin >> studentid;     cout << "Student ID Number = " << studentid << endl;     while (studentid != 0)     {          numberreverse[count] = studentid % 10;          if (count == 0)          {              minimum = numberreverse[count];              maximum = minimum;          }          else...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int...
#include <iostream> #include <string> #include <ctime> using namespace std; void displayArray(double * items, int start, int end) { for (int i = start; i <= end; i++) cout << items[i] << " "; cout << endl; } //The legendary "Blaze Sort" algorithm. //Sorts the specified portion of the array between index start and end (inclusive) //Hmmm... how fast is it? /* void blazeSort(double * items, int start, int end) { if (end - start > 0) { int p =...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const...
#include <iostream> #include <fstream> #include <vector> using namespace std; struct Point{ int x, y; bool operator==(const Point& p2) { return this->x == p2.x and this->y == p2.y; } bool operator!=(const Point& p2) { return this->x != p2.x or this->y != p2.y; } friend ostream &operator<<( ostream &out, const Point &P ) { out << "(" << P.x << ", " << P.y << ")"; return out; } friend istream &operator>>( istream &in, Point &P ) { char d1, d2, d3;...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float...
I need a Flowgorithm chart of this #include using namespace std; int main() {    float CISBook[] = {55.5,35.8,67.5};    float MATHBook[] = {25.5, 54.5};    float MECBook[] = {65.0,75.5,86.8};    float sumCIS=0, sumMATH = 0, sumMEC = 0;              for (int i=0; i<4; i++)    {sumCIS += CISBook[i];}       for (int i=0; i<3; i++)    {sumMATH += MATHBook[i];}       for (int i=0; i<4; i++)    {sumMEC += MECBook[i];}       cout<<"\nTotal for CIS Books:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT