Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this C++ coding problem ASAP. Last week, I learned about pointers and memory for the first time. Unfortunately, I am finding

Please help me with this C++ coding problem ASAP.

Last week, I learned about pointers and memory for the first time. Unfortunately, I am finding it very confusing. Sometimes, a pointer is just an address to a value in memory. Other times, a pointer is the address of another pointer! I just cant wrap my head around all these pointers to pointers to pointers. To try to understand pointers, I have been practicing by drawing out a region of memory on paper, and filling each position with either a value or a pointer to another memory address. Then, I take the address of the first element in memory and follow it to see where it leads. I do this by dereferencing the pointer as many times as needed to reach a value in the array. However, I am running into a horrible problem. Sometimes, when I chase pointers this way I end up at an address I have already seen. Then I end up in an endless loop going to the same addresses over and over again! Can you help me write a program to chase down these tricky pointers and find out which chases cause cycles? Input The first line of input will contain two space-separated integers: 0 S < 2 31, 1 A 1000 where: S: the starting address of the allocated region of memory A: the size of (number of elements in) the allocated region in memory You are also guaranteed that S + A 2 31 . On the second line will be A space-separated integers, each lying in the range [2 31 , 2 31), indicating what is stored at address S + i. This will be either: (a) A value, a strictly negative integer, or (b) A pointer (memory address), a nonnegative integer. It is guaranteed that no chase will result in a memory access that is outside the bounds of the array. Output Output a single line, the result of chasing when starting with a pointer to address S. This will be one of the following two statements: 1. The negative value v found while chasing. 2. The message There was a cycle, if, when chasing down a pointer, the program revisits any address it has previously seen. Comment This fictitious machine is a bit strange. The memory addresses require 32 bits yet each pointer uses only one byte. An alternative view of this problem is that you are chasing indices into a strange array whose first index is S rather than the traditional start index of 0. But dont overthink this too much, just solve the problem as it is without worry about whether this could be realized by a real computer.

Sample Input:

10428394 1

-12

Sample Output:

-12

Explanation: There is only one element in this allocated region of memory. A chase at the starting address, 10428394 returns the value -12 imediately.

Sample Input 2:

4000 3

4001 4002 4000

Sample Output:

There was a cycle

Explanation: Starting at position 4000, we dereference to get the value 4001. This is another valid address in the array, so if we dereference again, we get 4002, and then 4000 again, which leads back to 4001. This is an endless cycle.

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part 2 Lnai 8725

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448505, 978-3662448502

More Books

Students also viewed these Databases questions

Question

=+j Describe the various support services delivered by IHR.

Answered: 1 week ago

Question

=+j Explain IHRMs role in global HR research.

Answered: 1 week ago