Stack and the heap Solved

15.00 $

Category:
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (1 vote)
  • Explain the difference between the stack and the heap. Give a code example of malloc and free. List some common memory-related bugs in C programs.

 

  • Given the following C++ code, improve the code by eliminating length calls out of the loop, put most used variables first when initializing variables, use prefix operators rather than postfix operators, loop unrolling, and any other improvements you would like to make to improve performance. Please note, the order of the output is not important, all data should be output. 

    #include <iostream>

    #include <vector>

    #include <string>

    using namespace std;

    int main(int argc, char* argv[]) {

    double sum=0;

    vector<double> price;

    vector<string> game;

    vector<string> designer;

     

    push_back(53.41);

    game.push_back(“Carcassone”);

    designer.push_back(“Wrede”);

    price.push_back(46.51);

    game.push_back(“Agricola”);

    designer.push_back(“Rosenberg”);

    price.push_back(31.02);

    game.push_back(“Puerto Rico”);

    designer.push_back(“Seyfarth”);

     

    for(int i=0;i<game.size();i++)

    {

    cout<<game[i]<<“\t”<<endl;

    }

    cout<<endl;

    for(int i=0;i<designer.size();i++)

    {

    cout<<designer[i]<<“\t”<<endl;

    }

    cout<<endl;

    cout<<price[0]<<“\t”<<endl;

    cout<<price[1]<<“\t”<<endl;

    cout<<price[2]<<“\t”<<endl;

    sum=price[0]+price[1]+price[2];

    cout<<“The total price for all games is “<<sum<<endl;

    cin.ignore();

    return 0;

    }

  • Questions-2_sol.zip