# Week 4 Homework Optimizing Program Performance Solved

20.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

## Description

A programmer must write correct code that is clear and concise. There are also circumstances in which a programmer must write fast and efficient code. Processing video frames in real time must be fast. We will talk about ways to optimize code.

Given the following code, perform these operations to optimize the code. See Chapter 5 in the book for more details on code optimization. Please use comments to document all optimizations you have made to the code.

1.      Using switch instead of if

2.      Eliminating length calls out of the loop test

3.      Put the most used variables first when initializing variables

4.      Use prefix operations rather than postfix operations

5.      Loop unrolling—increase the number of elements computed in each iteration of a loop (i.e. instead of processing arrays separately, if you have two arrays of the same length, process them in parallel)

6.      Any other improvements you want to make

#include <iostream

#include <vector

#include <string

using namespace std;

#include <iostream

#include <vector

#include <string

using namespace std;

int main()

{

//This program stores the items purchased at the grocery store. The price vector stores the prices for each item purchased.

//The product name vector stores the products purchased and the category vector stores which category the item falls under.

//Frozen foods have a 10% discount, snacks has a 5% discount, and produce has a 15% discount.

//The total amount of items purchased should be calculated with a 7% tax rate.

double sum;

double tax,totalAmount;

vector<double price;

vector<string productName;

vector<char category;

price.push_back(4.5);

price.push_back(10);

price.push_back(1.25);

price.push_back(2.75);

price.push_back(9.50);

productName.push_back(“ice cream”); //Frozen food

productName.push_back(“pizza”);  //Frozen food

productName.push_back(“apples”); //Produce

productName.push_back(“cheesy poofs”); //Snacks

productName.push_back(“bananas”); //Produce

category.push_back(‘F’);

category.push_back(‘F’);

category.push_back(‘P’);

category.push_back(‘S’);

category.push_back(‘P’);

if (category==’F’)

{

price=price*.9;

}

else if (category==’S’)

{

price=price*.95;

}

else

{

price=price*.85;

}

if (category==’F’)

{

price=price*.9;

}

else if (category==’S’)

{

price=price*.95;

}

else

{

price=price*.85;

}

if (category==’F’)

{

price=price*.9;

}

else if (category==’S’)

{

price=price*.95;

}

else

{

price=price*.85;

}

if (category==’F’)

{

price=price*.9;

}

else if (category==’S’)

{

price=price*.95;

}

else

{

price=price*.85;

}

if (category==’F’)

{

price=price*.9;

}

else if (category==’S’)

{

price=price*.95;

}

else

{

price=price*.85;

}

sum=price+price+price+price+price;

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

{

cout<<“The price for item “<<(i+1)<<” is “<<price[i]<<endl;

tax=sum*.07;

totalAmount = sum+tax;

}

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

{

cout<<“The product name for item “<<(i+1)<<” is “<<productName[i]<<endl;

}

cout<<“The total price is: “<<totalAmount<<endl;

cin.ignore();

return 0;

}

• Program-Optimization.zip