If anyone could help me, that would be awesome! I would use a vector, but this is part of a bigger project (this is the only thing I'm stuck on), and the teacher is limiting us to arrays.
You can shift all the elements in an array over by doing this: for (int i = 0; i < array_size - 1; ++i) array[i + 1] = array[i];
You probably want this:
1 2
for (int i = target - 1; i > 0; -- i)
words[i] = words[i - 1];
In your case you would want to save the element at the index you're moving to the front, shift all the elements below that index over, and put the saved element into index 0.
Of course, you have to find the index of the element you're moving to the front. That's the upper bound of the loop above. Simple linear search is probably fine (you're stuck at O(n) no matter what): int i; for (i = 0; i < array_size; ++i) if (array[i] == elt) break;
Don't forget to check that the element was actually found in the array. if (i == array_size) { uh oh }
Hey mbozzi, I greatly appreciate you for answering my question. However, I'm running into some trouble in trying to implement your advice, and I hope you can offer me some more guidance. Here is what I have right now to test for an algorithm. I am still trying to fix it, so I'll be trying to figure it out while waiting for a reply from you. However, in the case that I give up, I'd want to have the correct answer to refer to.
What I have right now outputs {A, A, C, D, E, F, G, H, I, J} when chosenLetter = "A" ,
{B, B, B, D, E, F, G, H, I, J,} when chosenLetter = "B", and so on.
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string array[10] = {"A","B","C","D","E","F","G","H","I","J"};
string chosenLetter;
int array_size = sizeof(array)/sizeof(*array);
int indexOfChosenLetter;
//prints array
for (int i = 0; i < array_size; i++){
cout << array[i] << ", ";
}
cout << "Select a letter to put in front of the array" << endl;
cin >> chosenLetter;
for (int i = 0; i < array_size - 1; ++i){
if(array[i] == chosenLetter){
indexOfChosenLetter = i;
}
}
//code I need help with
for (int i = indexOfChosenLetter; i < array_size; ++i){
array[i + 1] = array[i];
if(array[i] == chosenLetter){
array[i] = array[0];
array[0] = chosenLetter;
break;
}
}
//prints array
for (int i = 0; i < array_size; i++){
cout << array[i] << ", ";
}
return 0;
}
EDIT: Help would be appreciated from anyone, as well.
You can iterate through your array and check the position of your character. Store the value of the array using a temp, then use a for loop to shift all of the elements of your array up to that position and place the temp element into the index 0
Hey personxd1, thanks for replying. I'm having problems doing the shifting. Refer to the code above, because it's pretty much what I still have. Sorry for annoying you guys, I'm pretty new to coding in general.
@mbozzi
Rotating the array is fine, but the assignment does not require you to. You can just swap two array elements and things will turn out well while still being able to preserve the original array elements.