Basic String Array Algorithm

Hello, its my first time here and programming isnt my strongest point. Any help is greatly appreciated.

Take an array of strings with fixed size..
string m_content[100] = {"mary","zack","quack",...}

I'm asked to write a function
bool z(int pos, const string& value)

where its supposed to insert the user input string 'value' into m_content[pos] and then push the rest of the strings over one position and return true else false. So if i called z(0,"friend"); s would look like {"friend", "mary", "zack", "quack",...}


this is what i got for the implementation of z:
1
2
3
4
5
6
7
8
9
10
11
12
13
if(pos <= 100 && pos > -1)
{
  for(int i = pos; i<100; i++)
   {
     m_content[i+1]=m_content[i];
   }
   m_content[pos] = value;
   return true;
}
else
{
return false;
}


but upon doing it on paper i realized all it does is insert value at pos and then make every item after that the same string. can someone please shed some insight on how to do what needs to be done? thank you
Last edited on
It's pretty simple. Your for loop just needs to count backwards from 100 to pos.
Have you considered looping in the other direction?

Also, you'll want to be very careful about staying within bounds of the array.
i couldnt figure out a solution looping backwards.. but i managed to find a solution using 2 holding variables.
Topic archived. No new replies allowed.