>im really sorry once again......if i made u feel like that.......plz forgive me......
I accept your appology. I hope that you continue to come here to learn, preferably after you've learned all of the rules, and eventually give back to the community by way of answering questions when you've gained some more experience.

>im not a girl like what u thought
I don't recall making that assumption. In fact, I tend--like everyone else--to assume that everyone is male.

thx narue......thx for accepting my apology...i really appreciate that....and yes i will continue to come here to learn from the very intelligent and brilliant teachers like u,so that i can learn about various things.....and i'll hopefully try to solve other people problems too.....

hi
i have just 1 question could any of u plz solve this.,it is.........TO DELETE DUPLICATE ELEMENTS IN AN ARRAY.......i have done like this......

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,flag;
clrscr();
printf("ENTER ARRAY ELEMENTS:");
scanf("%d",&a[i]);
fflush(stdin);
for(i=0;i<5;i++)
 {
for(j=i;j<5;j++)
    {
      if((i!=j)&&(a[i]==a[j]))
         {
          printf("duplicate exists:%d",a[j]);
          flag=1;
          break;
         }
    }
  if(flag=1)
  for(k=j;k<4;k++)
   {
    a[k]=a[k+1];
    printf("\nARRAY AFTER DELETION:");
    for(i=0;i<4;i++)
    printf("\n%d",a[i]);
   }
 }
getch();
}

but when i run this code although there is no compilation error but when i run this...then....
ENTER ARRAY ELEMENTS:
10
20
10
30
40
THEN IT PRINTS:::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
30
ARRAY AFTER DELETION:
10
20
30
40
so why does it do that way then?why is it printing twice?the first time it is printing wrongly and the second time it is printing the desired output....is there any mistake in my code?......if there is then plz help me out.....

but when i run this codeby entering five values as.....
ENTER ARRAY ELEMENTS:
10
20
30
10
40
then it prints::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
40
now it is printing rightly......why?
whereas when i put the duplicate element that's 10 in the third place above it was printing twice first wrongly and second rightly and now when the duplicate element that's 10 is positioned in the fourth place is printing rightly....why?
plz help me out.......tell me what was wrong in that code?or what was lacking?how can i make it run correctly for any duplicate element positioned anywhere in an array......

hi
i have just 1 question could any of u plz solve this.,it is.........TO DELETE DUPLICATE ELEMENTS IN AN ARRAY.......i have done like this......

Code:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,k,flag;
clrscr();
printf("ENTER ARRAY ELEMENTS:");
scanf("%d",&a);
fflush(stdin);
for(i=0;i<5;i++)
{
for(j=i;j<5;j++)
{
if((i!=j)&&(a==a[j]))
{
printf("duplicate exists:%d",a[j]);
flag=1;
break;
}
}
if(flag=1)
for(k=j;k<4;k++)
{
a[k]=a[k+1];
printf("\nARRAY AFTER DELETION:");
for(i=0;i<4;i++)
printf("\n%d",a);
}
}
getch();
}

but when i run this code although there is no compilation error but when i run this...then....

ENTER ARRAY ELEMENTS:
10
20
10
30
40
THEN IT PRINTS:::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
30
ARRAY AFTER DELETION:
10
20
30
40

so why does it do that way then?why is it printing twice?the first time it is printing wrongly and the second time it is printing the desired output....is there any mistake in my code?......if there is then plz help me out..... :sad:

the mistake is in the loop, edit it and u get the output correctly.

but when i run this codeby entering five values as.....
ENTER ARRAY ELEMENTS:
10
20
30
10
40
then it prints::
duplicate element exists:10
ARRAY AFTER DELETION:
10
20
30
40
now it is printing rightly......why?
whereas when i put the duplicate element that's 10 in the third place above it was printing twice first wrongly and second rightly and now when the duplicate element that's 10 is positioned in the fourth place is printing rightly....why?
plz help me out.......tell me what was wrong in that code?or what was lacking?how can i make it run correctly for any duplicate element positioned anywhere in an array......

could u plz tell me what should i have done...i haven't been able to come up with the answers......i really don't know what to do....now in this code.......plz tell me what should i have done........so that i can run my program rightly.....plz help me out....

hey
u have to close the for loop for(k=j;....)
before the other for loop for(i=0;.....)
thats ur mistake...
cool...
:lol:

i have tried using that what u just said that i have to close the for loop for (k=j;....)
before the for loop for(i=....)
but it is still giving very weird output and not the desired output what i want.........
so plz tell me where r u actually asking me to put the braces on.....im not getting where do u actually want me to put them.......plz guide me........

Well,

Here is the problem right here:

printf("ENTER ARRAY ELEMENTS:");
scanf("%d",&a[i]);
fflush(stdin);

So far you call scanf() onces only. Ask for one integer and send it to a. So far i is nothing. No loop, not even zero; or shall I say unitialized. For fflush(), there is no need to flush the input. Why? It is undefined behavior because the standard only provides words to make sense out of fflush() with output streams, not input streams. The reason this is so is because the stdio buffer and the operating system buffer are usually two different buffers. Furthermore, stdio may not be able to get to the OS buffer, because for instance, some OSs don't pass the buffer along until you hit return. So, normally, you'd either have to use a non-standard non-portable extension to solve this, or write a function to read until the end of the line.

Futhermore, fflush() does not need to be called unless you have any output on your stream that needs to be flushed. Here is a brief explanation of fflush(): If the given stream has been opened for writing operations the output buffer is phisically written to the file. When a file is closed all the buffers associated with it are automatically flushed.

Back to our first point, your scanf() is not getting all of the array elements. You could simply fix it by the following:

for (i = 0; i < 5; i++)
	scanf("%d", &a[i]);

That way you will enter 5 elements, all seperated by hitting the enter button. If you wanted to you could just use fgets(), and then sscanf() that after the fact; though it does become time consuming.


- Stack Overflow

But My Problem I Still There.......it Is Still Not Giving The Desired Output Even If I Don't Use Fflush(stdin) Over There........so How Can I Make My Code Run....if Suppose...if Enter 5 Elements Like...10 20 10 30 40...then It Should Print Duplicate Exisits :10 Which It Is Printing But It Should Print...array After Deletion: 10 20 30 40.....which It Is Printing In My Code As :array After Deletion:10 20 30 30
Array After Deletion:10 20 30 40
So Why Is Is Printing 2 Times...where The First Time It Is Wrongly Printing And The Second Time It Is Correct.......plz Help Me Out......

Well,

There was a small error with one of your if statements:

if (flag=1)

That will cause flag to always equal 1 instead of checking if flag is 1. The way we do that is to use the == operator. That checks for equality:

if (flag==1)

Here is the full code below. It seems to work fine for me:

int main() {
	int a[5],i,j,k,flag;

	printf("ENTER ARRAY ELEMENTS:");
	for (i = 0; i < 5; i++)
		scanf("%d",&a[i]);

	for(i=0;i<5;i++) {
		for(j=i;j<5;j++) {
			if((i!=j)&&(a[i]==a[j])) {
				printf("duplicate exists:%d",a[j]);
				flag=1;
				break;
			}
		}
		if(flag==1) {
			for(k=j;k<4;k++) {
				a[k]=a[k+1];
				printf("\nARRAY AFTER DELETION:");
				for(i=0;i<4;i++)
					printf("\n%d",a[i]);
			}
		}
	}
	getch();

	return 0;
}

Here is the output of a test:

ENTER ARRAY ELEMENTS:10
20
30
10
50
duplicate exists:10
ARRAY AFTER DELETION:
10
20
30
50

- Stack Overflow

yes what u said is correct but just tell me 1 thing.........u have taken 5 inputs as 10 20 30 10 50 so it is printing correct but just notice if u take 10 20 10 30 40
then what will it print?it will print wrong?
i have checked so many times......that is my main question to u.....
it will print in that case as....10 20 30 30 and 10 20 30 40.....now how is that possible.......it means there must be some mistake.........so what is that?plz tell me now in this case......
???????????

Ah,

This makes sense. What is happening is that you find a duplicate and then flag it. What if there are 2 of the same ones? The program can't deal with it unless you remove them right when you find them. Here is a new look of the program:

#include <stdio.h>
#include <conio.h>

void deleteDup(int a[], int size, int location) {
	int m;

	for(m = location; m < size; m++)
		// If the next array is out of bounds, set to 0
		if (m+1 >= size)
			a[m] = 0;
		else
			a[m] = a[m+1];
}

int main() {
	int a[5],i,j;

	printf("ENTER ARRAY ELEMENTS:");
	for (i = 0; i < 5; i++)
		scanf("%d",&a[i]);

	for(i=0;i<5;i++) {
		for(j=i;j<5;j++) {
			if((i!=j)&&(a[i]==a[j])) {
				printf("duplicate exists:%d\n",a[j]);
				deleteDup(a, 5, j);
				break;
			}
		}
	}

	printf("\nARRAY AFTER DELETION:");
	for(i=0;i<4;i++)
		printf("\n%d",a[i]);

	getch();

	return 0;
}

Output of example:

ENTER ARRAY ELEMENTS:10
20
10
30
40
duplicate exists:10

ARRAY AFTER DELETION:
10
20
30
40

- Stack Overflow

Galmca:

Please keep one topic all in the same thread. You had already started and discussed for some length in this thread about this exact issue; please stay in this thread.

I went ahead and merged your two threads together.

ahh well i really appreciate ur concern by writing that code for me.......but i will be highly thankful to u if u could just make some necessary changes in MY code above coz im pretty clear with the working of my code....and this code by u..im not able to understand that right..... it's a bit tough for me to understand.... so could u plz tell me what were the necessary changes that i had to make in my code to run if the situation was to print array with 5 elements like>:10 20 10 30 40.........? plz reply me......

i didn't understand what u did above in the code of urs;;;void duplicate(int a[],size,location)
{
_______________________
_________________________
}
this whole thing i diodn't understand what is it doing? and why do we need these lines...?
so plz tell me what was i doing wrong in MY code earlier......what should i have done.....to make it run for any duplicate value positioned anywhere in the array ......
plzzzz........

plz help me out guys..........

plz help me out guys..........

Please stop bumping your thread. The people interested in your topic will find this discussion, and continue with it. You're being very inconsiderate to other posters with your behavior.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.