Need help optimizing case 1. Language is C++ // Program id: CSLab08.txt // This
ID: 3854890 • Letter: N
Question
Need help optimizing case 1. Language is C++
// Program id: CSLab08.txt
// This is our template for CS Lab 08
// Efficiency practice program
// This document has three cases that represent portions of programs
// that need some help; they are inefficient.
// Study the code and rewrite or remove portions of the code
// to make it more efficient. Your goal should be to decrease the number
// of units (operations) by as much as possible.
// We will assume a fictional chip where everything takes one cycle.
// UNITS:
// Mathematics (1 cycle)
// Assignment (1 cycle)
// Logical operations such compare operations, etc… (1 cycle)
// Return (1 cycle)
// Hint: simply update this code for your answer by removing
// the operations (code) that you find to be inefficient (unnecessary)...
//
//
// CASE 1
// Assume all variables and constants were defined, etc.
// This should be an easy one (short and sweet too)
for (i = 0; i < max; i++)
myArray[i] = myArray[i] + sqrt(PI/2);
Explanation / Answer
Answer:
The given code is
for(i=0;i<MAX;i++)
myArray[i]=myArray[i]+sqrt(PI/2);
We can rewrite the code as
for(i=0;i++<MAX;)
myArray[i]=myArray[i]+sqrt(PI/2);
We can eliminate the updation section of the for loop by writing it in the condition section itself.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.