FOR LOOP in a FOR LOOP

Started by
8 comments, last by DigitalDelusion 18 years, 8 months ago
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?
Advertisement
Is my question really confusing, the way i asked it? so many views, no replys.
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).
First, you want to use [ source ]-tags or -tags (without the spaces).<br><br>This might work, but i haven't tested it. At least it might give you some ideas ;)<br><!--STARTSCRIPT--><!--source lang="cpp"--><div class="source"><pre><br><br><span class="cpp-keyword">int</span> remove_char(<span class="cpp-keyword">int</span> j)<br>{<br> <span class="cpp-keyword">for</span>(; j&lt;<span class="cpp-number">20</span>;++j )<br> {<br> encrypt[j]=encrypt[j+<span class="cpp-number">1</span>];<br> }<br> encrypt[<span class="cpp-number">20</span>]=<span class="cpp-number">0</span>;<br>}<br><br><span class="cpp-keyword">int</span> repeats()<br>{<br> <span class="cpp-keyword">int</span> i, j,z;<br> <span class="cpp-keyword">for</span>(i=<span class="cpp-number">0</span>;i&lt;<span class="cpp-number">21</span>;i++)<br> {<br> <span class="cpp-keyword">for</span>(j=<span class="cpp-number">1</span>;j&lt;<span class="cpp-number">21</span>;j++)<br> {<br> <span class="cpp-keyword">if</span>(encrypt[j]==encrypt<span style="font-weight:bold;">)<br> {<br> remove_char(j);<br> <span class="cpp-keyword">break</span>;<br> }<br> }<br> }<br><br> <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br><br></pre></div><!--ENDSCRIPT-->
whats int remove-char for?
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.
Quote:Original post by bikola_p
whats int remove-char for?


Err, are you joking :) It calls the function. I suggest a C book :)
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
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.
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]
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement