Ask Experts » Computers » Software »

Writing Algorithm in C

Date: 30 Nov 2009   Group: Computers    Category: Software   


How to write an algorithm in C program to delete duplicate elements from an array?


Find Answers

Author: Poornima    01 Dec 2009      Member Level: Silver     Points : 3  (Rs 1)    Voting Score: 0

Hope this code helps to solve the problem,



void rmdup(int *array, int length)
{
int *current , *end = array + length - 1;

for(current = array + 1;array < end;array++,current=array+1 )
{
while ( current < end )
{
if ( *current == *array )
{
*current = *end--;
}
else
{
current++;
}
}
}}

Author: Jayaramachandaran    02 Dec 2009      Member Level: Silver     Points : 5  (Rs 3)    Voting Score: 0

hi this is jayaram,

ALGORITHM TO REMOVE THE DUPLICATE NUMBERS IN AN ARRAY:

*HEADER FILE DECLARATION:
#include
#include

*MAIN FUNCTION
void main()
{
*DECLARATION :
int a[10],i,j,k,n,c=1;

*GETTING ARRAY OF ELEMENTS FROM THE USER:
printf("\nEnter the Array size:");
scanf("%d",&n);
printf("\nEnter the array elements");
for(i=0;iscanf("%d",&a[i]);

*REMOVING THE DUPLICATE ELEMENTS FROM THE ARRAY
for(i=0;i{
for(j=i+1;j{
if(a[i]==a[j])
{
for(k=j;k<(n-c);k++)
a[k]=a[k+1];
n=n-1;
j=j-1;
}
}
}

*THE NEW ARRAY ELEMENTS WITHOUT DUPLICATION :
printf("\nThe new array elements are follows\n");
for(i=0;iprintf("%d\n",a[i]);
getchar();
}


this removes all the duplicate numbers in the given array.




Author: Sunil Saharan    02 Dec 2009      Member Level: Gold     Points : 12  (Rs 10)    Voting Score: 0

Hello Friend,
You are asking about the algorithm so the required algorithm may be as below.
Step 1. Start
Step 2. Declare variables two more than that of number of elements in array.
( say number of elements in array is 5 ). Declare 7 variables viz a,b,c,d,e,f,g. It is better to declare an array and two variables but you can declare all variables. Initialize all variable with 0 value.
Step 3. Now add the value of array elements in variables. Something like
a<-array[0] and so on as
b<-array[1]
Step 4. After completing this step your last two declared variables will be still 0.
Set second last variable to the number of elements in this case it is 5.
f<-5
Step 5. Repeat step 6 to 8, 5(f) time.
Step 6. Initialize last variable, g in this case with first array element, a here. And compare g with each array element. If the variable g matches with any initialize the element to 0. Please note that the initial array must not have any 0 before if it has, set the value to -1 for duplicate element or any other value that is not present in array.
Step 7. After completing the loop set g to second array element and repeat step 6.
Step 8. Repeat above steps for all array elements.
This is the exact algorithm.
In exact form it looks like this
Start
int a,b,c,d,e,f,g<-0
a<-array[0]
b<-array[1]
c<-array[2]
d<-array[3]
e<-array[4]
f<-5
g<-a
for(i<-0, i<6,i++)
{if g=array[i]
array[i]<-0}

End
this is complete algorithm for the above problem.

Author: Mandar    07 Dec 2009      Member Level: Silver     Points : 5  (Rs 3)    Voting Score: 0

The algorithm-

Step 1. Input the Array.

Step 2. Arrange the Array in decreasing or increasing order as you wish but with a constraint that include a equal to condition in that now from here there can be two ways.

Step 2.a Now as you go on arranging the numbers in decreasing order you can keep deleting a number every time you see that the equal to condition has become true OR

Step 2.b you can let the array get arranged and then check every element to its next element one by one if they are equal then delete that entry and shift the whole array by one (YOU CAN WRITE A SIMPLE PROGRAM TO SHIFT THE ARRAY BY ONE ) every time an equal value is encountered the one of the value gets deleted and then array shifting put it in a loop until whole array is checked.

Step 3. The duplication in array has been removed.

Step 4. Display the Array

Step 5.End the Algorithm.

Answers      





Post answer or solution to "Writing Algorithm in C"

You can earn revenue online from Google AdSense and our revenue sharing programs by post valid, self written answers for the problem Writing Algorithm in C




You must Sign In to post a response.


Next Question: How to create an application?
Previous Question: Laptop Trouble
Return to Question Index
Ask Question to Expert
Category: Software

 
Related Questions
Popular Tags   What are tags ?   Search Tags  

Awards & Gifts
Active Members
TodayLast 7 Daysmore...

ISC Technologies, Kochi - India. Copyright © All Rights Reserved.