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