Can u guys tell me what's wrong in my insertion code

Discussion in 'C++' started by Siva Praveen, Apr 20, 2014.

  1. Siva Praveen

    Siva Praveen Guest

    #include <iostream>
    using namespace std;
    int main()
    {
    int A[10];
    cout<<"enter 10 elements \n";
    for(int i=0;i<10;i++)
    cin>>A;
    for(int j=2;j<10;j++)
    {
    int key=A[j];
    int k=j-1;
    while(k>0 && A[k]>key);
    {
    A[k+1]=A[k];
    k=k-1;
    }
    A[k]=key;
    }

    }
     
    Siva Praveen, Apr 20, 2014
    #1
    1. Advertisements

  2. Siva Praveen

    Siva Praveen Guest

     
    Siva Praveen, Apr 20, 2014
    #2
    1. Advertisements



  3. Can u guy tell us what's ur code supposed 2 accomplish? You called it
    "insertion code". "Insertion" where?

    IOW, you tell us what it's supposed to do, what it does and how it
    differs from what's needed, and we could try to help you correct the
    problem. Right now I can see at least one problem with your code: it
    doesn't output any *result*.

    V
     
    Victor Bazarov, Apr 20, 2014
    #3
  4. Siva Praveen

    Stefan Ram Guest

    When I was young, we had to compare all those sort algorithms:
    bubble sort, heap sort, insertation sort, quick sort, ....
     
    Stefan Ram, Apr 20, 2014
    #4
  5. Siva Praveen

    Paul N Guest



    Change "k>0" to "k >= 0", otherwise you won't change the first element.
    Change this to A[k+1]=key; as you're now one below the element you copied. For example, if the list was already in order, you wouldn't want to change anything, but k is one less than j.
    Alternatively, if you don't want k to go negative, you can alter different bits of the program instead.
     
    Paul N, Apr 21, 2014
    #5
  6. Siva Praveen

    Jorgen Grahn Guest

    I think Juha just wants the same thing as I do: don't hide the std
    namespace unless you have really good reasons to[1]. The character
    count argument is IMHO a red herring.

    /Jorgen

    [1] I never find such reasons. I do 'using std::string' or something
    rather often, but I never see a need to pull in all of it.
     
    Jorgen Grahn, Apr 23, 2014
    #6
  7. Siva Praveen

    Osmium Guest

    #define English eng

    eng::I eng::continue eng::to enf:;be eng::astonished eng::by eng:: how
    eng::many eng::people
    eng::can eng::tolerate eng::clutter .
     
    Osmium, Apr 24, 2014
    #7
  8. Siva Praveen

    Jorgen Grahn Guest

    That's IME not at all what code looks like when you don't hide 'std',
    so I wonder if you ever tried it?

    /Sometimes/ I get std clutter, but it tends to be std::string or
    std::vector, and then I just locally pull those ones in. I don't want
    std::find() or std::copy() to be called just find() or copy().

    I suppose that with C++11 (which I don't use yet) I could have reduced
    the clutter as much or more by using 'auto' -- it's often something
    like std::vector<std::string>::const_iterator which annoys me.

    /Jorgen
     
    Jorgen Grahn, Apr 24, 2014
    #8
  9. Siva Praveen

    woodbrian77 Guest

    I'm happy to be using C++ 2014. Hope you can switch
    to a newer version soon. And I wonder if 2017 C++ will
    have modules?

    Brian
    Ebenezer Enterprises - Heavenly code.
    http://webEbenezer.net
     
    woodbrian77, Apr 24, 2014
    #9
  10. Siva Praveen

    Öö Tiib Guest

    I usually typedef the container template instantiations with a type
    name that better matches its local purpose:

    class Person
    {
    public:
    typedef std::vector<std::string> Names;
    // ...
    };

    With some hugely configurable masterpieces among templates
    ('boost::multiindex::multi_index_container' anyone?) there are just no
    sane ways to behave otherwise; with simple collections it also helps me
    to postpone detail considerations ('list' or 'set' or 'deque' or 'vector'?)
    into later stages and just take 'std::vector' as default "collection".
    With or without 'auto' available I may want still to use
    'Person::Names::const_iterator' for clarity sometimes. It is sometimes clear
    (as some standalone variable) and sometimes clutter (like as cycle iterator).
     
    Öö Tiib, Apr 25, 2014
    #10
  11. How many times do we need to step on that rake, FCOL? Readability is in
    the eye of the beholder. You can't expect everybody to subscribe to
    your own understanding what improves it or destroys it. Honestly, how
    much do we *really* have to continue having those religious debates here?

    V
     
    Victor Bazarov, Apr 28, 2014
    #11
  12. Siva Praveen

    Jorgen Grahn Guest

    True, you can not ... but you can explain how you see it and perhaps
    convince /someone/. Juha explained very carefully what /he/ gets out
    from having std:: in his code.

    (Perhaps I'm biased because I often agree with him, not only about
    /what/ to do but also the rationale.)
    Forever, I'm afraid. It's boring seeing the same things pop up over
    and over again, but I think it's basically healthy.

    /Jorgen
     
    Jorgen Grahn, Apr 28, 2014
    #12
  13. Siva Praveen

    Ian Collins Guest

    Not as long as you don't start writing "::std::" :)
     
    Ian Collins, Apr 28, 2014
    #13
  14. I believe your honest impression, Juha. And considering that you are
    aware of what always happens, why insist on doing it? Are there no
    other issues that might be discussed with a more definite and positive
    outcome?

    V
     
    Victor Bazarov, Apr 28, 2014
    #14
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.