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

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,

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

4 Comments

  1. tbh, it seems like you should improve your own C++ first before trying to start a tutorial blog. What you are displaying here is really bad code. Owning raw pointers, new, delete, dynamic arrays, variable forward declaration are all no-nos in good C++.

    This is what an actually good implementation of the “average” problem could look like:

    #include
    #include
    #include

    int main() {
    std::vector values;
    int v;
    std::cout << "Provide values: " <> v)
    values.push_back(v);

    const float sum = std::accumulate(values.begin(), values.end(), 0.f);
    const float avg = sum / values.size();
    std::cout << "average: " << avg << std::endl;

    return 0;
    }

    • I am writing the code which is taught in most colleges. And, most of us are taught like that in colleges. And most of my readers are beginners and intermediate students. I am never going to write codes like that, so sorry. But, you can Keep commenting!

      • Then, sorry to tell you, but your college is teaching you bad code. If you are serious about becoming a good programmer, the right decision would be to start learning C++ on your own with a good book; the “C++ Primer” by Lippman e.g. is a popular choice.

        You don’t have to apologize for writing bad code. It doesn’t affect me. I’m just giving you advice; having experience with tech companies such as Facebook and Google, I can tell you that you won’t make it far if you don’t change direction.

        • you are right but i am making this series for beginners so not to confuse them with writing std::cout<<"exp" instead of "using namespace std;" at the beginning. once, someone knows how to write a basic code, i think they can research on their own to write better code. your valuable suggestion is highly appreciated. I may be thinking about that soon.

Leave a Reply

Your email address will not be published.


*