1

I am trying to insert a value "item" in a sorted linear array at position "pos" when i take input in the array using a initialization list the code works fine .. however when i take input using a for loop the code does not work

#include<iostream>
using namespace std;
int main()
{
const int size = 10;
int num[size];
int num[size] = {1,2,3,4,5,6,7,8,9,10}; //a sorted linear array
int item;
int pos;
int i;

/*

for(int a=0;a<size;a++)
{
    cin>>num[a];

}
*/

cout<<"Enter item"<<endl; //insert this item in array
cin>>item;
cout<<"Enter Position To Insert"<<endl;

cin>>pos;

i=size-1;
while(i>=pos)
{
    num[i+1]=num[i];
    i=i-1;
}
num[i+1] = item;

cout<<endl;
cout<<"array after insertion"<<endl;
for(int b=0;b<size;b++)
{

    cout<<num[b]<<endl;
}


system("pause");
return 0;
}

the for loop causing the problem

for(int a=0;a<size;a++)
{
    cin>>num[a];

}
  • 1
    You cannot. Just use `std::vector`. – πάντα ῥεῖ Feb 17 '16 at 16:32
  • i know ! thanks ! but this was a homework problem .. i got it to work but it just doesn't work .. wanted to know what's the problem –  Feb 17 '16 at 16:33
  • 2
    If you have an array of 10 items, and you need to insert 1, then you've overflowed the array (assigning to `num[size]`) with the code you have (you would need to drop the last item from the array). – crashmstr Feb 17 '16 at 16:34
  • 1
    Don't use `std::endl` unless you need the extra stuff that it does. `'\n'` ends a line. – Pete Becker Feb 17 '16 at 16:35
  • "however when i take input using a for loop the code does not work". What you mean it does not work? It does not compile? It gives the wrong result? "the for loop causing the problem": What is the problem with it? What do you expect and what do you get? Please be more precise. What _exactly_ is the problem? – dingalapadum Feb 17 '16 at 16:40
  • when i use initializer list the insertion occurs at the particular position .. when i use for loop the program does not insert a value in the array at particular position –  Feb 17 '16 at 16:43
  • @MuhammadUmarTariq So the `for` loop does not work because you did not write the `for` loop in a way that it inserts the values in a sorted order? So try to write that code then ask questions about why your attempt is not working. – crashmstr Feb 17 '16 at 16:51

1 Answers1

0

This code snippet

i=size-1;
while(i>=pos)
{
    num[i+1]=num[i];
    i=i-1;
}
num[i+1] = item;

is wrong. First of all pos can be greater than or equal to size In this case none element should be inserted in the array. However according to the last statement of the code snippet

num[i+1] = item;

num[size] will be asssigned with item.

Again within the loop for initial value of i equal to size - 1 there is an attempt to assign num[size] element

    num[i+1]=num[i];

The code snippet can be rewritten for example the following way

if ( pos < size )
{
    i = size;

    while ( --i != pos ) num[i] = num[i-1];

    num[i] = item;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335