Jump to content
  • Advertisement
Sign in to follow this  
bikola_p

FOR LOOP in a FOR LOOP

This topic is 4839 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, i need to read in a array of characters. The array of characters cannot have repeating characters, so i decided to use a for loop, in a for loop, by this i mean by setting to loops, one which increments every time the full array of characters is compared with an element of the array. The second loop increments the character which it is comparing it self to. ALL this is done in a function i called repeats().The point of this loop is to compare each letter with the first, then each with the second, then each with third, etc. Then once a repeted letter is found, it is supposed to replace the repeated letter with the next characters in line of the array. SO, if i was to input basement, there are 2 e's and what i need is to output basemnt. so i have: ______________________________________________________________________________ int repeats(){ int i, j,z; for(i=0;i<21;i++){ for(j=1;j<21;j++){ if(encrypt==encrypt[j]){ z=j++; do{ encrypt[j]=encrypt[z]; j++; z++; }while(z<21); }; } } return 0; } ____________________________________________________________________________ Now, the problem, if i was to input zolof, I CLEARLY have 2 O's. i would expect zolf. However, my program return's zooooooooooooooooooo. What am i doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
take pen and paper. Then step through the algorithm line by line, writing down what happens to the array at each line. This should help you find the problem.

sorry that I couldnt give you a complete solution, I'm too lazy to figure out all the loops and index variables, and its much more educational like this (it smells a bit like a school assignment).

Share this post


Link to post
Share on other sites
First, you want to use [ source ]-tags or [ code ]-tags (without the spaces).

This might work, but i haven't tested it. At least it might give you some ideas ;)


int remove_char(int j)
{
for(; j<20;++j )
{
encrypt[j]=encrypt[j+1];
}
encrypt[20]=0;
}

int repeats()
{
int i, j,z;
for(i=0;i<21;i++)
{
for(j=1;j<21;j++)
{
if(encrypt[j]==encrypt)
{
remove_char(j);
break;
}
}
}

return 0;
}

Share this post


Link to post
Share on other sites
It's a separate function to do the removal.

Trying to modify the array in place is probably too tricky. Try making a separate copy instead.

To remove repetitions in input:
output = {empty string}.
For each letter in input:
If the letter is not already in output:
Append the letter to output.
(optional) Copy output back over top of input.


And for heaven's sake, use std::string.

Share this post


Link to post
Share on other sites
Quote:
Original post by bikola_p
whats int remove-char for?


Err, are you joking :) It calls the function. I suggest a C book :)

Share this post


Link to post
Share on other sites
As Zahlman said, you make it a lot easier for yourself if you dont try and change the array in-place.

here is a quick python snippet that does the job (notice how similar it is to Zahlman's pseudocode). If you dont absolutely have to use C I'd recommend Python instead. That way you can focus on learning programming without fighting with char-arrays.


def repeat(encrypt):
new_encrypt = []

for letter in encrypt:
if letter not in new_encrypt:
new_encrypt.append(letter)

return new_encrypt


Share this post


Link to post
Share on other sites
Quote:

if(encrypt==encrypt[j]){
z=j++;


I once said that I don't like the ++ operator except in a very few cases, because it makes for hard to detect bugs. I was told by several people that I was wrong :)

Read that second line... Ask yourself what you meant, and what this does. It is wrong in two ways, atleast, and is the reason you get zoooooooooo.

Share this post


Link to post
Share on other sites
use functions, and keep it simple, this runs in quadratic time, could be made to run in linear (it can actually be given a hard upper bound to...) but that's left as an exercise to the reader.

bool contains(const char *b, const char *e, int c);
/*
Let's squish a string, a squished string (I made that one up right now) contains no duplicate characters. Only the first occurence of any string is kept.

returns the newly squished string for convinence.
*/

char* squish(char *sz)
{
char *in = sz, *out = sz;
for(; *in; ++in)//for the whole string
if( !contains( sz, in, *in))//if this char not already seen in output
*out++ = *in;//copy it
*out = 0;//null-terminate the new string
return sz;
}
//check if the sequence (b,e] contains the character 'c'
bool contains(const char *b, const char *e, int c)
{
while( b != e)
if( *b++ == c)
return true;
return false;
}



[Edit: Until I get the new IntelliMouse with horizontal panning rocker, I'll edit posts to minimize page width [smile] - Oluseyi]

[Edited by - Oluseyi on August 15, 2005 7:25:22 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!