Solved

c++ array question

Posted on 2004-10-26
257 Views
Last Modified: 2010-04-01
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
0
Question by:csenow
    26 Comments
     
    LVL 9

    Expert Comment

    by:jhshukla
    ...
    int count;
    i=0;
    for(count = 22; count > skip; count --){
      temp[i] = '\0';
      i += skip;
      if(temp[i] == '\0') i++;
      i %= 22;
    }
    0
     
    LVL 9

    Expert Comment

    by:jhshukla
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    one more flaw:
    while(temp[i] == '\0'){ i++; i%=22; }
    0
     
    LVL 4

    Assisted Solution

    by:anthony_w
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    > I use Microsoft VB 6.0 to compile it

    That wouldn't help :-)
    0
     

    Author Comment

    by:csenow
    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
     

    Author Comment

    by:csenow
    I need this done ASAP now, so increasing value of points
    0
     
    LVL 16

    Expert Comment

    by:PaulCaswell
    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
     

    Author Comment

    by:csenow
    That causes an infinite loop paul
    0
     

    Author Comment

    by:csenow
    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
     

    Author Comment

    by:csenow
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    start with skip -1
    0
     

    Author Comment

    by:csenow
    nope that gives me a b c i j p q r

    0
     

    Author Comment

    by:csenow
    anymore ideas anyone?
    0
     
    LVL 9

    Accepted Solution

    by:
    #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
     
    LVL 9

    Expert Comment

    by:jhshukla
    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
     

    Author Comment

    by:csenow
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    #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
     
    LVL 9

    Expert Comment

    by:jhshukla
    nope. need to start from either -1 or 21
    0
     
    LVL 4

    Expert Comment

    by:anthony_w
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    anthony, your first solution does not print 'j'
    0
     
    LVL 4

    Expert Comment

    by:anthony_w
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    you are right anthony. it is the sizeof that I didn't notice. the author should have accepted your solution.
    0
     

    Author Comment

    by:csenow
    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
     
    LVL 9

    Expert Comment

    by:jhshukla
    ask a question in CS TA: http://www.experts-exchange.com/Community_Support/
    dont forget to mention the URL of this question.
    0
     

    Author Comment

    by:csenow
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Product Review - Android Remix

    Come along for the ride with our Senior Product Manager, Brian Matis, as he reviews the Android Remix.

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now