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

S

#### Siva Praveen

#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;
}

}

S

#### Siva Praveen

#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+1]=key;

}

}

V

#### Victor Bazarov

#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;
}

}

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

S

#### Stefan Ram

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

When I was young, we had to compare all those sort algorithms:
bubble sort, heap sort, insertation sort, quick sort, ....

P

#### Paul N

#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);

Change "k>0" to "k >= 0", otherwise you won't change the first element.
{

A[k+1]=A[k];

k=k-1;

}

A[k]=key;

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.

J

#### Jorgen Grahn

Until the code grows and you have to revert it. Better to choose a
standard and stick to it, even if that means a whopping 11 more key
presses.

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.

O

#### Osmium

Juha Nieminen said:
Jorgen Grahn said:
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.

For some reason the majority of beginners, and even some more
experienced C++ programmers have this strange notion that the
"std::" prefix makes the code harder to read. I'm my experience
it's the exact opposite: It makes the code easier to read and
understand.

#define English eng

eng::I eng::continue eng::to enf:;be eng::astonished eng::by eng:: how
eng::many eng:eople
eng::can eng::tolerate eng::clutter .

J

#### Jorgen Grahn

Juha Nieminen said:
Jorgen Grahn said:
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.

For some reason the majority of beginners, and even some more
experienced C++ programmers have this strange notion that the
"std::" prefix makes the code harder to read. I'm my experience
it's the exact opposite: It makes the code easier to read and
understand.

#define English eng

eng::I eng::continue eng::to enf:;be eng::astonished eng::by eng:: how
eng::many eng:eople
eng::can eng::tolerate eng::clutter .

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

W

#### woodbrian77

/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'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

Ö

#### Öö Tiib

Juha Nieminen said:
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.

For some reason the majority of beginners, and even some more
experienced C++ programmers have this strange notion that the
"std::" prefix makes the code harder to read. I'm my experience
it's the exact opposite: It makes the code easier to read and
understand.

#define English eng

eng::I eng::continue eng::to enf:;be eng::astonished eng::by eng:: how
eng::many eng:eople
eng::can eng::tolerate eng::clutter .

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

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

V

#### Victor Bazarov

[...]
Fourthly: Honestly, how much do you *really* have to write 'std::' in
your average code? Is the minuscule saving in space really worth the
decrease in the readability and amount of information that the prefix
carries with it?

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

J

#### Jorgen Grahn

[...]
Fourthly: Honestly, how much do you *really* have to write 'std::' in
your average code? Is the minuscule saving in space really worth the
decrease in the readability and amount of information that the prefix
carries with it?

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.

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.)
Honestly, how much do we *really* have to continue having those
religious debates here?

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

I

#### Ian Collins

Victor said:
[...]
Fourthly: Honestly, how much do you *really* have to write 'std::' in
your average code? Is the minuscule saving in space really worth the
decrease in the readability and amount of information that the prefix
carries with it?

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?

Not as long as you don't start writing "::std::"

V

#### Victor Bazarov

Victor Bazarov said:
[...]
Fourthly: Honestly, how much do you *really* have to write 'std::' in
your average code? Is the minuscule saving in space really worth the
decrease in the readability and amount of information that the prefix
carries with it?

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?

I always give rational reasons why using the prefix improves the code.
The opposition only gives extremely subjective emotional reasons which
often feel like based on prejudice and stubbornness rather than
rationality.

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