Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c++ array question

Posted on 2004-10-26
27
Medium Priority
?
258 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
Comment
Question by:csenow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
  • 3
  • +2
27 Comments
 
LVL 9

Expert Comment

by:jhshukla
ID: 12418714
...
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
ID: 12418719
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
ID: 12418725
one more flaw:
while(temp[i] == '\0'){ i++; i%=22; }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 4

Assisted Solution

by:anthony_w
anthony_w earned 1200 total points
ID: 12418846
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
ID: 12419576
> I use Microsoft VB 6.0 to compile it

That wouldn't help :-)
0
 

Author Comment

by:csenow
ID: 12422355
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
ID: 12422680
I need this done ASAP now, so increasing value of points
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 12424057
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
ID: 12424555
That causes an infinite loop paul
0
 

Author Comment

by:csenow
ID: 12424592
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
ID: 12425555
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
ID: 12426290
start with skip -1
0
 

Author Comment

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

0
 

Author Comment

by:csenow
ID: 12429710
anymore ideas anyone?
0
 
LVL 9

Accepted Solution

by:
jhshukla earned 800 total points
ID: 12430239
#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
ID: 12430255
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
ID: 12430335
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
ID: 12430405
#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
ID: 12430421
nope. need to start from either -1 or 21
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 12430765
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
ID: 12440431
anthony, your first solution does not print 'j'
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 12441688
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
ID: 12455927
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
ID: 12458697
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
ID: 12464168
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
ID: 12465513
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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