#How to make my arrays use Dynamic Memory Allocation?

59 messages · Page 1 of 1 (latest)

dusky trail
#

Program takes in 5 teams no matter what right now but I want the user to state how many teams there are first, then go off of that number instead of the default 5.

I like jumping out of airplanes*/
#include <iostream>
using namespace std;

void initializeArrays(string names[], int wins[], int size);
void sortData(string names[], int wins[], int size);
void displayData(string names[], int wins[], int size);

int main()
{
    const int NUM_TYPES = 5;
    int numTeams;
    int* ptr = NULL;

    cout << "How many teams will you enter?: ";
    cin >> numTeams;

    
    string names[NUM_TYPES] = {};
    int wins[NUM_TYPES] = {};
    initializeArrays(names, wins, 5);
    sortData(names, wins, 5);
    displayData(names, wins, 5);


    return 0;
}

void initializeArrays(string names[], int wins[], int size) {
    int rotations[5] = { 1, 2, 3, 4, 5 };
    for (int i = 0; i < 5; i++) {
        cout << "Enter team #" << rotations[i] << ": ";
        cin >> names[i];
        cout << "Enter the wins for team #" << rotations[i] << ": ";
        cin >> wins[i];

    } return;

}
void sortData(string names[], int wins[], int size) {

    for (int j = 0; j < 5; j++) {

        for (int i = 0; i < 5 - 1; i++) {
            if (wins[i] < wins[i + 1]) {
                string temp;
                temp = names[i];
                names[i] = names[i + 1];
                names[i + 1] = temp;
                int x = wins[i];
                wins[i] = wins[i + 1];
                wins[i + 1] = x;

            }
        }
    }
    return;
}
void displayData(string names[], int wins[], int size) {
    int rotations[5] = { 1, 2, 3, 4, 5 };
    cout << "League Standings:" << endl;
    for (int i = 0; i < 5; i++) {
        cout << names[i] << ": " << wins[i] << endl;
    }

    return;
}
rapid jungleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

quiet dome
#

is there a reason you have to use C-style arrays instead of std::vector?

#

if so then you need to use new (or malloc) if you want an array whose size is not known until runtime

dusky trail
#

Yeah trying to learn how the pointers work without the vectors

normal trail
#

int *arr = new int[size] where size can be any integer

dusky trail
#

tried that but the name wouldn't work, and with the name the int wouldnt work, etc

normal trail
#

i didnt read your code im just telling you how to use dynamic memory allocation in the arrays

dusky trail
#

Ah, well I know what it looks like but I'm not sure what to change here as what I thought I needed to do didn't work

#

I did that, wasn't running 😦

normal trail
#

whats int rotations[5] for ??

dusky trail
#

Just to make it the same as the default size of the names and prices array

#

So there's something else I need to work on lol

normal trail
#

i didnt understand your question

#

you make 5 arrays and then input one team in each array right ?

dusky trail
#

nono there is only two arrays

#

price and wins

#

in main

normal trail
dusky trail
#
    int wins[NUM_TYPES] = {};```
normal trail
#

so you want to enter as many names as the user wants in the names array right ?

#

and as many wins as the user wants in the wins array

dusky trail
#

ye

normal trail
#

okay

dusky trail
#

If theres 4 games, it asks the user 4 times for team names and the wins

normal trail
#

this firstly to make variable length arrays is bad practice

dusky trail
#

Gotchya

normal trail
#
int num_of_elements = 0;
cin >> num_of_elements; //user enters the number of elements in the array
int *arr = new int[num_of_elements]; // allocates memory for the number of elements given 
for (int i = 0; i < num_of_elements; i++)
  cin >> arr[i]; // taking input in the array
#

if you use int arr[n] the n can be more than what more than what memory you have on the stack and your program may crash

dusky trail
#

which is the way I originally did it right?

normal trail
#

so int *array_name = new int[size] allocates memory on the heap and gives you a pointer to the first element of the array (the name of the array always points to the first element of the array)

dusky trail
#

So I would replace the string names[NUM_TYPES] with that array

normal trail
#

just change the type

dusky trail
#

One sec, ima try this then

#

Two things: so I need two pointers then?

#

And, I did that still doesn't work:

#
int *wins = new int[NUM_TYPES] = {};```
normal trail
#

thats wrong

#

the new keyword just allocates memory for you to use

dusky trail
#
int* wins = new int[numTeams];```
#

Gotchya okay so, now I know how to write an array with a pointer

#

Gotta do the same for the string array right?

normal trail
#

also after using the array at the end of the program you should free the allocated memory

dusky trail
#

Well, before that it still goes over 5 times by default 😦

normal trail
#

using

delete []array_name; 
dusky trail
#

yeah so im still confused on how its dynamic if my functions still use (int j = 0; j < 5; j++)

normal trail
#

your function should use j < size

dusky trail
#

ahhh one sec

#

okay so it still goes to 5, but breaks if I say anything less than 5 teams

normal trail
#
Dynamic memory allocation with new
Memory allocated dynamically is taken from the heap memory. Every program at runtime is allocated a temporary memory called stack memory.
int arr[n];
the above code is trying to create a array of user input length but this is bad practice and in most cases not allowed by the compiler as the memory is being used from the stack and the input may be greater than the memory available in the stack thus causing the program to crash.
To avoid this memory is allocated dynamically using the new keyword. This memory is allocated in the heap. the memory cannot be allocated to a variable in as the address is being allocated and hence a pointer is used to store the address.
dusky trail
#

So I can logically understand that, but I can't visually see that in code

normal trail
#

no lol

dusky trail
#

mb 😦

rapid jungleBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.