GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Apr-2004, 14:16
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road

Deleting elements of arrays C++


New to this and was after some help/direction.

I'm writing a program (new to C++). I've to open a file, read in 20 chars, sort it alphabetically and then remove any duplicates.

I can get it to read in, sort and display the sorted array, but I can't work out the deletion bit. Any advice?

The source code is below for the programs. Any algorithm that would help would be a massive weight off the shoulders:-

CPP / C++ / C Code:
void getLetters()
{

int count;

ifstream inputFile; // like declaring a variable
inputFile.open("A:letters.dat");

count = 0;


inputFile >> GetTwentyLetters[firstElement]; // priming read

while ( count != lastElement )
{
count++;
inputFile >> GetTwentyLetters[count];
}

char GetTwentyLetters = count;

inputFile.close(); // always close the file when done with it





}



void sortLetters() 
{
/********* PASS 1: SORTING THE ARRAY *********/
int i,j;
char temp;
// For each element in the array
for(i=lastElement; i>=0; i--) 
{ //For each pair of elements in the array) 
for(j=0; j<i; j++) 

{//At each location, compare the two adjacent elements

if(GetTwentyLetters[j] > GetTwentyLetters[j+1]) 
{ 
// Swap elements as needed
temp = GetTwentyLetters[j];
GetTwentyLetters[j] = GetTwentyLetters[j+1];
GetTwentyLetters[j+1] = temp;
}
} 
}

}

I presume I have to move all elements down 1 position if there is a duplicated pair? But how?
Last edited by admin : 27-Jun-2007 at 20:31. Reason: Use [c] & [/c] for syntax highlighting
  #2  
Old 28-Apr-2004, 17:31
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Deleting the elements in an array is not possible. You'd have to use linked lists which involves use of pointers. If you are not familiar with pointers and linked lists, i suggest you do some reading.

One other way I can think of is while you are sorting your array, you can add an else if(GetTwentyLetters[j] == GetTwentyLetters[j+1]) statement after your current if(GetTwentyLetters[j] > GetTwentyLetters[j+1])
and if you find a duplicate, just replace it with '$' or something, and also place a condition that if you find duplicate '$' in your array, just leave it alone because there are bound to be more than one '$' in your array if you have many duplicates. and when you print your elements, just ignore the '$' characters if you want.

CPP / C++ / C Code:
// .....your code
if(GetTwentyLetters[j] > GetTwentyLetters[j+1])  //your if statement
{
     /*
      *
      your original code
      *
      */
      
}
		  
else if(GetTwentyLetters[j] == GetTwentyLetters[j+1]) 
             if(GetTwentyLetters[j] != '$') 
                   GetTwentyLetters[j+1]= '$';

//your code  .......

  #3  
Old 28-Apr-2004, 20:08
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello crazyman. Welcome to GIDForums. You can do this using arrays, but as you said, you need to move all of the elements down.

One thing you should do is null terminate the array to make this easier. So after you read in the chars, add:

CPP / C++ / C Code:
GetTwentyLetters[lastElement+1] = 0;

Make sure your array is large enough to hold this.

Then you can just index through, with something like:
CPP / C++ / C Code:
for(i=0;GetTwentyLetters[i];i++)
  if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
    for(j=i;GetTwentyLetters[j];j++)
      GetTwentyLetters[j] = GetTwentyLetters[j+1];

Thats untested, but it should give you an idea of how to do it.

BTW - did you have to use such a large variable name as GetTwentyLetters?

HTH,
d
  #4  
Old 29-Apr-2004, 08:39
machinated machinated is offline
Awaiting Email Confirmation
 
Join Date: Mar 2004
Location: victoria, canada
Posts: 324
machinated has a spectacular aura aboutmachinated has a spectacular aura about
Ah! I didn't even read the last part before! Sorry about that!

Quote:
I presume I have to move all elements down 1 position if there is a duplicated pair? But how?
  #5  
Old 29-Apr-2004, 14:38
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road
dsmith and machinated.


much appreciated. I've got it working.
both very useful tips you gave.

ended up using the code from dsmith. simple really when moving the elements up. I wasn't counting up an element, as I was starting the second nested for loop at 0 instead of 1.

you stars...
  #6  
Old 05-May-2004, 00:36
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road
Hi,

only thicko me again

I'm still having a few problems with this.
the program is this now giving me a few headaches just on the deletion part.

using test data FFFFEEEEDDDDCCCCBBAA,
should produce, ABCDEF, but instead gives me, ABCCDDEEFF

the basis of the program is as follows:

I think the problem is in function 3, void deleteDuplicates()
but I can't see what's going wrong. I thought I had it working, but as i put more test data through, it's showing a few errors.

any help greatly appreciated.
(sorry about the format of the text, i've had to copy/paste the program from notepad, as work won;t provide me with a complier


CPP / C++ / C Code:
					// Inclusion Of Header Files

#include <iomanip.h>
#include <stdlib.h> 
#include <iostream.h>
#include <fstream.h> // for file handling



					// Declaration of Constants

const char NumberOfLetters = 20;
const char firstElement = 0;
const char lastElement = NumberOfLetters -1;



					// Declaration of Global variables
char GetTwentyLetters[NumberOfLetters];
int vowelCount = 0;
int consonantCount = 0;


					// FUNCTION DECLARATIONS

/* Function 1 - getLetters
   Purpose of function - Get 20 letters from floppy drive letters.dat 
					   and place into an array
   Input: 20 letters from file a:letters.dat
   Output:20 letters of type 'char'*/
void getLetters();



/* Function 2 - sortLetters
   Purpose of function: Sort the letters within the array into alphabetical order,
   Input: 20 letters from array 'GetTwentyLetters'
   Output:20 Remaining letters held within the array in alphabetical order */
void sortLetters();


/* Function 3 - displayLetters
   Purpose of function: Delete duplicated letters held within the array
   Input: 20 letters from array 'GetTwentyLetters' in alphabetical order
   Output:Remaining letters held within the array in alphabetical order 
		  after all duplicated letters have been removed*/
void deleteDuplicates();


/* Function 4 - countLetters
   Purpose of function: Count the numbers of vowels and consonants
                        held within the array
   Input: Remaining letters from array 'GetTwentyLetters' in alphabetical order
   Output:numbers of vowels and consonants held within the array*/
void countLetters();


/* Function 5 - DisplayLetters
   Purpose of function - Display letters in alphabetical order. 
						 Display number of vowels & number of consonants.
   Input: Remaining letters held within the array in alphabetical order. 
		  Count of number of vowels, count of number of consonants 
   Output:Display letters in alphabetical order. 
		  Display number of vowels & number of consonants */
void displayLetters();





		/********	START OF MAIN   *********/
void main()
{
	
	// START OF FUNCTION CALLS
	getLetters();
	sortLetters();
	deleteDuplicates();
	countLetters();
	displayLetters();
	
}
		/*********	 END OF MAIN   **********/



// Execution of function 1: getLetters
/* Key attributes of function 1
   1. Array populated from file A:letters.dat
   2. Letters passed into array 'GetTwentyLetters[]' */

void getLetters()
{

  int count;

  ifstream inputFile; // like declaring a variable
  inputFile.open("A:letters.dat");

  count = 0;
  

  inputFile >> GetTwentyLetters[firstElement]; // priming read

  while (  count != lastElement )
  {
    count++;
    inputFile >> GetTwentyLetters[count];
  }

  
  inputFile.close(); // always close the file when done with it
  
    
}
  

// Execution of function 2: sortLetters
/* Key attributes of function 2
   1. Array searched and sorted into alphabetical order. Bubblesort method used.
   2. If letter is of greater ordinal value than the letter in j+1 element it will be 
      swapped*/

void sortLetters() 
{
				
	int i,j;
	char temp;
	
	// For each element in the array
	for(i=lastElement; i>=firstElement; i--) 

	{//For each pair of elements in the array)										
		for(j=firstElement; j<i; j++)					
		
		{//At each location, compare the two adjacent elements
			if(GetTwentyLetters[j] > GetTwentyLetters[j+1]) 
			{	
				// Swap elements as needed
				temp = GetTwentyLetters[j];
				GetTwentyLetters[j] = GetTwentyLetters[j+1];
				GetTwentyLetters[j+1] = temp;
			}
		}		
    }
	
}


// Execution of function 3: deleteDuplicates
/* Key attributes of function 3
   1. Array searched and duplicated items removed. Items are not 'deleted' as such. 
      The elements are moved down one position. */
void deleteDuplicates()
{
	int i,j;
	
	//For each pair of elements in the array)
	for(i=firstElement; i<=lastElement; i++) 
	{											
					
		
		//At each location, the two adjacent elements are compared
		if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
		{
			// if elements are the same, then elements are moved down one position
			for(j=i;GetTwentyLetters[j];j++)
			{
				GetTwentyLetters[j] = GetTwentyLetters[j+1];
			}
		}/*At each location, compare the element 
			  to determine vowel or consonant*/
	}
}
Last edited by dsmith : 05-May-2004 at 07:05. Reason: Please use [c] & [/c] for syntax highlighting
  #7  
Old 05-May-2004, 00:51
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 570
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
*ahem*... http://www.gidforums.com/t-689.html
__________________
-Aaron
  #8  
Old 05-May-2004, 03:03
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Yes,your function 3 is the problem.

CPP / C++ / C Code:
void deleteDuplicates()
{
int i,j;

//For each pair of elements in the array)
for(i=firstElement; i<=lastElement; i++) 
{ 


//At each location, the two adjacent elements are compared
if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
{
// if elements are the same, then elements are moved down one position
for(j=i;GetTwentyLetters[j];j++)
{
GetTwentyLetters[j] = GetTwentyLetters[j+1];
}
}/*At each location, compare the element 
to determine vowel or consonant*/
}
}


If you look at it carefully, this is what it is actually doing. lets say you have this data in your array:
AABBCCCDDD...
your loop compare the first element array[0] with the second array[1], and if its the same, it does THE SHIFTING, so this is what you got after the first loop,
ABBCCCDDD..
now, it compares array[1] with array[2] and do the shifting, produce this
ABCCCDDD
here it compares array[2] with array[3], same thing happens
ABCCDDD
but now is the problem, when comparing array[3] with array[4] and see the resul:
ABCCDDD... so I assume you can see the problem here..

To do this coorectly, Issuggest you to do something like this...

CPP / C++ / C Code:
void deleteDuplicates()
{
int i,j;
int tmp=0;
bool flag;
//For each pair of elements in the array)
for(i=firstElement; i<=lastElement; i++) 
{ 

flag=false;
//At each location, the two adjacent elements are compared
if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
{
flag=true;
tmp++;
}
if(!flag)
{
// if elements are the same, then elements are moved down one position
for(j=i-tmp;j<=lastElement;j++)
{
GetTwentyLetters[j] = GetTwentyLetters[j+tmp];
}
}

what the code does is that it keeps the loop running when it found duplicates and stops when no more duplicates and do the shifting.
notice I haven't tested this, its just a guide.. I'll try to test it when I got time later...
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #9  
Old 05-May-2004, 03:25
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Here is your code, I hope you can understand it. Its just a simple, you need analyse each loops and what happens on each loop..
CPP / C++ / C Code:
int i,j;
int tmp=0;
bool flag;

//For each pair of elements in the array)
for(i=0; i<=19; i++) 
{ 

	flag=false;
	//At each location, the two adjacent elements are compared
	if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
	{
		flag=true; //found duplicate
		tmp++;     //increment num of duplicates
	}
	if(!flag) //if no more duplicates
	{
		// the elements are moved down from i-tmp 
		for(j=i-tmp;j<=19;j++)
		{
			GetTwentyLetters[j] = GetTwentyLetters[j+tmp]; 
		}
	}

}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #10  
Old 06-May-2004, 07:28
the_crazyman the_crazyman is offline
Awaiting Email Confirmation
 
Join Date: Apr 2004
Posts: 11
the_crazyman is on a distinguished road
hi,

thanks for this.
I'm a bit confused with regards Boolean statements as I've never really used them before.
is there any other way of doing this without using the bool method, ie. writing out to a temporary array and then sending the temp array contents back into getTwentyLetters.

I was thinking that if I ran the loop again that runs around the array again and if it detects that the array still contains duplicates that it could call itself again.. Else, it just falls out of the function and continues to the next call. does this make sense ?? Or, alternatively am I talking pap? ??:
i.e. original code.

CPP / C++ / C Code:
void deleteDuplicates()
{
  int i,j;
  
for(i=firstElement; i<=lastElement; i++) 
  {    
    //At each location, the two adjacent elements are compared
    if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
    {
      // if elements are the same, then elements are moved down one position
      for(j=i;GetTwentyLetters[j];j++)
      {
        GetTwentyLetters[j] = GetTwentyLetters[j+1];
      }
    }
  }
   for(i=firstElement; i<=lastElement; i++)
     { 
       if(GetTwentyLetters[i] == GetTwentyLetters[i+1])
      {  
         deleteDuplicates();
      }
    
}
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with passing 3 arrays into a function tommy69 C Programming Language 14 07-Apr-2004 00:22
Problem multiplying arrays hellhammer C Programming Language 9 29-Mar-2004 16:32
Arrays Chazza C++ Forum 10 23-Jan-2004 22:19
pointers and arrays jack C Programming Language 4 15-Jan-2004 13:27
arrays in c wolfgangaz C Programming Language 1 26-Oct-2003 05:52

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:45.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.