Growing Arrays in C Varoon Wadhwa
Arrays An array is basically a variable that contains a list Arrays are one of the simplest data structures in C They have many uses and a broad range of tools associated with them
What are Growing Arrays? Growing Arrays are arrays that are constantly having new sorted values added to them They are used in situations where we need to keep track of a variable Often overshadowed by hash tables due to the amount of resources needed for large arrays
Comparisons to C++/Java In C++ or Java, Growing Arrays would be replaced by classes from standard libraries In C, these Growing Arrays are constructed using a struct struct { char cname[8]; char sname[16]; char exam[16]; char grade; } record;
Struct A struct is used to declare a new data type This basically means grouping variables together Similar to enum in C++
Why Grow Arrays? Growing arrays is usually a resource hungry operation Used when elements added (n) is relatively small Very convenient data structure Fixed size data sets, arrays ideal
Why not use Growing Arrays? Changing set of values in arrays can be expensive Number of elements unpredictable Number of elements potentially large n
Benefits of Growing Arrays Arrays are simplest way to group data Most languages have efficient and convenient tools for arrays Arrays work well with sorting algorithms Provide easy access to any item
Drawbacks of Growing Arrays Cannot handle large amounts of data change Requires resorting of entire array upon deletion of elements Elements can sometimes be overwritten by fast “squeezing” algorithms Must decide on correctness or speed
Deleting an element Deleting a element in an array is tricky Deleting the element results in a gap in the array Easiest solution is to swap last element into the gap More complicated if order matters
Questions???