C++ array output error

Deleting repeating letters

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1248 Views - Last Post: 10 June 2009 - 01:17 PM Rate Topic: -----

#1 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

C++ array output error

Posted 09 June 2009 - 09:37 PM

This code compiles, but still throwing out the repeating letters, what did I do wrong??






#include <iostream>
using namespace std;

bool delete_repeats(char a[], int& size);

int main()

{
  char a[10] = {'a' , 'b' , 'a' , 'c' , 'd', 'a'};
  int size = 6;
  delete_repeats(a, size);

  cout << "size = " << size << endl;
  for (int i = 0; i < size; i++)
  {
	  cout << a[i] << endl;;
  }

  return 0;
}

bool delete_repeats( char a[], int& size)
{
	for(int  i = 0; i < size; i++)
	{
			 for(int j = i+1; j < size; j++)
			 {
					 if(a[i]==a[j])
					 return true;

			 }
	}
	return false;
}


This post has been edited by lxlpxp: 09 June 2009 - 09:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ array output error

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,179
  • Joined: 18-April 07

Re: C++ array output error

Posted 09 June 2009 - 10:19 PM

Your delete_repeats function just detects the repeats and returns "true" to the calling function (in this case main). No where are you actually altering the array by changing its values.

In the example below when we detect the repeat, we simply empty it out by setting it to null. This has the effect of "removing" it from the array. The result is that it will still print spaces for the empty parts of the array but no more repeats...

void delete_repeats( char a[], int& size)
{
    for(int  i = 0; i < size; i++)
    {
             for(int j = i+1; j < size; j++)
             {
                          // If you detect a repeat, set the repeat to null
                          if(a[i]==a[j]) { a[j] = NULL; }

             }
    }
}



If you want to get rid of the spaces then you will need to either move elements up after you delete an item or copy the values that remain to an a new array.

But this is just an example to show you how to detect the repeat and alter the array to remove it.

Enjoy!

"At DIC we be array altering code ninjas.. we alter a newbie's reality. In an alternate reality they might have been successful, but not here." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 09:13 AM

The output on this is
size = 4
a
b

c


how do i move the c up?



#include <iostream>
using namespace std;

void delete_repeats(char a[], int& size);

int main()

{
  char a[10] = {'a' , 'b' , 'a' , 'c'};
  int size = 4;
  delete_repeats(a, size);

  cout << "size = " << size << endl;
  for (int i = 0; i < size; i++)
  {
	  cout << a[i] << endl;;
  }


}

void delete_repeats( char a[], int& size)
{
	for(int  i = 0; i < (size-1); i++)
	{
			 for(int j = i+1; j < size; j++)
			 {
					 if(a[i]==a[j])
					 {a[j] = NULL;}

			 }
	}

}




Was This Post Helpful? 0
  • +
  • -

#4 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 09:34 AM

because in your delete_repeats you're pretty much just telling it to print out a line. instead, you should tell it to go to the next letter/number in the array. hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#5 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 09:47 AM

is this the for loop i have in main?
Was This Post Helpful? 0
  • +
  • -

#6 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 10:08 AM

...no. check the delete function you have. you should change that so that if there's an extra one, it skips it or makes the next letter/number next in line. kind of like a sort, but instead of switching them, you're skipping them, but not just simply telling it to make it a blank.
Was This Post Helpful? 0
  • +
  • -

#7 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 10:14 AM

so dont use NULL?
Was This Post Helpful? 0
  • +
  • -

#8 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 10:25 AM

for once i don't need to facepalm... lol yes, don't use NULL. or at least i think it would prevent the chance of it just giving you a blank for it because you have a,b,a,c right? so it prints out a,b, but then just gives you a blank. try to skip it some how cause that's what i believe is happening. hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#9 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 12:24 PM

im confused on how that would work...im a beginner
Was This Post Helpful? 0
  • +
  • -

#10 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 12:26 PM

since you showed some code, i would be willing to help you out with writing it. hold on...
Was This Post Helpful? 0
  • +
  • -

#11 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 12:37 PM

thanks
Was This Post Helpful? 0
  • +
  • -

#12 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 01:00 PM

i thought of a way that you could probably use your own code and still take out that big space. try having an if statement before you print it out that if it is null, then you don't print a space. try that and let me know if it works.
Was This Post Helpful? 0
  • +
  • -

#13 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 01:04 PM

so after that NULL statement, how would just delete that space? like

if (a[j] = NULL
{

}
then how would i get that space to be deleted?
Was This Post Helpful? 0
  • +
  • -

#14 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Re: C++ array output error

Posted 10 June 2009 - 01:10 PM

don't forget the code tags :wink:

here's what i've got right now:

#include<iostream>
using namespace std;

int main(){

char letters[10]={'a','b','a','c'};

for(int i=0;i<4-1;i++){
if(letters[i]==letters[i])
cout<<letters[i+1]<<endl;

else
cout<<letters[i]<<endl;
}
return 0;
}



hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#15 lxlpxp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 01-May 09

Re: C++ array output error

Posted 10 June 2009 - 01:16 PM

thanks, ill see what i can do, thanks a lot!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2