c++ array question

Hello Experts,

I have a small problem with some C++ code.  I use Microsoft VB 6.0 to compile it, but here is my problem:

I have a Character Array of 22 Characters named people... so
temp[21]

I assigned a unique character to each one of them, for example put lets put a-v in them such that temp[0]=a and temp[1]=b and so on all the way to v.

There are more manipulations that i need to do, but here is the main thing i need help with.
I am going to prompt to user to enter a skip number for example 8.

What i want to do then is go to temp[8] which is character "i" in the example do some of my manipulations and then delete the character out of the array or just set it equal to 0 or something.  

Then i want to continue to skip 8 so that i go to people[16] which should be "r" and once i get past the 21 i want to be able to go back to 0 and and go to people[4] which shoud be "e".

Here is where my problem comes in.  Now some of the values are 0 or taken out of the array if I add another 8  then temp[4+8] is temp[12] but we took out  character "i" and i need to be able to skip i when adding the 8.  I want to continue this until there are only Skip number of characters left in the array.  So for this example 8.

void main()
{
  cout<<"Enter Skip Number: ";
  cin>>skip;
  cout<<endl;

  //initialize
  for(int i=0; i<22; ++i)
   temp[i] = 'a' + i;

for (i=0; i <22-skip; i++)
{
Manipulation Code here
}

That last for loop should make sure that we will do it until there is only skip number of characters left

Any help or input is appriciated
csenowAsked:
Who is Participating?
 
jhshuklaCommented:
#include <iostream>

using namespace std;

int main(){
  char temp[] = "abcdefghijklmnopqrstuv";
  int remaining = 22;
  int skip = 8;

  for ( int i = skip; remaining > skip; remaining --){
    temp[i] = 0;
    for(int j=0; j<=skip; j++){
      i = (i+1)%22;
      while( !temp[i] ) i = (i+1)%22;
    }
    for(j=0; j<22; j++)
      cout << temp[j];
    cout << endl;
  }
    for(int j=0; j<22; j++)
      cout << temp[j];
    cout << endl;
  return 0;
}

output (paste in fixed-width fonts):
abcdefgh jklmnopqrstuv
abcdefgh jklmnopq stuv
abcd fgh jklmnopq stuv
abcd fgh jklmn pq stuv
ab d fgh jklmn pq stuv
ab d fgh jklm  pq stuv
ab   fgh jklm  pq stuv
ab   fgh jklm  p  stuv
ab   fg  jklm  p  stuv
ab   fg  jklm  p  stu
ab   fg  jklm     stu
ab   fg  j lm     stu
ab   f   j lm     stu
ab       j lm     stu
0
 
jhshuklaCommented:
...
int count;
i=0;
for(count = 22; count > skip; count --){
  temp[i] = '\0';
  i += skip;
  if(temp[i] == '\0') i++;
  i %= 22;
}
0
 
jhshuklaCommented:
slight change:
for(count = 22; count > skip; count --){
  temp[i] = '\0';
  i += skip;
  i %= 22;           <<<<< added this.
  if(temp[i] == '\0') i++;
  i %= 22;
}
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
jhshuklaCommented:
one more flaw:
while(temp[i] == '\0'){ i++; i%=22; }
0
 
anthony_wCommented:
If you want an array with 22 elements, then you need to say so

char temp[22];

The number in the brackets is not the upper bound, but the size of the array.

Now for the removal:


for(unsigned count=sizeof(temp),index=0;count>skip;--count)
{
    for(unsigned i=0;i<skip || !temp[index];index=(index+1)%sizeof(temp))
    {
        if(temp[index])
        {
            ++i;
        }
    }
    temp[index]=0;
}

Of course, if you can use a std::vector instead of an array, then you don't have to worry about skipping the empty entries, since you can just delete them:

    std::vector<char> temp2;

    for(unsigned i=0;i<22;++i)
    {
        temp2.push_back(i+'a');
    }
   
    for(unsigned index=0;temp2.size()>skip;)
    {
        index=(index+skip)%temp2.size();
        temp2.erase(temp2.begin()+index);
    }    

    for(unsigned i=0;i<temp2.size();++i)
    {
        std::cout<<temp2[i];
    }
    std::cout<<std::endl;
0
 
rstaveleyCommented:
> I use Microsoft VB 6.0 to compile it

That wouldn't help :-)
0
 
csenowAuthor Commented:
Getting there but not working all the way.  With your solution these are left over:

d f h  l n p t v when skip is 8.  The correct left over should be a b j l m s t u

because we first eliminate i then r then e then o then c then n then d then q then h then v then p then k then g then f

hope that helps
0
 
csenowAuthor Commented:
I need this done ASAP now, so increasing value of points
0
 
PaulCaswellCommented:
int remaining = 22;
for ( int i = skip; remaining > skip; i = (i + skip)%22)
{
 while ( temp[i] == 0 ) i = (i + 1)%22;
 temp[i] = 0;
 remaining -= 1;
}

Paul
0
 
csenowAuthor Commented:
That causes an infinite loop paul
0
 
csenowAuthor Commented:
My fault it dosn't cause a infinite loop but the answer is incorrect.

output is b f h l n p t v  but correct should be a b j l m s t u when skip is 8
0
 
csenowAuthor Commented:
I need to have it done in less then an hour, will give up to 500 points for anyone who can get it done, any programming language that u can run on Windows is ok.  I just need it done.  I have a strict deadline
0
 
jhshuklaCommented:
start with skip -1
0
 
csenowAuthor Commented:
nope that gives me a b c i j p q r

0
 
csenowAuthor Commented:
anymore ideas anyone?
0
 
jhshuklaCommented:
mistakes in previous codes:
1) incremented i by skip instead of skipping skip number of chars i.e. we were supposed to skip 'skip' characters
2) and it is not simple addition and %, we need to take care of all the blank spots in our way. so the for & while loops for incrementing i.
0
 
csenowAuthor Commented:
Cool that seem to work, one more thing and we are all done, also uped the points to 500 like i said i would.

I need to print it out in a certain way like such if skip is 8:
a goes to i
j goes to r
s goes to e
f goes to o
and so on all the way to the last line which is j goes to f.

i can't figure out how to print it out without it printing weired characters

Thank you so much for all your help after this i'm all set
0
 
jhshuklaCommented:
#include <iostream>

using namespace std;

int main(){
  char temp[] = "abcdefghijklmnopqrstuv";
  int remaining = 22;
  int skip = 8;

  for ( int i = -1; remaining > skip; remaining --){
    int j;
    for(j=0; j<=skip; j++){
      i = (i+1)%22;
      while( !temp[i] ) i = (i+1)%22;
      if(!j)    cout << temp[i] << " goes to ";
    }
    cout << temp[i] << "\t";
    temp[i] = 0;

    for(j=0; j<22; j++)
      cout << temp[j];
    cout << endl;
  }
    for(int j=0; j<22; j++)
      cout << temp[j];
    cout << endl;
  return 0;
}

note that since we want to print 'a', we start from -1. could have started from 22 to get same results.

output:
a goes to i     abcdefgh jklmnopqrstuv
j goes to r     abcdefgh jklmnopq stuv
s goes to e     abcd fgh jklmnopq stuv
f goes to o     abcd fgh jklmn pq stuv
p goes to c     ab d fgh jklmn pq stuv
d goes to n     ab d fgh jklm  pq stuv
p goes to d     ab   fgh jklm  pq stuv
f goes to q     ab   fgh jklm  p  stuv
s goes to h     ab   fg  jklm  p  stuv
j goes to v     ab   fg  jklm  p  stu
a goes to p     ab   fg  jklm     stu
s goes to k     ab   fg  j lm     stu
l goes to g     ab   f   j lm     stu
j goes to f     ab       j lm     stu
0
 
jhshuklaCommented:
nope. need to start from either -1 or 21
0
 
anthony_wCommented:
Both the solutions I posted yesterday gave the correct answer for the final set.

Here is both solutions in one program, complete with "a goes to i" stuff:

#include <iostream>
#include <vector>

int main()
{
    char temp[22];
   
    for(unsigned i=0;i<sizeof(temp);++i)
    {
        temp[i]=i+'a';
    }
   
    unsigned const skip=8;
    for(unsigned count=sizeof(temp),index=0;count>skip;--count)
    {
        for(unsigned i=0;i<skip || !temp[index];index=(index+1)%sizeof(temp))
        {
            if(temp[index])
            {
                if(!i)
                {
                    std::cout<<temp[index]<<" goes to ";
                }
                ++i;
            }
        }
        std::cout<<temp[index]<<std::endl;
        temp[index]=0;
    }    

    for(unsigned i=0;i<sizeof(temp);++i)
    {
        if(temp[i])
        {
            std::cout<<temp[i];
        }
    }
    std::cout<<std::endl;

    // repeat with a vector rather than an array
    std::vector<char> temp2;

    for(unsigned i=0;i<22;++i)
    {
        temp2.push_back(i+'a');
    }
   
    for(unsigned index=0;temp2.size()>skip;)
    {
        std::cout<<temp2[index]<<" goes to ";
        index=(index+skip)%temp2.size();
        std::cout<<temp2[index]<<std::endl;
        temp2.erase(temp2.begin()+index);
    }    

    for(unsigned i=0;i<temp2.size();++i)
    {
        if(temp2[i])
        {
            std::cout<<temp2[i];
        }
    }
    std::cout<<std::endl;
   
}
0
 
jhshuklaCommented:
anthony, your first solution does not print 'j'
0
 
anthony_wCommented:
jhshukla, below is the output from my program, which shows that it *does* print "j" in both cases. Note that the definition of "temp" is important, since my solution uses "sizeof(temp)". If you define temp as you have:

char temp[]="abcdefghijklmnopqrstuv";

sizeof(temp) is 23, so it will yield the wrong answer, as it would assume there is a 23rd element, which has already been printed. If you use your definition of temp, and replace my uses of "sizeof(temp)" with 22 (or a constant which has the value 22), then it will give the correct answer.

csenow, I feel slightly offended that you didn't credit me with any points (though I don't begrudge jhshukla the "Accepted answer", as he obviously worked hard for this question, and his final program to do the updated task was posted before mine), though it is unsurpsrising, since it appears that you didn't read my posts --- I posted a correct solution 8 hours before you said " I need this done ASAP now, so increasing value of points".

Rant over, here's the output of my program:

a goes to i
j goes to r
s goes to e
f goes to o
p goes to c
d goes to n
p goes to d
f goes to q
s goes to h
j goes to v
a goes to p
s goes to k
l goes to g
j goes to f
abjlmstu
a goes to i
j goes to r
s goes to e
f goes to o
p goes to c
d goes to n
p goes to d
f goes to q
s goes to h
j goes to v
v goes to p
s goes to k
l goes to g
j goes to f
abjlmstu
0
 
jhshuklaCommented:
you are right anthony. it is the sizeof that I didn't notice. the author should have accepted your solution.
0
 
csenowAuthor Commented:
is there anyway that i can change the points?  What happend is i saw your code containing stuff such as   std::vector which i have never seen and it wouldn't of helped me much since i wouldn't understand how it would work.  I didn't notice the first part you posted in regular C++.  Then also the next guy talked about it not working in Microsoft Visual Studio 6.0 so i moved on hoping for a solution
0
 
jhshuklaCommented:
ask a question in CS TA: http://www.experts-exchange.com/Community_Support/
dont forget to mention the URL of this question.
0
 
csenowAuthor Commented:
I split up the points between you 2, 300 for the inital code by anthony and 200 for finishing my problem with the extra question.  Everyone ok with this?  Sorry again for the problems.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.