C++ Suppose arr is an array of ints initialized to contain [ 6, 4, 2, 1, 3, 5, 7
ID: 3678199 • Letter: C
Question
C++
Suppose arr is an array of ints initialized to contain [ 6, 4, 2, 1, 3, 5, 7, 8 ] What are the contents of arr after the following code executes? Explain/show your work.
What will the contents of the array a be after the following code fragment is executed? Explain/show your work.
Suppose that Widget is a structure that has already been defined and that InventoryItem is the following structure:
Write a function named stockSwap that takes two parameters of type InventoryItem and swaps the widgetPtr fields of the two parameters. Note: your function should only swap pointers (i.e., you are swapping Widget *widgetPtr not Widgets).
Given the following structure:
Complete the following setColor function. This function takes an array of pointers to Colors and initializes the array at the given index to be a new Color with the givenred, blue, and green values. (Note: your setColor function does not need to do any error-checking of parameter values.)
Suppose we have a singly linked list (i.e., chain of linked nodes) that holds characters defined as follows:
and that head is pointing to a chain of nodes containing 'a', 'b', 'c', 'd', 'e' (in that order). Write a code fragment that moves the node containing 'd' to the beginning of the chain. That is, initially we have
and after the code fragment executes, we have
Make sure your code moves the node itself (and not just the value within the node).
Given the following code:
What is printed when the testIt() function is executed? Explain/show your work. Hint: draw the memory diagram.
Explanation / Answer
Please find the answers below :
1)
#include <iostream>
#include <string>
using namespace std;
int main()
{
int arr[8] = {6, 4, 2, 1, 3, 5, 7, 8 };
for (int i = 0; i < 8; i++)
*(arr + i) = *arr + i;
for (int i = 0; i < 8; i++)
cout << arr[i] << ", " << flush;
}
-------------
output :6, 7, 8, 9, 10, 11, 12, 13,
-------------------------------------------------------------------------
2)
code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int *p, a[7] = {1, 2, 3, 4, 5, 6, 7};
p = a;
p++;
p[4] = 8;
for (int i = 0; i < 7; i++)
cout << a[i] << ", " << flush;
}
----------------------
output : 1, 2, 3, 4, 5, 8, 7,
----------------------------------------------------------------------------------
3)
void stockSwap(InventoryItem i1, InventoryItem i2){
Widget *tmp = i1.widgetPtr;
i1.widgetPtr = i2.widgetPtr;
i2.widgetPtr = tmp;
}
-----------------------------------------------------------------------------------------
4)
void setColor(Color **arr, int index, int red, int blue, int green) {
Color c;
c.red = red;
c.blue = blue;
c.green = green;
*arr[index] = c;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.