1.1 Arrays in Data Structure | Declaration, Initialization,
Memory representation
We will see what is the need of array what does any types of array how array can
be declared how arrays can be initialized and how data is to be represented in
memory or you can see the memory representation of an array fine see I guess
you are aware with the fundamental data type status int float care double etc
and if I write like this int a is equal to 5 it means you have declared one variable
variable name is a type of the data type of this variable is int and the value
stored in this variable is 5 finally this is one variable variable
declaration you can see now how this venue is to be stored in memory let us
check so let us take this is our main memory you can draw it vertically you also
fine so see these are bytes so you can say memory is a long tape of bytes you
can say by it means 8 bits this is one byte this is one byte this is 1 byte like this
so this can be extended to this side and this side also that is why I am taking
open-ended so now how this variable can be stored here when the compiler
compile this line then what happens the word memory manager will do memory
manager will allocate some space for this variable to store this value 5 here how
much space would be allocated see the data type int how many bytes it will take
4 bytes in typical compilers to store an integer data type it will take 4 bytes in
traditional compilers we consider it two bytes so you can say 2 or 4 its approve
generally we take 4 bytes that is why I am taking for storing 1 integer it will take
4 bytes fine so we have only 1 integer that is why how much memory would be
allocated 4 bytes would be allocated 4 bytes fine it
means how many bits get into 4 that is 32 bits because one bite one bite is equal
to 8 bits fine now how the state has to be store first of all this decimal number 5
has to be converted into binary it's binary number sorry that is in 32 bits how in
32 bits this number can be represented like this 29 zeros would be there and
then we'll write 1 0 1 now this binary this binary number is to be stored in
memory now how this data is to be stored this 4 bytes is to be allocated 1 2 3 4
let us suppose these 4 bytes are
to be allocated to store this 5 like this these 4 bytes so these 4 bytes are this is a
closer look here we are going to store these visits like this and 1 0 1 and the end
8 bits here eight bits 8 bits 8 bits that is 32 bits this is how the number is going to
be stored in memory fine and suppose at address you can say C address is
always in hexadecimal form but here for simplicity purpose I am taking let us
suppose the addresses address of this byte is hundred let us suppose so address
of this byte would be 1 0 1 1 0 2 & 1 0 3
so it's 100 1 0 1 1 0 2 1 0 3 these 4 bytes to be used to store the center see this
address would be in hexadecimal form always it's not like hundred or thousand
or something like this hexadecimal means always hexadecimal would be
represented by prefix with the 0x then you can say 677 see and 0 something like
this in this form the address would be there but here I am taking just hundred for
simplicity purposes so now I'll discuss what is need of Faerie see in this variable
we can store only one value at a time if I
want to store 6 then what would happen I will write here 6 and 5 is overwritten 5
is no more now fine if I want to store 7 then I will write 7 and now 5 and 6 are no
more and here you will insert 7 or you can say overwritten those 5 & 6 so at one
, time you can store only one value if I want to store more one will you suppose in
my class I have sixty students and I want to store all umbers of all the 60
students now what would happen you have to take in this case if you follow this
approach in that case we'll have to take sixty variables
because one variable can only store on one value at a time see because we
have to process large amount of return that is why the concept of array came
now you will modify this declaration such that under one variable name we can
store roll numbers of all 60 students that is what array now how you can do this
see same data type would be there here we have name but rather than storing
one value now I want to store 16 values now obviously you how to tell the
compiler that I want to store 60 values then you have to give the size of your
array so here like this we are going to give the size fine so this is what you can
say that declaration of a general syntax would be what data type then air a
name any name is a then in brackets size of the air and here you will write what
constant well okay see this is invalid declaration if you write int a this this is
invalid declaration you cannot leave it blank this is also invalid declaration if you
write into a and here you write sizes n sorry int N and here you here you write n
this is also invalid declaration here you have
to write what constant you can write here a variable but in that case for that
case you have to declare macros before main function and below that header
files you will declare one macro you can say hash define variable name n size is
hundred in that case after that in main function you can write int a and in
breakers you can write that variable name fine that is different logic but simply
you cannot declare something like this you have to declare this datatype then
arrayname then sighs sighs should be constant now see this declaration of
arrays language-specific I am here considering the syntax in C language in
Python maybe it's different in Pascal in you can say that foreign language the
declaration syntax would be a little bit different so it is language specific fine now
this is what the array declaration now see this is declaration of 1d array types of
era types of array three types of arrays are there one day array one dimensional
two dimensional and multi-dimensional array so in this video I am going to talk
about 1b array so this is declaration of 1d array so in this we are having one row
with 60 columns 60 values in columns fine now see how the arrays can be
initialized and after that we will discuss how the air is can be represented in
memory so see how you can define an array what is definition of error you can
say that array is a collection of more than one data item so more than one
elements but condition is what the data type of those elements should be same
or you can say collection of for the more than one data items of same data tank
see here five we can store five elements in the
array but all the five elements should be of integer type let us take this example
see this is a valid array because here the size of air is 1 2 3 4 5 5 elements are
there but all the five elements are of same data-type that is integer here all the
data items are of same data-type that is character you can also declare a
character array like this data type should be character suppose name of arrays a
or b you can take and size is suppose and that is also fine you can take float area
also like this float array name is suppose B and I am
taking size is fine so that is also fine but see this is fine this is fine because all in
this in this array all the elements are character type same like but this is not valid
this is not valid because in this array one-two-three-four-five size is five but here
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller RobertBrown17. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £5.87. You're not tied to anything after your purchase.