Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,158 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,122 people online right now. Registration is fast and FREE... Join Now!




Increase Array Size Question?

 
Reply to this topicStart new topic

Increase Array Size Question?

lockdown
6 Dec, 2007 - 09:16 AM
Post #1

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
I am creating a program that will take grade scores that a user enters and then do certain operations with them. I want to create it so the user can enter as many grade scores as they want.

I am using an array for this program for the grade scores but I have run into a mental road block. I am trying to determine a way to increase my array size as the user enters in more grades. Ex. The array is set to have 2 values but the user enters in a third grade. I want the array to be able to increase from 2 values to 3 values. scores[1] increase to scores[2].

I have tried a couple things such as giving the array size a name and increases that as I need to inside a for loop. But that caused issues because I believe its a illegal operation? Or just wrong. So I removed that from my code and went back to a basic array to try and determine something else.

Here is some of the code that I am working with.
CODE

void main()
{
    int scores[4];
    int count=0;
    int score=0;

    cout << "Enter 0 when all grades have been entered" <<endl;

    for (count=0;count<=4;count++)
        {
            cout << "Please enter the grades ";
            cin >> score;
            if (score == 0)
            {
                break;
            }
                if (score < 0)
                {
                    while (score < 0)
                    {
                        cout << "Error - Invalid Score. Please only enter positive scores ";
                        cin >> score;
                            if (score == 0)
                            {
                                break;
                            }
                    }
                }
            scores[count] = score;
        }


Is their a way to increase the array size or is it something that cant grow as the application needs to?

This post has been edited by lockdown: 6 Dec, 2007 - 09:18 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Increase Array Size Question?
6 Dec, 2007 - 11:08 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
Ah the wonders of dynamic memory needs.

There are two ways to create and array. You can create a static array inside the local memory space:

int array[100]; //creates an array of 100 elements.

the problem with this is that it is static. You hard code into your program the size of the array. When the program loads, it sets that much memory aside (which can eat away at the memory available to your program for function calls and other needs.). (Note that if you create a static array in a function then it is allocated on the stack when the function is called, so for example if you call the function recursively that can eat up memory fast).

There is a more dynamic way. You can ask the operating system for a chunk of free memory using the malloc() in C or the new in C++.

char *array = new (nothrow) int[100]; //allocates a dynamic array...


note: the '(nothrow)' part just tells the compiler not to use C++'s exception handling ability if there is an error. It is actually better to use the exceptions.
The new[] operator needs to be paired with the delete[] operator which releases the memory back to the operating system. (note the 'new' operator can be used to allocate a single object, and it is paired with the 'delete' operator, but arrays use the 'new[]' operator and 'delete[]' operator).

delete[] array;// return the memory to the OS

Now the new operator does not have a fancy "re-Allocate" ability, but you can write your own routine to allocate a new array, copy the data over, and then delete the old one.

A better solution is to use a vector object to handle all your array needs.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Increase Array Size Question?
6 Dec, 2007 - 11:14 AM
Post #3

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 617



Thanked: 14 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
A similar discussion has been taking place in another thread. the best way is to ditch arrays .. http://www.dreamincode.net/forums/showtopic38894.htm
User is offlineProfile CardPM
+Quote Post

lockdown
RE: Increase Array Size Question?
6 Dec, 2007 - 12:07 PM
Post #4

D.I.C Regular
Group Icon

Joined: 29 Sep, 2007
Posts: 376



Thanked: 1 times
Expert In: PC, Support

My Contributions
Sweet. Thanks for the explanation and other thread.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 11:27PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month