Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is the CPP file Questions to add to the program: Write a function prototype

ID: 3821083 • Letter: H

Question

Here is the CPP file

Questions to add to the program:

Write a function prototype and definition for print_contents. print_contents takes a pointer to a node as its only parameter. It does not return a value. The function goes to a new line on output and prints the information in the node referenced by the pointer. For example if the following code was executed for the above example:

print_contents (ptr2);

The function would print:

e17

Write a loop in main that controls a pointer moving through the given list starting at ptr1. For each element of the list, print_contents should be called to print that element’s information. Even though we know that the list in the example is three elements, the loop should work for any size list.

Answer the following questions:

1. Print ptr1 -> next -> form just before the cout << “ ”. What value is printed and why?

2. Change the value printed to ptr1 -> next. Explain why this value is printed. What does it point to?

3. Change the code to print ptr3 -> next. What does this value represent?

4. Change the code to print ptr3 -> next -> version. What happens and why?

Remove this print statement.

5. Put the following assignment statement just before the printing loop in main:

ptr3 -> next = ptr1;

Run your program. What happens? Explain why. Draw a box and arrow diagram to help in the explanation.

Explanation / Answer

PROGRAM CODE:

#include <cstdlib>
#include <iostream>

struct tax_node
{
char form; // tax form letter
int version; // tax form number
tax_node *next; // pointer to next node
};

typedef tax_node* tax_ptr;

using namespace std;

void print_contents(tax_ptr ptr)
{
   cout<<endl<<ptr->form<<ptr->version;
}
int main(int argc, char *argv[])
{
tax_ptr ptr1, ptr2, ptr3, mover;

ptr1 = new tax_node;
ptr1 -> form = 'w';
ptr1 -> version = 2;
   ptr1->next = NULL;
   while(ptr1 != NULL)
   {
       print_contents(ptr1);
       ptr1 = ptr1->next;
   }
cout << " ";

return 0;
}

OUTPUT:

Answers:

1. Nothing is printed since next is not initialized for ptr1

2. Throws a Runtime Error since next is a pointer and cannot be printed.

3. ptr3->next is not initialized. So this will now be NULL. it will throw Runtime Error

4. it will throw Runtime Error .

5. ptr3 will have not any item in the first index. But the next index will contain values from ptr1.