User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 392,050 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,333 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 12903 | Replies: 49
Reply
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: plz help me out!!!!!!!

  #41  
Nov 2nd, 2004
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[i]. 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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: plz help me out!!!!!!!

  #42  
Nov 2nd, 2004
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......
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: plz help me out!!!!!!!

  #43  
Nov 2nd, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: plz help me out!!!!!!!

  #44  
Nov 2nd, 2004
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......
???????????
Reply With Quote  
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation: Stack Overflow is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Stack Overflow's Avatar
Stack Overflow Stack Overflow is offline Offline
C Programmer

Re: plz help me out!!!!!!!

  #45  
Nov 2nd, 2004
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
Following the rules will ensure you get a prompt answer to your question. If posting code, please include BB [code][/code] tags. Your question may have been asked before, try the search facility.

IRC
Channel: irc.daniweb.com
Room: #c, #shell
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)

  #46  
Nov 2nd, 2004
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.
Alex Cavnar, aka alc6379
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)

  #47  
Nov 2nd, 2004
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......
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)

  #48  
Nov 2nd, 2004
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........
Reply With Quote  
Join Date: Oct 2004
Posts: 47
Reputation: galmca is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
galmca galmca is offline Offline
Light Poster

Re: MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)

  #49  
Nov 3rd, 2004
plz help me out guys..........
Reply With Quote  
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Rep Power: 11
Solved Threads: 101
Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: MERGED: Deleting duplicates in an array (plz help me out!!!!!!!)

  #50  
Nov 3rd, 2004
Originally Posted by galmca
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.
Alex Cavnar, aka alc6379
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 11:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC