Question
a. Rewrite the following function so that it returns the same result, but does not increment the variable ptr. Your new program must not use
a. Rewrite the following function so that it returns the same result, but does not increment the variable ptr. Your new program must not use any square brackets, but must use an integer variable to visit each double in the array. You may eliminate any unneeded variable.
double computeAverage(const double* scores, int nScores)
{
const double* ptr = scores;
double tot = 0;
while (ptr != scores + nScores)
{
tot += *ptr;
ptr++;
}
return tot/nScores;
}
b. Rewrite the following function so that it does not use any square brackets (not even in the parameter declarations) but does use the integer variable k. Do not use any of the functions such as strlen, strcpy, etc.
// This function searches through str for the character chr.
// If the chr is found, it returns a pointer into str where
// the character was first found, otherwise nullptr (not found).
const char* findTheChar(const char str[], char chr)
{
for (int k = 0; str[k] != 0; k++)
if
(str[k] == chr)
return &str[k];
return nullptr;
}
c. Now rewrite the function shown in part b so that it uses neither square brackets nor any integer variables. Your new function must not use any local variables other than the parameters.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started