Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. consider a 12 * 12 square array( in form of a matrix). we are interested in the intersection of rows and columns in the

a.

consider a 12 * 12 square array( in form of a matrix).

we are interested in the intersection of rows and columns in the secondary diagonal

i. explain how you might go about finding the sum of elements in the secondary diagonal

ii. assuming you are constrained to using a single iteration to transverse secondary diagonal, give a pseudo code that might reasonably be implemented using a program code in C++ , to find the sum of the elements.

iii. give a general formula that can be used to find the sum of the elements in the secondary diagonal of any square array irrespective of the number of equal rows and columns.

b.

consider this array

double randomDoubles[] = {3.0, 5.0, 7.0, 9.0, 11.0, 2.5, 5.8, 12.0, 15.0, 18.0, 25.1, 27.19, 33.45};

Also consider the following sequence of code, executed consecutively, that generates the indicated outputs:

CODE 1:

int numberOfDoubles = sizeof(randomDoubles_/ sizeof (randomDoubles[0]);

//

cout << "Number of Doubles is " << numberOfDoubles << endl;

output for code 1 : Number of Doubles is 13

CODE 2:

double* ptr_randomDoubles = &randomDoubles[0];

//

// Code fragment to retrieve 1st element

double firstDouble = *ptr_randomDoubles;

cout << "1st Double is " << firstDouble << endl;

output for code 2: 1st Double is 3

CODE 3:

// Code fragment to retrieve 2nd element

double secondElement = *(++ptr_randomDoubles);

cout << "2nd Double is " << secondElement << endl;

output for code 3: 2nd double is 5

CODE 4:

// code fragment to rerieve 2nd to last element (i.e 27.19)

double lastButOneElement = *ptr_randoDoubles + numberOfDoubles - 2;

cout << "last but 1 Double is " << lastButOneElement << endl;

output: last but 1 Double is 16

the mistake occurs at the output of code 4 because it produces an invalid output because the array does not contain an element value 16.

I.Explain why the element value 16 is invalid and what you might need to do to enable code Block 4 to retrieve the correct element value of 27.19.Write out what you did to fix the problem.

II.If it is your determination that the fix you made in A does retrieve the element value 27.19, then do NOTHING. Otherwise, explain what you might do to fix any of the code Block 2, 3 and 4 to assure that:

Code Block 2 retrieves the element value 3

Code Block 3 retrieves the element value 5 and

Code Block 4 retrieves the element value 27.19

Write out your corrected version of any of the 3 code blocks.

III.Given that at the time of the execution of code Block 2, the address in the variable double* ptr_randomDoubles is 0x00b0f7cc, explain how you might use this address to derive the address of the element value 27.19.

IV.Might you be able to use this address to retrieve the element value 27.19. If so write out the code that you might use to accomplish this.

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

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions