d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Anyone Here? > Need Some Help With A C++ Program
12Next
Add Reply New Topic New Poll
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Feb 10 2013 03:12pm
pm or post plz
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 10 2013 03:23pm
post your questions with code you will get help when we feel like.
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Feb 10 2013 03:27pm
Quote (AbDuCt @ Feb 10 2013 02:23pm)
post your questions with code you will get help when we feel like.


lol <3

having trouble doing this delete thing to delete duplicates in an array

Code
#include <iostream>
#include <iomanip>
using namespace std;

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

int main()
{
char a[10];
int size = 0;
cout << "Enter your char array, enter # when your done";
for (int i = 0; i<10; i++)
{
 cin >> a[i];
 if (a[i] == '#')
 {
  break;
 }
 size++;
}
cout << a[0];
cout << size << endl;
delete_repeats ( a, size);
cout << size << endl;
for (int j = 0; j < size; j++)
{
cout << a[j];
}
}

void delete_repeats (char array[], int &size)
{
int count = size;
char compare = 0;

//cout << size << " " << count << endl;
for ( int j = 0; j < count; j++)
{
 compare = array[j];
 for (int i = 1; i < count; i++)
 {
  if ( compare == array[i])
  {
   array[i] = array[i+1];
   cout << "test: "<<array[j] << " " << array[i] << endl;
   cout << "test b4: "<<size << endl;
   size--;
   count--;
   cout <<" test after: "<< size << endl;
  }

 }
//01 12 23 34 45 56 67 78
}
}
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 10 2013 05:30pm

what is going wrong with it? also how are you handling removing from the array.

from what im seeing you are just bumping like chars up in the array rather then removing them from the array.


what you should be doing is shifting all data down 1 element starting from where you found the duplicate until no duplicates are found.

something like

Code
remove_dupes(char array[], size)
{
int i, j, k;

for(i = 0; i < size; i++)
{
  for(j = 0; j < size; j++)
  {
     if(array[i] == array[j])
     {
        for(k = j; k < size; k++)
        {
             array[k] = array[k + 1];
         }
       }
   }
}


or something to the like.

basically you need to loop through the array AT LEAST ONCE, then you have to compare each element to each other. if a matching element is found you must delete that element by way of shifting down all other data. then finally continue the checks.

this code preserves the size of the array but in the real work you should dealloc memory that you no longer need in the array.

at least i think thats what youre attempting to do.

This post was edited by AbDuCt on Feb 10 2013 05:30pm
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Feb 10 2013 05:35pm
im trying to delete duplicates in the array then show the partiall filled array

like my inputs are
a b c d e b a
would eventually become
a b c d e
removing the a the first time the loop executes (then reduces size by 1 to account for the loss of an element, then repeat to remove b and reduce by 1)

im having trouble making the delete_Repeats function work correctly
been trying to troubleshoot it for the past 3 hours
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Feb 10 2013 10:06pm
Something like this i guess?

Whipped this up in a couple minutes so it's probably not the best practice of using arrays but should work for whatever it is you need it for.

Code
#include <iostream>
#include <conio.h>
#include <vector>

bool charExists(char element, std::vector<char> buffer)
{
for(std::vector<char>::iterator it = buffer.begin(); it != buffer.end(); it++)
{
 if(*it == element)
  return true;
}

return false;
}

void modifyArray(char arr[], std::vector<char> buffer, int &size)
{
for(int i = 0; i < buffer.size(); i++)
 arr[i] = buffer.at(i);

size = buffer.size();
}

void delete_repeats(char arr[], int &size)
{
bool b = false;
std::vector<char> buffer;

for(int i = 0; i < size; i++)
{
 if(b == false)
 {
  buffer.push_back(arr[i]);
  b = true;
 }
 else
 {
  if(charExists(arr[i], buffer) == false)
  {
   buffer.push_back(arr[i]);
  }
 }
}

modifyArray(arr, buffer, size);
}


int main()
{
int size = 0;
char arr[10];

for(int i = 0; i < 10; i++)
{
 std::cin >> arr[i];

 if(arr[i] == '#')
  break;

 size++;
}

delete_repeats(arr, size);

for(int i = 0; i < size; i++)
{
 std::cout << arr[i] << ' ';
}

_getch();

return 0;
}


This post was edited by SelfTaught on Feb 10 2013 10:29pm
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 11 2013 03:46am
Just add all elements to std::set. It doesn't allow duplicates.
Member
Posts: 23,261
Joined: Dec 17 2006
Gold: 18,129.00
Feb 11 2013 10:06am
How do you need to delete them? Must you push on the new input and remove old duplicate from the array? If not, you could try:

- read input
-for loop i < size of array
if input = variable already in array
do nothing
else
push on array
array size + 1
end for loop

That should work logically.

If you must push it on and then remove the old duplicate you could try:

read input
for loop i < size of array
if input = variable already in array
remove variable
end for loop
push input on array.

And that should work. Lmk if you need help with specific code. This is fairly easy.


Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 11 2013 01:24pm
another viable option is to loop through your array as you are adding to it to check if a duplicate exists before adding the new value.
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
Feb 11 2013 03:54pm
I feel so ignored... Why are you guys reinventing the wheel?

Code
#include <iomanip>
#include <iostream>
#include <set>
#include <type_traits>
#include <typeinfo>

using namespace std;



int main()
{
       int array[] = {1,1,1,3,4,7,8,2,4,6,2,7,2,7,21,6,4,1,6,1,4,6};
       
       set<int> s;
       
       for(auto&& it = begin(array); it != end(array); ++it) s.insert(*it);
       
       for(auto&& i : s) cout << i << ", ";
       
}


Just a few lines and it works perfectly:
http://ideone.com/coXeTG

Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll