array problems

im trying to create an array that sorts the input numbers in increasing order. the problem i am having is shifting the array to make room for the new number. if anyone has any ideas to fix this it would be greatly appreciated.

ex. if 1 1 4 3 2 is inputted the array would be 1 1 2 3 4. thanks again
Are you using any sorting algorithm in particular?
I don't know what you mean with "shifting the array" or "make room for the new number" - cause there is no new number in your example. Also, I don't know what you mean by "create an array that does that" - do you want to sort an array, or do you want to write a self sorting array class?
sorry. if you take my example after inputting 1 twice the array would be 1 1 then after the four it would be 1 1 4 but then i have problems trying to input the three because i dont know how to shift the value of the four one space in the array to allow there to be an open space for the 3. hope this clears up the confusion.
"Arrays" by definition have a static size. You'd have to create a new array that is larger than the previous one. I think what you're looking for is a list.
i know how to make it the write size. my problem is getting the numbers in the right order. here is what i have coded now if it will help.
1
2
3
4
5
6
7
8
9
10
11
12
		
for (int i = 0; i < size; i++) {
	if (input < data[i]) { //data is the array that should be sorted
				
	        data[i] = input; //writes inp OVER array[i]. need to shift array and make new spot!
		
	}//end if
	else{
		data[size-1] = input;
				
	}
}//end for	 


lets say i input 1 3 2 and print out the array after each input is entered. it will output
1
1 3
1 2 2

because instead of moving the three over a space it just writes over it. im trying to figure out how to move the three to the last spot in the array and put 2 where it belongs.
If you really want to do it with arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

using namespace std;

int data[200], datacopy[200];
int x = 0, y = 0, input = 0, z = 0;

int main()
{
    x = 0;
    while(x < 200)
    {
        data[x] = -1;
        datacopy[x] = -1;
        ++x;
    }
    while(z < 200)
    {
        cin >> input;
        x = 0;
        while(x < 200)
        {
            datacopy[x] = data[x];
            ++x;
        }
        x = 0;
        while(input < data[x])
        {
            ++x;
        }
        data[x] = input;
        y = x;
        x = y  + 1;
        while(x < 200)
        {
            data[x] = datacopy[x - 1];
            ++x;
        }
        x = 0;
        while(x < 200)
        {
            if(data[x] >= 0)
            {
                cout << data[x] << " ";
            }
            ++x;
        }
        cout << endl;
        ++z;
    }
    return 0;
}


Although simply using something like list from the STL would be easier probably.

thanks so much. we never learned list so i have to use arrays. thanks for you help
Topic archived. No new replies allowed.