Hello everyone. I'm just about finished with my degree and am now taking a Data Structures class. So far, it doesn't make sense to me. I'm seeing code snippets of 100+ lines that I could solve in 10. Hopefully I begin the realize the power of Data Structures soon.

Anyways, I have been 4 terms away from C++ and now they scheduled me back into a C++ class. In short, I'm very rusty.

Besides the fact that this seems like a overexaggerated solution in my opinion, my problem has to be solved this way, instructor orders.

Instructor Skeleton Code:

// Simple display of array from start to finish
// --------------------------------------------
void displayArray(int *arrayStart, //first element of array
                  int *arrayEnd  ) //last  element of array
{
	int *tmp;
   // output label
   // ------------
   cout << "Original array: ";

   // display the array elements from the start to the end
   // (two lines of code required).
   // ----------------------------------------------------
   //CODE NEEDED HERE 
   //CODE NEEDED HERE 
   
   // terminate array display line
   // ----------------------------
   cout << endl;
}

Essentially, you pass in integer values that are stored in an array and you need to print them all back out once you're finished. Later, I have to reverse them but I'm pretty sure I can figure that out once I get a little boost. How do I loop through the above array? There aren't any global variables that stores the entire array, just the start and finish.

By no way am I asking you to solve my homework. I just need a little nudge in what I'm not seeing.

Thank you so much.

P.S. Do any of you know of any C++ Data Structure Tutoring programs or the like? I haven't had trouble in my programming classes where I needed it but I can see me really needing it for this course.

Edited 5 Years Ago by SacredFootball: n/a

Since you're just looking for a tip to get you started, here's a clue to look up: "pointer arithmetic".

Since you're just looking for a tip to get you started, here's a clue to look up: "pointer arithmetic".

I've checked it out but I'm confused as to how the program 'knows' the length of the array. How does it know how long to loop for? All the examples I see use a single pointer to an array, not two.

Correct me if I'm wrong but from what I'm reading, I'll need something like this:

for (int i = 0; i < *DON'T KNOW THIS PART*; i++)
cout << element[i];

Why wouldn't this be acceptable?

It would be acceptable, presumably, if only you knew how long the array was.

You could just move the starting pointer along until you knew it was time to stop...

I don't want to just give you a solution, since you asked us not to; hopefully that's enough to give you some ideas.

Edited 5 Years Ago by Moschops: n/a

It would be acceptable, presumably, if only you knew how long the array was.

You could just move the starting pointer along until you knew it was time to stop...

I don't want to just give you a solution, since you asked us not to; hopefully that's enough to give you some ideas.

Yeah, I don't know how long it is, just that it allows a maximum of 20 inputs.
What do you mean by "move the starting pointer along"? Like making the starting pointer equal to the next value in the array after each loop iteration or am I completely off base?

Like making the starting pointer equal to the next value in the array

Making it point at the next one along, yes. If you take a pointer, and add 1 to it, it then points at the next object in the array.

Making it point at the next one along, yes. If you take a pointer, and add 1 to it, it then points at the next object in the array.

I see. I've get that but I guess where I'm hung up is on the loop. I get get it to print out the values correctly but cannot figure out how to loop through the array correctly. My while loop comparing the start and end references gives me an infinite loop and I cannot figure out how to do a for loop in this case. Should I be using either of these loops?

You could use a for loop, or a while loop.

So you've got it going through the array, but it doesn't stop at the right place. To my mind, the time to stop is when

arrayStart == arrayEnd

You could use a for loop, or a while loop.

So you've got it going through the array, but it doesn't stop at the right place. To my mind, the time to stop is when

arrayStart == arrayEnd

Right, I've tried that but it gives me an infinite loop. Here's the while loop I tried:

while (&arrayStart != &arrayEnd)
	   cout << *arrayStart++ << endl;

I used the references just in case the user input duplicate values.

Edited 5 Years Ago by SacredFootball: n/a

What you've said there is

while (address of the pointer named arrayStart does not equal the address of the pointer named arrayEnd)

Given that arrayStart and arrayEnd are not the same object (they are not part of the array; they are two independent pointer objects, which take up their own memory somewhere and are complete, fully formed objects like an int or a double), they will never have the same address, so will never be equal, so this is an infinite loop. You have not used references; you have used the operator "&", which means "address of".

Edited 5 Years Ago by Moschops: n/a

Right, I've tried that but it gives me an infinite loop. Here's the while loop I tried:

while (&arrayStart != &arrayEnd)
	   cout << *arrayStart++ << endl;

I used the references just in case the user input duplicate values.

Nevermind, I think I got it with this:

while (arrayStart != arrayEnd+1)
	   cout << *arrayStart++ << endl;

I cannot thank you enough. You've been a tremendous help :D :D

What you've said there is

while (address of the pointer named arrayStart does not equal the address of the pointer named arrayEnd)

Given that arrayStart and arrayEnd are not the same object (they are not part of the array; they are two independent pointer objects, which take up their own memory somewhere and are complete, fully formed objects like an int or a double), they will never have the same address, so will never be equal, so this is an infinite loop. You have not used references; you have used the operator "&", which means "address of".

Big brain fart there on my part. Syntax and meanings got jumbled in my head. Again, thank you so, so much for all your help. I'm understanding what's going on much better now :)

I don't know what that int *tmp is for, though.

Is there some restriction on not changing the pointers you've been passed?

If there is, you could use a for loop like this, for example, and use that mysterious tmp:

for (tmp = arrayStart; tmp != arrayEnd+1; ++tmp)
  {cout << *tmp << endl;}

I expect it could be done with some other similar type loops as well.

I don't know what that int *tmp is for, though.

Is there some restriction on not changing the pointers you've been passed?

If there is, you could use a for loop like this, for example, and use that mysterious tmp:

for (tmp = arrayStart; tmp != arrayEnd+1; ++tmp)
  {cout << *tmp << endl;}

I expect it could be done with some other similar type loops as well.

Oops, good catch. That was my code. Forgot to take it out before I posted it. I thought I might need it when I was trying to figure stuff out before I posted my question. I was going to try and figure out how to traverse the array using a temporary pointer variable but I couldn't figure it out for the life of me :(

Why would it be arrayEnd + 1? From tradition, arrayEnd should already be 1-pass the end of the array. So using your function this call would be a bug :

const int S = 5;
int array[S] = {0};
display(array, array + S);

because you will be looping through array + S + 1 which is an invalid pointer.
So take that +1 out of the loop and let it only be while(arrayBegin != arrayEnd){...}

Why would it be arrayEnd + 1? From tradition, arrayEnd should already be 1-pass the end of the array.

Tradition is no match for reading the question.

int *arrayEnd  ) //last  element of array

Edited 5 Years Ago by Moschops: n/a

That came across a bit stern. It meant to read:

Tradition is no match for reading the question :p

or possibly

Tradition is no match for reading the question :)

Meant to be less serious in tone that it came out.

This article has been dead for over six months. Start a new discussion instead.