I'm suppose to write a program that asks the user to enters numbers which will be stored in a dynamic array. Then once they enter their first set of numbers they are asked whether they would like to enter another number or remove a number from their list. You can assume the user doesn't enter a number more than once. Then finally once the user is done entering and removing integers i have to pring the the numbers in the array in increasing order. To do this program i am suppose to use a dynamic array. I created three functions. One to add a number, one to remove a number, and one to print the numbers in increasing order. I was able to get all of the program working besides the funtion that prints the integers in increasing order. To do this function i was told to look for the smallest number in the array, print it and then erase it (using removeNumber()). Continue doing it until all elements of the array are removed. I have it somewhat working in that i can get it to find the smallest number and print it out, but then when i try to put it in a loop to print the rest of the array it prints like repeats of the numbers and doesnt work like its suppose to. If someone could please help me fix this output function in my program that would be great. My code is below.

#include <iostream>

using namespace std;

// adds "number" to the array pointed to by "array" of "size".
// Note the size of the array is thus increased. Note also
// that the pointer to the array is passed by reference
void addNumber(int *& array, int number, int &size);

// removes a "number" from the "array" of "size".
// if "number" not there -- no action
// note, "size" changes
void removeNumber(int *& array, int number, int &size);

// prints the values in "array" of "size" in sorted order
void output(int *array, int size);


typedef int* IntArrayPtr;

int main(){

        int size, num_add, num_remove;
        char array_action, choice;

        cout << "Enter the number of integers." << endl;
        cin >> size;

        IntArrayPtr array;
        array = new int[size];

        cout << "Enter integers" << endl;
        for(int index = 0;index < size;index++)
        {
                cin >> array[index];
        }
        for(int index = 0; index < size;index++)
        {
                cout << array[index] << " ";

        }
        do{
              cout << "done? [y/n]." << endl;
              cin >> choice;
              if(choice == 'n')
        {
                cout << "add or remove? [a/r]" << endl;
           cin >> array_action;
           if(array_action == 'a')
           {
                cout << "input number: " << endl;
                cin >> num_add;
                addNumber(array,num_add,size);
                for(int index = 0;index < size;index++)
                {
                        cout << array[index] << " ";
                }
           }
           else
           {
                cout << "input number: " << endl;
                cin >> num_remove;
                removeNumber(array,num_remove,size);
                for(int i=0;i<size;i++)
                {
                        cout << array[i] << " ";
                }
           }

        }
        }while(choice == 'n');

        output(array,size);


}
void addNumber(int *& array, int number, int &size)
{
        IntArrayPtr temp;
        size = size+1;
        temp = new int[size];
        for(int i=0;i<size;i++)
        {
                     if(i<size-1)
                {

                        temp[i]=array[i];

                }
                else
                {
                        temp[i] = number;
                }
        }
        delete [] array;
        array = temp;

}
void removeNumber(int *& array,int number, int &size)
{
        IntArrayPtr temp;
        size = size-1;
        temp = new int[size];
        for(int i=0;i<size;i++)
        {
                if(array[i] == number)
                {
                        for(int j=i;j<size;j++)
                        {
                                array[j] = array[j+1];
                        }
                }
                else
                {
                        continue;
                }
        }
        for(int i=0;i<size;i++)
        {
                temp[i]=array[i];
        }
        delete [] array;
        array = temp;
}
void output(int *array, int size)
{

        int temp = array[0];
        for(int i=0;i<size;i++)
        {
                for(int j=0;j<size;j++)
                {

                        if(array[j]<temp)
                        {
                                temp = array[j];
                        }
                }
                cout  << temp << " ";
                removeNumber(array,temp,size);

        }

}

Hi i have only skimmed your code but there are a couple of things that stand out as potential problems

1: in remove number:
with the first for loop you want the original size not size -1, otherwise, if you try to remove the last number in array it won't be found.

void removeNumber(int *& array,int number, int &size)
{
        IntArrayPtr temp;
        size = size-1;
        temp = new int[size];
        for(int i=0;i<size;i++)
        {
                if(array[i] == number)
                {
                        for(int j=i;j<size;j++)
                        {
                                array[j] = array[j+1];
                        }
                }
        }

so you need to add a variable say istop that has the old size to use with the loop before you save the new size

The other issue won't be so obvious and may not matter for you usage, again addNumber has a size being used twice where different values are wanted. and output should leave array unaltered.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.