If u wanna code her, You must feel her!

Array : Smart Declaration




At the beginning, let's see two short codes before we begin our discussion.
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int ara[500000];
  5.         int n;
  6.         scanf("%d", &n);
  7.         for(int i = 0; i < n; i++) {
  8.             scanf("%d", &ara[i]);
  9.         }
  10.         for(int i = 0; i < n; i++) {
  11.             printf("%d ", ara[i]);
  12.         }
  13.     } 

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int ara[n];
  5.         int n;
  6.         scanf("%d", &n);
  7.         for(int i = 0; i < n; i++) {
  8.             scanf("%d", &ara[i]);
  9.         }
  10.         for(int i = 0; i < n; i++) {
  11.             printf("%d ", ara[i]);
  12.         }
  13.     }


Now, what is the differences between these two codes? Can you guess? If you cannot guess, then see those codes again to understand the difference. If you have read these two codes carefully, then move to the next section.


In the first code, we declared an array of size 500000. Then we scanned a value of n and took input of n integers as the elements of the array. Now, if n = 5, almost whole of the array will be left unused. On the other hand, if n > 500000, the array will not be able to store all elements we need. Moreover, we will get a runtime error!

So, to be a smart programmer, we declare the array as of the second example and BINGO! We feel like we are smarter than ever! But the truth is - "we get a compilation error!" Why?

Let's have a closer look at the code again. At first, we declared ara[n] and then we declared n. We know that C/C++ programs run line by line and from top to bottom. In the first line, we declared it will see that we have used a variable n as the size of the array but it does not know anything about n. That's why we get a compilation error. So we must declare n before we use it as the size of the array. And for fixing this bug, we write the following code:

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int n;
  5.         int ara[n];
  6.         scanf("%d", &n);
  7.         for(int i = 0; i < n; i++) {
  8.             scanf("%d", &ara[i]);
  9.         }
  10.         for(int i = 0; i < n; i++) {
  11.             printf("%d ", ara[i]);
  12.         }
  13.     }


Ah! Relief! Is it?

No, it's not. We might get a runtime error! Why? What did we do wrong? Let's have a closer look at the code again. If you can understand the problem by yourself, that's very good for you. But if you cannot understand, do not panic! Because, panic will seize the day! Move on to the next portion.

In this code, we declared n before we used it. That's fine. But can you please tell me what is the value of n while we were declaring the array with size n?

I guess now you know the answer! Yes, you are right! We declared n, but we did not assign an value to n and besides, we declared n as the size of an array. So, this is wrong. And this the reason, we get a runtime error. So, before we declare int ara[n], we must assign a value to n or the value of n can be scanned before declaring the array.

So, the Smartest Declaration is as in the following code - 

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int n;
  5.         scanf("%d", &n);
  6.         int ara[n];
  7.         for(int i = 0; i < n; i++) {
  8.             scanf("%d", &ara[i]);
  9.         }
  10.         for(int i = 0; i < n; i++) {
  11.             printf("%d ", ara[i]);
  12.         }
  13.     }

And that is - we declare the array after we have the size of the array in our hand and not before that. And this is how we can make the best use of our memory!

If you have any query, please comment and if you like this post, please share with others and comment!
Do not forget to Follow By Email.

Happy Coding!



No comments

Theme images by enot-poloskun. Powered by Blogger.