<p>Please utilize C++ to answer the questions</p> <p> 1. Assume that an arr
ID: 3626565 • Letter: #
Question
<p>Please utilize C++ to answer the questions</p><p> 1. Assume that an array of integers named  a that contains exactly five elements has been declared and initialized.</p>
<p> Write a single statement that assigns a new value to the first  element of the array. This new value should be equal to twice the value stored in the last  element of the array.</p>
<p> Do not modify any values in the array other than the first element.</p>
<p>2. An vector of  int named  a that contains exactly five elements has already been declared and initialized. In addition, an  int variable  j has also been declared and initialized to a value somewhere between 0 and 3.</p>
<p><br /> Write a single statement that assigns a new value to the element of the vector indexed by  j . This new value should be equal to twice the value stored in the next element of the vector (i.e. the element after the element indexed by  j ).</p>
<p><br />Please be sure not to modify any other elements of the vector. Thanks</p>
Explanation / Answer
1. a[0] = 2 * a[ sizeof(a)/sizeof(int) - 1] The sizeof (a) finds the total size of your array in bytes, and then subtracts it by the type of each value in the array - in this case, int, so that you can find the number of elements in the array. The -1 part gets you the last element, since arrays start at 0 (this is called zero indexing) 2. a[j] = 2*a[j+1] I'm pretty sure you can use the [] syntax with vectors, but I'm not completely sure. But anyways, the logic should be sound.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.