-5

So i'm trying to insert a number into an array in ascending order and then print the array by using only pointer notation. I tried doing this by finding the position of where the number would be inserted and then I try to store all the values at that position and after in positions further down the array. Then I want to insert the number at it's proper position and then move all of the numbers back to their position+ 1. However I think I am missing something in my pointer notation because none of my checks are showing up, so my for loops arent even being used. Any help or advice would be appreciated.

using namespace std;
int main(int argc, char *argv[]) 
{
    int spot; // spot holder for the added number 
    int *pointer = NULL; 

    cout << "How many numbers do you want in your array" << endl; 
    int input; 
    cin >> input; 
    pointer = new int[input * 2 ];
    for (int index = 0; index < input; index ++)
    {
        cout << "Enter integer number" << index + 1 << endl; 
        cin >> *(pointer + index); 

    }
    for (int index = 0; index < input; index ++)
    {
        cout <<  *(pointer + index); 
    }
    cout << endl; 

    cout << "What number would you like to add?" << endl;
    int added; 
    cin >> added;  

    for (int index = 0; added < *(pointer + index); index++)
    {
        spot = index;  
        cout << "check .5: " << spot;
    }
    for (int index = spot; index < input + 1; index++)
    {
        *(pointer + input + index) = *(pointer + index); //& added 
        cout << "check 1: " << *(pointer + input + index); 
    }
    *(pointer + spot) = added; 
    for (int index = spot + 1; index < input + 1; index++)
    {
        *(pointer + index) = *(pointer + index + input); 
        cout << "check 2" ;
    }

    for (int index = 0; index < input + 1; index ++)
    {
        cout <<  *(pointer + index); 
    }
    cout << endl; 
}
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
CLS_simple
  • 1
  • 2
  • 2
  • Do you have a MCVE, http://stackoverflow.com/help/mcve ? – inetknght Jan 06 '15 at 19:57
  • help us help you: show the code you have tried. Why do you tell a story about how your code looks? Show us the code! But don't dump on us 100 lines of code for a one line error – bolov Jan 06 '15 at 19:59
  • Also have a look at the help center about how to improve your question: http://stackoverflow.com/help/how-to-ask – moooeeeep Jan 06 '15 at 19:59
  • 1
    good. Ty for the code. Next steps :)) 1. remove the code not relevant to your question, leave only the minimum code needed to reproduce your problem. 2. Format/indent your code. It is really hard to follow unformatted code. – bolov Jan 06 '15 at 20:03
  • 3
    Why do you allocate twice as many numbers as the User specified? – Thomas Matthews Jan 06 '15 at 20:03
  • The calculation of the `spot` value depends on a sorted array. – Thomas Matthews Jan 06 '15 at 20:06
  • 1
    When you have `(pointer + input)` in an expression, the result is already out of bounds (unless you subtract something), for an array of single size (not double the size). – Thomas Matthews Jan 06 '15 at 20:08
  • 1
    fyi, `pointer[index]` is just as much "pointer notation" as is `*(pointer + index)`. In fact, the one is defined as a synonym of the other. – Benjamin Lindley Jan 06 '15 at 20:52

1 Answers1

1

Here is a demonstrative program that shows how to do the assignment by means of standard algorithms

#include <iostream>
#include <algorithm>

int main() 
{
    const size_t N = 5;
    int a[N] = { 2, 5, 1, 4, 3 };
    int b[N];

    int *first = b;
    int *last  = b;

    for ( int x : a )
    {
        auto p = std::upper_bound( first, last, x );

        if ( p != last )
        {
            std::copy_backward( p, last, last + 1 );
        }

        *p = x;
        ++last;
    }

    for ( int x : b ) std::cout << x << ' ';
    std::cout << std::endl;

    return 0;
}

The output is

1 2 3 4 5 

The approach is that you need to fill the array placing numbers in ascending order. In this case you should use the binary search method that to determine the position where a next number has to be added. And then you simply need to shift right all existent elements starting from this position.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335