Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Rewrite the following function so that it returns the same result, but does not increment the variable ptr. Your new program must not use anysquare

Rewrite the following function so that it returns the same result, but does

not increment the variable ptr. Your new program must not use anysquare 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 integervariable 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(intk=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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What is the principle of thermodynamics? Explain with examples

Answered: 1 week ago