#5-Dynamic Memory Allocation in C++-Program with explanation

Write a program to store n numbers dynamically and find the average using casting operator.

So, first let’s go and learn a little bit about the dynamic memory allocation.

There is a feature of C++. It is called as new and delete operator.

New and delete operator in C++ are used for dynamic memory allocation and deallocation respectively.

Then first let’s be clear about what is dynamic memory allocation?

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

Sometimes while making big softwares, the programmers may find inconvenient to initialize memory before finishing/continuing the program. So, it will be easier for them if they can declare memory when they are writing the code instead of doing it at first. So, for this purpose, the concept of dynamic memory was started. According to this dynamic memory allocation concept, we can initialize the memory we want, at, any time. It doesn’t matter whether we are at the middle of the program or start of the program.

In c++, memory can be allocated dynamically by using new operator. And we can also de-allocate/free memory b y using delete operator.

Whereas, in C; we used the calloc() and malloc() and free functions for this purpose.

So, now let’s go back to the initial question and let’s write the source code for it. It is shown below-:

The output of the above code will come like below-:

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

 

How can you understand this code? This seems tedious at first but actually it is not so tedious.

 c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,
c plus plus program, c++ programming, c++ programming tutorial, c++ tutorial, cplusplus tutorial, solved c++ programs, c++ practical programs, c++ tutorial for engineering, c++ in codeblocks tutorials, c++ online academy,

I will reverse engineer the solution of it.

The above picture speaks more than 1000 words that I am going to write now.

So, let’s start to discuss about the program source code, let’s understand how the program flows and how can we write the source code without looking at the program.

As above, I am also going to reverse engineer it.

We need to find the avg. And that average is using the dynamic memory allocation, so we need to use the static_cast operator.
And, remember that there is a rule to write static_cast.

It’s static_cast<float>(sum)/n;

Then, we need to find the sum. And, the sum is-:

sum=sum+ptr[i];

And, to take this value[i]; it is clear that we need to create a for loop;and it is what is created above.

Now, we need ptr[i];

To take this input from the user, it is again clear that we need a loop.

so, we did-:

cin>>ptr[i]; within a loop.

Now, we need ptr, and we allocated it by doing-:

ptr=new int[n];

AND, before that we took n input from the user.

Before that, we also initialized *ptr as pointer variable.

So, this is the explanation of the program.

Now, Homework For You-:

I know that this program may confuse you at first. So, it will be better for you, if you write this program 2 times by yourself; once in your copy and next in your compiler.

That will make you learn this program 100% well.

Stay conscious!!!

Keep Learning

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top