•
•
•
•
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
![]() |
•
•
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation:
Rep Power: 4
Solved Threads: 4
Well,
Here is the problem right here: 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: 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
Here is the problem right here:
printf("ENTER ARRAY ELEMENTS:");
scanf("%d",&a[i]);
fflush(stdin);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]);- 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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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......
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......
•
•
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation:
Rep Power: 4
Solved Threads: 4
Well,
There was a small error with one of your if statements: 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: Here is the full code below. It seems to work fine for me: Here is the output of a test:
- Stack Overflow
There was a small error with one of your if statements:
if (flag=1)if (flag==1)
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; }
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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......
???????????
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......
???????????
•
•
Join Date: Sep 2004
Location: Overflow State
Posts: 183
Reputation:
Rep Power: 4
Solved Threads: 4
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: Output of example:
- Stack Overflow
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; }
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
IRC
Channel: irc.daniweb.com
Room: #c, #shell
•
•
Join Date: Dec 2003
Location: Nashville, TN
Posts: 2,333
Reputation:
Rep Power: 11
Solved Threads: 101
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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......
•
•
Join Date: Oct 2004
Posts: 47
Reputation:
Rep Power: 4
Solved Threads: 0
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........
{
_______________________
_________________________
}
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........
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Other Threads in the C Forum
- Previous Thread: Help with strings and file directory tree
- Next Thread: Fatal Error C010?



Linear Mode