The following C program for a real-time system is not optimized. Optimize the co
ID: 3599481 • Letter: T
Question
The following C program for a real-time system is not optimized. Optimize the code using software optimization techniques. Correct as many program deficiencies as you can find and rewrite the corrected program in your answer booklet. short Function( short someArg); short someFunction( short someArg); return x +3 int main( void) int a, b; short result; theArg 20; theArg someFunction( theArg) result- Function( theArg ); short Function( short someArg) short i, k, r; r=r*9 r=r*r; for 1-0; i 4; i++) k-60; return r;Explanation / Answer
Code:
#include<stdio.h>
short Function(short someArg);
//short someFunction(short someArg){
// return x+3;
//}
//the above code is not efficient hence it is commented. it is not efficient
// as it contains only one return statement with value increment
// instead I did the same computation instead of the above function call
int main(void){
int a,b;
short result;
//previously in the below line datatype is not mentioned . it is added now
int theArg=20;
// theArg=someFunction(theArg);
//as explained previously I'm doing the computation below instead of function call
theArg = theArg + 3; // increment argument by 3
result = Function(theArg);
printf("%d",result);
}
short Function(short someArg){
short i,k,r;
//in the below 3 commented lines same variable r is set using assignment statements 3 times with simple calculations which consumes time
// so make it as a two line calculation
//r=4*6;
//r=r*9;
//r=r*r;
//below is the optimized assignment
// since above is haiving 4*6*9
//then r = 216,assign it directly
r=216;
//now perform square of variable r
r=r*r;
// variable k is moved below after removing loop invariant
k=60;
for(i=0;i<4;i++){
//k=60; // this line contains a variable which is not used in the loop . it is being set to 60 for all the loop iterations
// it is called loop invariant. even k is not used outside the loop so better remove it to make code efficient or move the variable outside loop
r=r+(i*someArg);
}
return r;
}
Explanation:
please find my explanation in the comments of the code