a small problem......plz help me out guys!!!!!!

This is a discussion on a small problem......plz help me out guys!!!!!! within the C Programming forums, part of the General Programming Boards category; hi i have just 1 question could any of u plz solve this.,it is.........TO DELETE DUPLICATE ELEMENTS IN AN ARRAY.......i ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    63

    a small problem......plz help me out guys!!!!!!

    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[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......

    plz help me out!!!!!!!


  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Judging by the number of red marks next to your name, I'm betting you've been told this several times before. Use CODE tags.

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Well,

    Hence the thread here... I'm sure there is a perfect explanation why your program performs the way it does.

    Let's think of the general concept of deleteing an array. What we would do is:
    Code:
    loop from the next spot forward
    	move all the indexes one over to the left
    This a simple overview of how your code works. However what happens when you do move everything over? Do you take into consideration of how many duplicates are found, or do you break after the first one was found? Lastly, when you print your new array list, how do you know how many to display?


    - Stack Overflow
    Last edited by Stack Overflow; 11-03-2004 at 11:40 AM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    so what do i have to do here ....plz find out the necessary changes that r required to make the program run correctly.........plz help me out here ..i have written the entire code here........but don't know where to go now........to make it run correctly for every duplicate placed anywhere.......? plz help me out here guys..............

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Step 1) Use code tags
    Step 2) Don't use void main
    Step 3) Don't use fflush(stdin)
    Step 4) Use meaningful variable names
    Step 5) Use full words and sentances in your questions

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Such wise words Thantos.

    galmca has been told these instructions for quite sometime. Maybe he's in it for himself, and/or is looking for a handout. On the mentioned forum above, where he posts the same like question, he quotes this on October 28th, 2004:
    Quote Originally Posted by galmca
    im really sorry but im new to this site so don't know how exactly it works
    I've seen you've been posting here since September 29th, almost one month apart. Forums are identical. They all have Register, Post Reply, and User Control Panels. Each of these forums have rules, including code tag explanations.

    1) Use code tags
    Try copying this and pasting it in a post:

    [code]void foo();[/code]

    It makes a difference.

    2) Don't use void main
    Your question may be: ...but why?
    • Because the standard says so.
    • Because the startup routines that call main could be assuming that the return value will be pushed onto the stack. If main() does not do this, then this could lead to stack corruption in the program's exit sequence, and cause it to crash.
    • Because you are likely to return a random value to the invokation environment. This is bad, because if someone wants to check whether your program failed, or to call your program from a makefile, then they won't be able to guarantee that a non-zero return code implies failure.
    Overall, declaring a function as void does not merely shut off or rearrange warnings: it may also result in a different function call/return sequence, incompatible with what the caller (in main's case, the C run-time startup code) expects. The ANSI standard says "no" to void main(), which should be an end of it.

    Side note: Sadly, a number of beginners' books on C have used void main(void) in all of their examples, leading to a huge number of people who don't know any better.

    3) Don't use fflush(stdin)
    Calling fflush() like that is undefined behavior. 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.

    Lastly, 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.

    4) Use meaningful variable names
    Of course. Ever wonder why your program is so hard to read or understand? Declaring meaningful variable names can help you in remembering why that variable is there. Does f help you remember or does frac or fraction when dealing with a fraction of 2 or more numbers?

    5) Use full words and sentences in your questions
    Definetely. Expressing your question clearly and well is important. If you can't be bothered to do that, we can't be bothered to pay attention. Spend the extra effort to polish your language. It doesn't have to be stiff or formal. But it has to be precise; there has to be some indication that you're thinking and paying attention. Spell, punctuate, and capitalize correctly.

    If you cannot complete the steps above, then you are disgraced to the programming community. You will continually be accosted and ostracized for your stupidity and ignorance. You do not seem to understand the defintion of the word "learn".


    - Stack Overflow
    Last edited by Stack Overflow; 11-04-2004 at 03:35 PM. Reason: Note at the bottom.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    63
    could any 1 of u plz solve this problem of mine?????
    i have written the whole code.........just let me know what's wrong in it while it is running?????pz let me know ........reply plz.........

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Ok Ill speak in your language:

    plz read the above comments......plz read them now.....plz post back and correct what was said.

  9. #9
    Banned
    Join Date
    Aug 2004
    Posts
    128
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    	vector <int> iVector;
    	iVector.push_back(10);
    	iVector.push_back(20);
    	iVector.push_back(20);
    	iVector.push_back(20);
    	iVector.push_back(20);
    	for(int iNumber = 0; iNumber != iVector.size(); iNumber++)
    	{
    		for(int ianotherNumber = iNumber+1; ianotherNumber !=  iVector.size(); ianotherNumber++)
    		{
    			if(iVector[iNumber] == iVector[ianotherNumber])
    			{
    				iVector.erase((iVector.begin() + ianotherNumber));
    				ianotherNumber--;
    			}
    		}	
    	}
    	cout << endl << endl;
    	for(int i = 0; i != iVector.size(); i++)
    	{
    		cout << iVector[i] << endl;
    	}
    	cin.get();
    	return 0;
    }
    OutPut :

    Code:
    10
    20
    Something i through together real quick, im just barely learning
    how to use vectors, this functions properly, for the most part.
    But i didnt do that many tests.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You've just posted C++ code to a C problem. But hey, they can't be too picky now, can they? After all, just look at their posts.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2004
    Posts
    128
    lol yea i forgot which section i was in.

    its ok though, for all we know he wanted c++ :/

  12. #12
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Just a few notes in the code...

    Code:
    #include<stdio.h>
    main()
    {
        int a[5],i,j,k,flag;
        
        printf("ENTER ARRAY ELEMENTS:");
        scanf("%d",&a[i]); /* i is not initialized */
        
        fflush(stdin);  /*  you've been shown before what to do here */
        
        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) /* There are 2 problems here.  1st, you've
                            used an assignment operator when you're 
                            performing a comparison.
                            
                            2nd, what defines the block of code to
                            be executed if flag does equal 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();
    }

  13. #13
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    2nd, what defines the block of code to be executed if flag does equal 1?
    The for loop block that is after the if statement.

  14. #14
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Quote Originally Posted by galmca
    could any 1 of u plz solve this problem of mine?????
    i have written the whole code.........just let me know what's wrong in it while it is running?????pz let me know ........reply plz.........
    Since, hereby, you have ignored and disregarded our simple requests stated prior on many occasions, you are no longer eligable to recieve assistance/guidance or even be considered being a part of this community. It would simply just be best for you to walk away quietly.

    You obviously do not seem to heed to our requests, which has been made abudantly clear, why then do you expect anyone to be concerned with the predicament you find yourself in.

    Now that you have proven that you are incapable of following simple standard procedures, it is absurd to believe that you will be able to comprehend any help or assistance given.

    It will be in your best interest to read, comprehend, and execute the material found at this link: How To Ask Smart Questions

    If the message in this post is not clear and you cannot grasp the importance of it, continue making simple-minded posts and you will recieve posts like this in very clear and vivid language describing your incompetence.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a very small problem, i'm new... please help?
    By Uberverse in forum C++ Programming
    Replies: 9
    Last Post: 11-10-2007, 09:44 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. plz help me with run problem
    By onlinegeek in forum C++ Programming
    Replies: 9
    Last Post: 12-14-2005, 10:46 PM
  4. plz help me fix this problem
    By Joe100 in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2003, 01:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21