Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

dynamic array resizing

$
0
0
I have to write a program where the user will input integer numbers. How many numbers they enter is unknown, therefor you should use a repetition structure for the input. When the user is done, they will enter -1 to exit.

Create a dynamic array if the size=2( the initial size must be 2)
Repeat until user enters -1.

I have to do this without using vectors.

This is what i have, I cannot figure out what to put in main. I was thinking of a do-while?

This is my first time in c++
Any hints?


Code:

#include <iostream>
using namespace std;
void resize(int *[], int);
int main()
{
        int *listDyn;
        int size=2;
        listDyn=new int[size];

       
                for (int i=0; i<size; i++)
                {
                        cout << "Enter as many integers as you'd like or enter -1 to exit" ;
                        cin >> listDyn[i];
                }


void resize(int *listDyn, int size)
{
        int *listDynNew=new int[size*2];
        for (int i=0; i<size; i++)
                listDynNew[i]=listDyn[i];
        size++;
        listDyn=listDynNew;
        delete[] listDynNew;
}


Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images